🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
chore: limit traces to trace AMT migration to only write into traces_all_amt (#8458)
Steffen Schmitz011b4912f29c
diff --git a/worker/src/backgroundMigrations/migrateTracesToTracesAMTs.ts b/worker/src/backgroundMigrations/migrateTracesToTracesAMTs.tsindex 4497a78843..047b633250 100644--- a/worker/src/backgroundMigrations/migrateTracesToTracesAMTs.ts+++ b/worker/src/backgroundMigrations/migrateTracesToTracesAMTs.ts@@ -16,6 +16,7 @@ type MigrationState = {maxDate: string | undefined;minDate: string | undefined;queryTimeoutMinutes: number | undefined;+ targetTracesAllAmtOnly: boolean | undefined;};/**@@ -91,9 +92,7 @@ async function executeLongRunningQuery(const abortController = new AbortController();const timeoutMs = timeoutMinutes * 60 * 1000;- logger.info(- `[Background Migration] Executing traces_null backfill query ${queryId}`,- );+ logger.info(`[Background Migration] Executing backfill query ${queryId}`);// Start the query executionconst queryPromise = client.command({@@ -208,7 +207,7 @@ export default class MigrateTracesToTracesAMTs implements IBackgroundMigration {};}- // Check if new ClickHouse tables exists+ // Check if required ClickHouse tables existconst tables = await clickhouseClient().query({query: "SHOW TABLES",});@@ -249,6 +248,10 @@ export default class MigrateTracesToTracesAMTs implements IBackgroundMigration {const queryTimeoutMinutes =initialMigrationState.state?.queryTimeoutMinutes ??(args.queryTimeoutMinutes as number | undefined);+ const targetTracesAllAmtOnly =+ initialMigrationState.state?.targetTracesAllAmtOnly ??+ (args.targetTracesAllAmtOnly as boolean | undefined) ??+ false;const maxDate = initialMigrationState.state?.maxDate? new Date(initialMigrationState.state.maxDate)@@ -264,6 +267,7 @@ export default class MigrateTracesToTracesAMTs implements IBackgroundMigration {maxDate,minDate,queryTimeoutMinutes,+ targetTracesAllAmtOnly,},},});@@ -272,14 +276,21 @@ export default class MigrateTracesToTracesAMTs implements IBackgroundMigration {const queryStart = Date.now();// @ts-ignore- const migrationState: { state: { maxDate: string; minDate: string } } =- await prisma.backgroundMigration.findUniqueOrThrow({- where: { id: backgroundMigrationId },- select: { state: true },- });+ const migrationState: {+ state: {+ maxDate: string;+ minDate: string;+ targetTracesAllAmtOnly?: boolean;+ };+ } = await prisma.backgroundMigration.findUniqueOrThrow({+ where: { id: backgroundMigrationId },+ select: { state: true },+ });const maxDate = new Date(migrationState.state.maxDate);const minDate = new Date(migrationState.state.minDate);+ const targetTracesAllAmtOnly =+ migrationState.state.targetTracesAllAmtOnly ?? false;// Get current month in YYYYMM formatconst currentMonth = maxDate.toISOString().slice(0, 7).replace("-", "");@@ -287,45 +298,89 @@ export default class MigrateTracesToTracesAMTs implements IBackgroundMigration {`[Background Migration] Migrating traces for ${currentMonth}`,);- const query = `- INSERT INTO traces_null- SELECT- -- Identifiers- project_id,- id,- timestamp as start_time,- null as end_time,- name,-- -- Metadata properties- metadata,- user_id,- session_id,- environment,- tags,- version,- release,-- -- UI Properties- bookmarked,- public,-- -- Aggregations (ignored)- [] as observation_ids,- [] as score_ids,- map() as cost_details,- map() as usage_details,-- -- Input/Output- input,- output,-- created_at,- updated_at,- event_ts- FROM traces- WHERE toYYYYMM(timestamp) = ${currentMonth}- `;+ const targetTable = targetTracesAllAmtOnly+ ? "traces_all_amt"+ : "traces_null";++ const query = targetTracesAllAmtOnly+ ? `+ INSERT INTO ${targetTable}+ SELECT+ -- Identifiers+ project_id,+ id,+ t.timestamp as timestamp,+ t.timestamp as start_time,+ t.timestamp as end_time,+ name,++ -- Metadata properties+ metadata,+ user_id,+ session_id,+ environment,+ tags,+ version,+ release,++ -- UI Properties+ arrayReduce('argMaxState', [toNullable(bookmarked)], [event_ts]) as bookmarked,+ arrayReduce('argMaxState', [toNullable(public)], [event_ts]) as public,++ -- Aggregations+ [] as observation_ids,+ [] as score_ids,+ map() as cost_details,+ map() as usage_details,++ -- Input/Output+ arrayReduce('argMaxState', [coalesce(input, '')], [if(coalesce(input, '') <> '', event_ts, toDateTime64(0, 3))]) as input,+ arrayReduce('argMaxState', [coalesce(output, '')], [if(coalesce(output, '') <> '', event_ts, toDateTime64(0, 3))]) as output,++ created_at,+ updated_at+ FROM traces t+ WHERE toYYYYMM(t.timestamp) = ${currentMonth}+ `+ : `+ INSERT INTO ${targetTable}+ SELECT+ -- Identifiers+ project_id,+ id,+ timestamp as start_time,+ null as end_time,+ name,++ -- Metadata properties+ metadata,+ user_id,+ session_id,+ environment,+ tags,+ version,+ release,++ -- UI Properties+ bookmarked,+ public,++ -- Aggregations (ignored)+ [] as observation_ids,+ [] as score_ids,+ map() as cost_details,+ map() as usage_details,++ -- Input/Output+ input,+ output,++ created_at,+ updated_at,+ event_ts+ FROM traces+ WHERE toYYYYMM(timestamp) = ${currentMonth}+ `;await executeLongRunningQuery(query, queryTimeoutMinutes ?? 90);@@ -341,7 +396,7 @@ export default class MigrateTracesToTracesAMTs implements IBackgroundMigration {});logger.info(- `[Background Migration] Inserted traces into traces_null for ${currentMonth} in ${Date.now() - queryStart}ms`,+ `[Background Migration] Inserted traces into ${targetTable} for ${currentMonth} in ${Date.now() - queryStart}ms`,);if (maxDate < minDate) {@@ -412,6 +467,10 @@ async function main() {short: "t",default: "90",},+ targetTracesAllAmtOnly: {+ type: "boolean",+ default: false,+ },},});