Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

chore: remove AMT documentation and background migration (#9465)

Steffen Schmitz819fa50f2ecd
diff --git a/worker/src/backgroundMigrations/migrateTracesToTracesAMTs.ts b/worker/src/backgroundMigrations/migrateTracesToTracesAMTs.ts
deleted file mode 100644
index 047b633250..0000000000
--- a/worker/src/backgroundMigrations/migrateTracesToTracesAMTs.ts
+++ /dev/null
@@ -1,505 +0,0 @@
-import { IBackgroundMigration } from "./IBackgroundMigration";
-import {
- clickhouseClient,
- ClickhouseClientType,
- logger,
-} from "@langfuse/shared/src/server";
-import { parseArgs } from "node:util";
-import { prisma } from "@langfuse/shared/src/db";
-import { env } from "../env";
-import { randomUUID } from "node:crypto";
-
-// This is hard-coded in our migrations and uniquely identifies the row in background_migrations table
-const backgroundMigrationId = "f4b51797-e5ae-4d74-9625-05321493486e";
-
-type MigrationState = {
- maxDate: string | undefined;
- minDate: string | undefined;
- queryTimeoutMinutes: number | undefined;
- targetTracesAllAmtOnly: boolean | undefined;
-};
-
-/**
- * Checks if a query exists in the system.query_log table
- */
-async function checkQueryExists(
- client: ClickhouseClientType,
- queryId: string,
-): Promise<boolean> {
- const queryLogTable =
- env.CLICKHOUSE_CLUSTER_ENABLED === "true"
- ? `clusterAllReplicas(${env.CLICKHOUSE_CLUSTER_NAME}, system.query_log)`
- : "system.query_log";
-
- const resultSet = await client.query({
- query: `
- SELECT COUNT(*) > 0 AS exists
- FROM ${queryLogTable}
- WHERE query_id = '${queryId}'
- ${env.CLICKHOUSE_CLUSTER_ENABLED === "true" ? " SETTINGS skip_unavailable_shards = 1 " : ""}
- `,
- format: "JSONEachRow",
- });
- const result = (await resultSet.json()) as { exists: 0 | 1 }[];
- return result.length > 0 && result[0].exists !== 0;
-}
-
-/**
- * Checks if a query has completed successfully
- */
-async function checkCompletedQuery(
- client: ClickhouseClientType,
- queryId: string,
-): Promise<boolean> {
- const queryLogTable =
- env.CLICKHOUSE_CLUSTER_ENABLED === "true"
- ? `clusterAllReplicas(${env.CLICKHOUSE_CLUSTER_NAME}, system.query_log)`
- : "system.query_log";
-
- const resultSet = await client.query({
- query: `
- SELECT type, exception
- FROM ${queryLogTable}
- WHERE query_id = '${queryId}' AND type != 'QueryStart'
- LIMIT 1
- ${env.CLICKHOUSE_CLUSTER_ENABLED === "true" ? " SETTINGS skip_unavailable_shards = 1 " : ""}
- `,
- format: "JSONEachRow",
- });
- const result = (await resultSet.json()) as {
- type: string;
- exception: string | undefined;
- }[];
- if (result.length > 0 && result[0].type !== "QueryFinish") {
- throw new Error(
- `Query ${queryId} failed with ${result[0].type}: ${result[0].exception}`,
- );
- }
- return result.length > 0;
-}
-
-/**
- * Executes a long-running ClickHouse query with timeout and progress monitoring
- * Based on: https://github.com/ClickHouse/clickhouse-js/blob/main/examples/long_running_queries_timeouts.ts#L85
- */
-async function executeLongRunningQuery(
- query: string,
- timeoutMinutes: number,
-): Promise<void> {
- const queryId = randomUUID();
- const client = clickhouseClient();
-
- const abortController = new AbortController();
- const timeoutMs = timeoutMinutes * 60 * 1000;
-
- logger.info(`[Background Migration] Executing backfill query ${queryId}`);
-
- // Start the query execution
- const queryPromise = client.command({
- query,
- query_id: queryId,
- abort_signal: abortController.signal,
- });
-
- const startTime = Date.now();
-
- // Check whether the query was created
- let checkExistTries = 0;
- await new Promise<void>((resolve, reject) => {
- const checkInterval = setInterval(async () => {
- const queryExists = await checkQueryExists(client, queryId);
- if (queryExists) {
- clearInterval(checkInterval);
- resolve();
- }
- // We rather use lots of tries, but a low interval to ensure that this completes quickly.
- // Since the query should be cheap, we're not worried too much about the load.
- if (checkExistTries++ > 60) {
- clearInterval(checkInterval);
- reject(
- new Error(
- `Query ${queryId} does not exist in system.query_log after ${
- checkExistTries * 2
- }s`,
- ),
- );
- }
- }, 2000);
- });
-
- // Cancel the HTTP request and keep it running server-side only.
- abortController.abort();
-
- // Handle the expected abort error when canceling the HTTP request
- try {
- await queryPromise;
- } catch (err) {
- if (err instanceof Error && err.message.includes("abort")) {
- logger.info(
- `[Background Migration] Query ${queryId} HTTP request aborted as expected, query continues server-side`,
- );
- } else {
- throw err;
- }
- }
-
- // Check whether the query completed or aborted after timeoutMin minutes
- await new Promise<void>((resolve, reject) => {
- const checkInterval = setInterval(async () => {
- try {
- const isCompleted = await checkCompletedQuery(client, queryId);
- if (isCompleted) {
- clearInterval(checkInterval);
- resolve();
- }
- } catch (err) {
- clearInterval(checkInterval);
- reject(err);
- }
-
- // If Date.now() - startTime rounded down to a second is a multiple of 60, print a log message, i.e. ~ every minute
- if (Math.floor((Date.now() - startTime) / 1000) % 60 === 0) {
- logger.info(
- `[Background Migration] Query ${queryId} still running after ${Math.floor(
- (Date.now() - startTime) / 60000,
- )} minutes`,
- );
- }
-
- if (Date.now() - startTime > timeoutMs) {
- logger.warn(
- `[Background Migration] Query ${queryId} still running after ${timeoutMinutes} minutes. Aborting...`,
- );
- await client.command({
- query: `KILL QUERY WHERE query_id = '${queryId}'`,
- });
- clearInterval(checkInterval);
- reject(
- new Error(
- `Query ${queryId} cancelled after ${timeoutMinutes} minutes`,
- ),
- );
- }
- }, 1000);
- });
-
- logger.info(`[Background Migration] Query ${queryId} completed successfully`);
-}
-
-export default class MigrateTracesToTracesAMTs implements IBackgroundMigration {
- private isAborted = false;
- private isFinished = false;
-
- async validate(
- args: Record<string, unknown>,
- attempts = 5,
- ): Promise<{ valid: boolean; invalidReason: string | undefined }> {
- // Check if Clickhouse credentials are configured
- if (
- !env.CLICKHOUSE_URL ||
- !env.CLICKHOUSE_USER ||
- !env.CLICKHOUSE_PASSWORD
- ) {
- return {
- valid: false,
- invalidReason:
- "Clickhouse credentials must be configured to perform migration",
- };
- }
-
- // Check if required ClickHouse tables exist
- const tables = await clickhouseClient().query({
- query: "SHOW TABLES",
- });
- const tableNames = (await tables.json()).data as { name: string }[];
- if (
- ["traces_null", "traces_all_amt", "traces_7d_amt", "traces_30_amt"].every(
- (table) => tableNames.some((t) => t.name === table),
- )
- ) {
- // Retry if the table does not exist as this may mean migrations are still pending
- if (attempts > 0) {
- logger.info(
- `[Background Migration] ClickHouse tables do not exist. Expected to find traces_null, traces_all_amt, traces_7d_amt, traces_30_amt. Retrying in 10s...`,
- );
- return new Promise((resolve) => {
- setTimeout(() => resolve(this.validate(args, attempts - 1)), 10_000);
- });
- }
- }
-
- return { valid: true, invalidReason: undefined };
- }
-
- async run(args: Record<string, unknown>): Promise<void> {
- const start = Date.now();
- logger.info(
- `[Background Migration] Migrating traces to traces AMTs with ${JSON.stringify(args)}`,
- );
-
- // @ts-ignore
- const initialMigrationState: { state: MigrationState } =
- await prisma.backgroundMigration.findUniqueOrThrow({
- where: { id: backgroundMigrationId },
- select: { state: true },
- });
-
- // Use values from database state if available, otherwise fall back to args
- 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)
- : new Date((args.maxDate as string) ?? new Date());
- const minDate = initialMigrationState.state?.minDate
- ? new Date(initialMigrationState.state.minDate)
- : new Date((args.minDate as string) ?? new Date("2023-05-18"));
-
- await prisma.backgroundMigration.update({
- where: { id: backgroundMigrationId },
- data: {
- state: {
- maxDate,
- minDate,
- queryTimeoutMinutes,
- targetTracesAllAmtOnly,
- },
- },
- });
-
- while (!this.isAborted && !this.isFinished) {
- const queryStart = Date.now();
-
- // @ts-ignore
- 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 format
- const currentMonth = maxDate.toISOString().slice(0, 7).replace("-", "");
- logger.info(
- `[Background Migration] Migrating traces for ${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);
-
- maxDate.setMonth(maxDate.getMonth() - 1);
- await prisma.backgroundMigration.update({
- where: { id: backgroundMigrationId },
- data: {
- state: {
- ...migrationState.state,
- maxDate,
- },
- },
- });
-
- logger.info(
- `[Background Migration] Inserted traces into ${targetTable} for ${currentMonth} in ${Date.now() - queryStart}ms`,
- );
-
- if (maxDate < minDate) {
- logger.info(
- "[Background Migration] No more traces to migrate. Exiting...",
- );
- this.isFinished = true;
- }
- }
-
- if (this.isAborted) {
- logger.info(
- `[Background Migration] Migration of traces to traces AMTs aborted.`,
- );
- return;
- }
-
- logger.info(
- `[Background Migration] Finished migration of traces to traces AMTs in ${Date.now() - start}ms`,
- );
- }
-
- async abort(): Promise<void> {
- logger.info(
- `[Background Migration] Aborting migration of traces to traces AMTs`,
- );
- this.isAborted = true;
- }
-}
-
-async function createBackgroundMigrationRecord(): Promise<void> {
- logger.info("[Background Migration] Creating background migration record...");
-
- await prisma.backgroundMigration.create({
- data: {
- id: backgroundMigrationId,
- name: "20250704_1356_migrate_traces_to_traces_amt",
- script: "migrateTracesToTracesAMTs",
- args: {},
- },
- });
- logger.info(
- "[Background Migration] Background migration record created successfully",
- );
-}
-
-// TODO: Confirm defaults for maxDate and minDate to ensure coverage for self-hosters
-async function main() {
- const args = parseArgs({
- options: {
- maxDate: {
- type: "string",
- short: "d",
- default: new Date().toISOString(),
- },
- minDate: {
- type: "string",
- short: "m",
- default: new Date("2023-05-18T00:00:00.000Z").toISOString(),
- },
- createRecord: {
- type: "boolean",
- short: "c",
- default: false,
- },
- timeoutMinutes: {
- type: "string",
- short: "t",
- default: "90",
- },
- targetTracesAllAmtOnly: {
- type: "boolean",
- default: false,
- },
- },
- });
-
- if (args.values.createRecord) {
- await createBackgroundMigrationRecord();
- }
-
- // Convert string args to numbers
- const migrationArgs = {
- ...args.values,
- queryTimeoutMinutes: parseInt(args.values.timeoutMinutes!),
- };
-
- const migration = new MigrateTracesToTracesAMTs();
- await migration.validate(migrationArgs);
- await migration.run(migrationArgs);
-}
-
-// If the script is being executed directly (not imported), run the main function
-if (require.main === module) {
- main()
- .then(() => {
- process.exit(0);
- })
- .catch((error) => {
- logger.error(
- `[Background Migration] Migration execution failed: ${error}`,
- error,
- );
- process.exit(1); // Exit with an error code
- });
-}
diff --git a/worker/src/services/IngestionService/README.md b/worker/src/services/IngestionService/README.md
deleted file mode 100644
index 3e22a690b9..0000000000
--- a/worker/src/services/IngestionService/README.md
+++ /dev/null
@@ -1,552 +0,0 @@
-# IngestionService
-
-## Experimental Setup with AggregatingMergeTrees
-
-Warn: Do not apply on your own as this is highly experimental and may change anytime.
-
-To reduce the costs associated with trace processing, we aim to derive trace information from spans eventually.
-As a first step, we created a `traces_mt` table which acted as a proxy for a bunch of AggregatingMergeTrees that collapse
-trace writes into a single row.
-We have now moved to a `traces_null` table that doesn't store intermediate results to save on storage and compute overhead.
-
-We can enable experimental writes to the `traces_null` table by setting `LANGFUSE_EXPERIMENT_INSERT_INTO_AGGREGATING_MERGE_TREES=true`.
-This requires that the following table schema is manually applied on the database instance:
-
-```sql
--- Setup
--- Context: https://fiddle.clickhouse.com/d4e84b88-6bd7-455c-9a84-e9126594f92a
-
-
--- TODO: Make sure to update migrateTracesToTracesAMTs.ts if the traces_null schema changes
-
--- Create a Null table that serves as a trigger for all materialized views.
--- We use a Null engine here to avoid storing intermediate results and save on storage.
-CREATE TABLE traces_null
-(
- -- Identifiers
- `project_id` String,
- `id` String,
- `start_time` DateTime64(3),
- `end_time` Nullable(DateTime64(3)),
- `name` Nullable(String),
-
- -- Metadata properties
- `metadata` Map(LowCardinality(String), String),
- `user_id` Nullable(String),
- `session_id` Nullable(String),
- `environment` String,
- `tags` Array(String),
- `version` Nullable(String),
- `release` Nullable(String),
-
- -- UI properties - We make them nullable to prevent absent values being interpreted as overwrites.
- `bookmarked` Nullable(Bool),
- `public` Nullable(Bool),
-
- -- Aggregations
- `observation_ids` Array(String),
- `score_ids` Array(String),
- `cost_details` Map(String, Decimal64(12)),
- `usage_details` Map(String, UInt64),
- -- TODO: Do we want to aggregate/collect `levels` seen within the trace?
-
- -- Input/Output
- `input` String,
- `output` String,
-
- `created_at` DateTime64(3),
- `updated_at` DateTime64(3),
- `event_ts` DateTime64(3)
-) Engine = Null();
-
--- Create the all AMT
-CREATE TABLE traces_all_amt
-(
- -- Identifiers
- `project_id` String,
- `id` String,
- `timestamp` SimpleAggregateFunction(min, DateTime64(3)), -- Backward compatibility: redundant with start_time
- `start_time` SimpleAggregateFunction(min, DateTime64(3)),
- `end_time` SimpleAggregateFunction(max, Nullable(DateTime64(3))),
- `name` SimpleAggregateFunction(anyLast, Nullable(String)),
-
- -- Metadata properties
- `metadata` SimpleAggregateFunction(maxMap, Map(String, String)),
- `user_id` SimpleAggregateFunction(anyLast, Nullable(String)),
- `session_id` SimpleAggregateFunction(anyLast, Nullable(String)),
- `environment` SimpleAggregateFunction(anyLast, String),
- `tags` SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
- `version` SimpleAggregateFunction(anyLast, Nullable(String)),
- `release` SimpleAggregateFunction(anyLast, Nullable(String)),
-
- -- UI properties
- `bookmarked` AggregateFunction(argMax, Nullable(Bool), DateTime64(3)),
- `public` AggregateFunction(argMax, Nullable(Bool), DateTime64(3)),
-
- -- Aggregations
- `observation_ids` SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
- `score_ids` SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
- `cost_details` SimpleAggregateFunction(sumMap, Map(String, Decimal(38, 12))),
- `usage_details` SimpleAggregateFunction(sumMap, Map(String, UInt64)),
-
- -- Input/Output -> prefer correctness via argMax
- `input` AggregateFunction(argMax, String, DateTime64(3)) CODEC (ZSTD(3)),
- `output` AggregateFunction(argMax, String, DateTime64(3)) CODEC (ZSTD(3)),
-
- `created_at` SimpleAggregateFunction(min, DateTime64(3)),
- `updated_at` SimpleAggregateFunction(max, DateTime64(3)),
-
- -- Indexes
- INDEX idx_trace_id id TYPE bloom_filter(0.001) GRANULARITY 1,
- INDEX idx_user_id user_id TYPE bloom_filter(0.001) GRANULARITY 1,
- INDEX idx_session_id session_id TYPE bloom_filter(0.001) GRANULARITY 1,
- INDEX idx_name name TYPE bloom_filter(0.001) GRANULARITY 1,
- INDEX idx_version version TYPE bloom_filter(0.001) GRANULARITY 1,
- INDEX idx_release release TYPE bloom_filter(0.001) GRANULARITY 1,
- INDEX idx_tags tags TYPE bloom_filter(0.001) GRANULARITY 1
-) Engine = AggregatingMergeTree()
- ORDER BY (project_id, id);
-
--- Create materialized view for all_amt
--- Create the materialized view for unclustered deployment:
-CREATE MATERIALIZED VIEW IF NOT EXISTS traces_all_amt_mv TO traces_all_amt AS
-SELECT
- -- Identifiers
- tn.project_id as project_id,
- tn.id as id,
- min(tn.start_time) as timestamp, -- Backward compatibility: redundant with start_time
- min(tn.start_time) as start_time,
- max(coalesce(tn.end_time, tn.start_time)) as end_time,
- anyLast(tn.name) as name,
-
- -- Metadata properties
- maxMap(tn.metadata) as metadata,
- anyLast(tn.user_id) as user_id,
- anyLast(tn.session_id) as session_id,
- anyLast(tn.environment) as environment,
- groupUniqArrayArray(tn.tags) as tags,
- anyLast(tn.version) as version,
- anyLast(tn.release) as release,
-
- -- UI properties
- argMaxState(tn.bookmarked, if(tn.bookmarked is not null, tn.event_ts, toDateTime64(0, 3))) as bookmarked,
- argMaxState(tn.public, if(tn.public is not null, tn.event_ts, toDateTime64(0, 3))) as public,
-
- -- Aggregations
- groupUniqArrayArray(tn.observation_ids) as observation_ids,
- groupUniqArrayArray(tn.score_ids) as score_ids,
- sumMap(tn.cost_details) as cost_details,
- sumMap(tn.usage_details) as usage_details,
-
- -- Input/Output
- argMaxState(tn.input, if(tn.input <> '', tn.event_ts, toDateTime64(0, 3))) as input,
- argMaxState(tn.output, if(tn.output <> '', tn.event_ts, toDateTime64(0, 3))) as output,
-
- min(tn.created_at) as created_at,
- max(tn.updated_at) as updated_at
-FROM traces_null tn
-GROUP BY project_id, id;
-
--- Create the 7-day TTL AMT
-CREATE TABLE traces_7d_amt
-(
- -- Identifiers
- `project_id` String,
- `id` String,
- `timestamp` SimpleAggregateFunction(min, DateTime64(3)), -- Backward compatibility: redundant with start_time
- `start_time` SimpleAggregateFunction(min, DateTime64(3)),
- `end_time` SimpleAggregateFunction(max, Nullable(DateTime64(3))),
- `name` SimpleAggregateFunction(anyLast, Nullable(String)),
-
- -- Metadata properties
- `metadata` SimpleAggregateFunction(maxMap, Map(String, String)),
- `user_id` SimpleAggregateFunction(anyLast, Nullable(String)),
- `session_id` SimpleAggregateFunction(anyLast, Nullable(String)),
- `environment` SimpleAggregateFunction(anyLast, String),
- `tags` SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
- `version` SimpleAggregateFunction(anyLast, Nullable(String)),
- `release` SimpleAggregateFunction(anyLast, Nullable(String)),
-
- -- UI properties
- `bookmarked` AggregateFunction(argMax, Nullable(Bool), DateTime64(3)),
- `public` AggregateFunction(argMax, Nullable(Bool), DateTime64(3)),
-
- -- Aggregations
- `observation_ids` SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
- `score_ids` SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
- `cost_details` SimpleAggregateFunction(sumMap, Map(String, Decimal(38, 12))),
- `usage_details` SimpleAggregateFunction(sumMap, Map(String, UInt64)),
-
- -- Input/Output -> prefer correctness via argMax
- `input` AggregateFunction(argMax, String, DateTime64(3)) CODEC (ZSTD(3)),
- `output` AggregateFunction(argMax, String, DateTime64(3)) CODEC (ZSTD(3)),
-
- `created_at` SimpleAggregateFunction(min, DateTime64(3)),
- `updated_at` SimpleAggregateFunction(max, DateTime64(3)),
-
- -- Indexes
- INDEX idx_user_id user_id TYPE bloom_filter(0.001) GRANULARITY 1,
- INDEX idx_session_id session_id TYPE bloom_filter(0.001) GRANULARITY 1,
- INDEX idx_name name TYPE bloom_filter(0.001) GRANULARITY 1,
- INDEX idx_version version TYPE bloom_filter(0.001) GRANULARITY 1,
- INDEX idx_release release TYPE bloom_filter(0.001) GRANULARITY 1,
- INDEX idx_tags tags TYPE bloom_filter(0.001) GRANULARITY 1
-) Engine = AggregatingMergeTree()
- ORDER BY (project_id, id)
- TTL toDate(start_time) + INTERVAL 7 DAY;
-
--- Create materialized view for 7d_amt
-CREATE MATERIALIZED VIEW IF NOT EXISTS traces_7d_amt_mv TO traces_7d_amt AS
-SELECT
- -- Identifiers
- tn.project_id as project_id,
- tn.id as id,
- min(tn.start_time) as timestamp, -- Backward compatibility: redundant with start_time
- min(tn.start_time) as start_time,
- max(coalesce(tn.end_time, tn.start_time)) as end_time,
- anyLast(tn.name) as name,
-
- -- Metadata properties
- maxMap(tn.metadata) as metadata,
- anyLast(tn.user_id) as user_id,
- anyLast(tn.session_id) as session_id,
- anyLast(tn.environment) as environment,
- groupUniqArrayArray(tn.tags) as tags,
- anyLast(tn.version) as version,
- anyLast(tn.release) as release,
-
- -- UI properties
- argMaxState(tn.bookmarked, if(tn.bookmarked is not null, tn.event_ts, toDateTime64(0, 3))) as bookmarked,
- argMaxState(tn.public, if(tn.public is not null, tn.event_ts, toDateTime64(0, 3))) as public,
-
- -- Aggregations
- groupUniqArrayArray(tn.observation_ids) as observation_ids,
- groupUniqArrayArray(tn.score_ids) as score_ids,
- sumMap(tn.cost_details) as cost_details,
- sumMap(tn.usage_details) as usage_details,
-
- -- Input/Output
- argMaxState(tn.input, if(tn.input <> '', tn.event_ts, toDateTime64(0, 3))) as input,
- argMaxState(tn.output, if(tn.output <> '', tn.event_ts, toDateTime64(0, 3))) as output,
-
- min(tn.created_at) as created_at,
- max(tn.updated_at) as updated_at
-FROM traces_null tn
-GROUP BY project_id, id;
-
--- Create the 30-day TTL AMT
-CREATE TABLE traces_30d_amt
-(
- -- Identifiers
- `project_id` String,
- `id` String,
- `timestamp` SimpleAggregateFunction(min, DateTime64(3)), -- Backward compatibility: redundant with start_time
- `start_time` SimpleAggregateFunction(min, DateTime64(3)),
- `end_time` SimpleAggregateFunction(max, Nullable(DateTime64(3))),
- `name` SimpleAggregateFunction(anyLast, Nullable(String)),
-
- -- Metadata properties
- `metadata` SimpleAggregateFunction(maxMap, Map(String, String)),
- `user_id` SimpleAggregateFunction(anyLast, Nullable(String)),
- `session_id` SimpleAggregateFunction(anyLast, Nullable(String)),
- `environment` SimpleAggregateFunction(anyLast, String),
- `tags` SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
- `version` SimpleAggregateFunction(anyLast, Nullable(String)),
- `release` SimpleAggregateFunction(anyLast, Nullable(String)),
-
- -- UI properties
- `bookmarked` AggregateFunction(argMax, Nullable(Bool), DateTime64(3)),
- `public` AggregateFunction(argMax, Nullable(Bool), DateTime64(3)),
-
- -- Aggregations
- `observation_ids` SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
- `score_ids` SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
- `cost_details` SimpleAggregateFunction(sumMap, Map(String, Decimal(38, 12))),
- `usage_details` SimpleAggregateFunction(sumMap, Map(String, UInt64)),
-
- -- Input/Output -> prefer correctness via argMax
- `input` AggregateFunction(argMax, String, DateTime64(3)) CODEC (ZSTD(3)),
- `output` AggregateFunction(argMax, String, DateTime64(3)) CODEC (ZSTD(3)),
-
- `created_at` SimpleAggregateFunction(min, DateTime64(3)),
- `updated_at` SimpleAggregateFunction(max, DateTime64(3)),
-
- -- Indexes
- INDEX idx_user_id user_id TYPE bloom_filter(0.001) GRANULARITY 1,
- INDEX idx_session_id session_id TYPE bloom_filter(0.001) GRANULARITY 1,
- INDEX idx_name name TYPE bloom_filter(0.001) GRANULARITY 1,
- INDEX idx_version version TYPE bloom_filter(0.001) GRANULARITY 1,
- INDEX idx_release release TYPE bloom_filter(0.001) GRANULARITY 1,
- INDEX idx_tags tags TYPE bloom_filter(0.001) GRANULARITY 1
-) Engine = AggregatingMergeTree()
- ORDER BY (project_id, id)
- TTL toDate(start_time) + INTERVAL 30 DAY;
-
--- Create materialized view for 30d_amt
-CREATE MATERIALIZED VIEW IF NOT EXISTS traces_30d_amt_mv TO traces_30d_amt AS
-SELECT
- -- Identifiers
- tn.project_id as project_id,
- tn.id as id,
- min(tn.start_time) as timestamp, -- Backward compatibility: redundant with start_time
- min(tn.start_time) as start_time,
- max(coalesce(tn.end_time, tn.start_time)) as end_time,
- anyLast(tn.name) as name,
-
- -- Metadata properties
- maxMap(tn.metadata) as metadata,
- anyLast(tn.user_id) as user_id,
- anyLast(tn.session_id) as session_id,
- anyLast(tn.environment) as environment,
- groupUniqArrayArray(tn.tags) as tags,
- anyLast(tn.version) as version,
- anyLast(tn.release) as release,
-
- -- UI properties
- argMaxState(tn.bookmarked, if(tn.bookmarked is not null, tn.event_ts, toDateTime64(0, 3))) as bookmarked,
- argMaxState(tn.public, if(tn.public is not null, tn.event_ts, toDateTime64(0, 3))) as public,
-
- -- Aggregations
- groupUniqArrayArray(tn.observation_ids) as observation_ids,
- groupUniqArrayArray(tn.score_ids) as score_ids,
- sumMap(tn.cost_details) as cost_details,
- sumMap(tn.usage_details) as usage_details,
-
- -- Input/Output
- argMaxState(tn.input, if(tn.input <> '', tn.event_ts, toDateTime64(0, 3))) as input,
- argMaxState(tn.output, if(tn.output <> '', tn.event_ts, toDateTime64(0, 3))) as output,
-
- min(tn.created_at) as created_at,
- max(tn.updated_at) as updated_at
-FROM traces_null tn
-GROUP BY project_id, id;
-```
-
-## Query AggregatingMergeTrees
-
-We can query the properties of the resulting AggregatingMergeTree via (make sure to pick the right timeframe:
-
-```sql
-SELECT
- -- Identifiers
- project_id,
- id,
- start_time,
- end_time,
- name,
-
- -- Metadata properties
- metadata,
- user_id,
- session_id,
- environment,
- tags,
- version,
- release,
-
- -- UI properties
- finalizeAggregation(bookmarked) AS bookmarked_value,
- finalizeAggregation(public) AS public_value,
-
- -- Aggregations
- observation_ids,
- score_ids,
- cost_details,
- usage_details,
-
- -- Input/Output
- finalizeAggregation(input) AS input_value,
- finalizeAggregation(output) AS output_value,
-
- created_at,
- updated_at
-FROM traces_all_amt
-LIMIT 100;
-```
-
-## Find discrepancies
-
-We can identify discrepancies in the original and the new data using
-
-```sql
--- Query to compare traces_all_amt with traces table and identify discrepancies
-WITH amt_data AS (
--- First get the finalized values from the AMT table
- SELECT project_id,
- id,
- start_time,
- end_time,
- name,
-
- metadata,
- user_id,
- session_id,
- environment,
- tags,
- version,
- release,
-
- finalizeAggregation(bookmarked) AS bookmarked_value,
- finalizeAggregation(public) AS public_value,
-
- finalizeAggregation(input) AS input_value,
- finalizeAggregation(output) AS output_value,
-
- created_at,
- updated_at
- FROM traces_all_amt
-)
-
--- Main query to compare AMT with original traces table
-SELECT t.project_id,
- t.id,
- -- Null-safe difference detection
- NOT (t.timestamp = a.start_time OR (t.timestamp IS NULL AND a.start_time IS NULL)) AS timestamp_diff,
- NOT (t.name = a.name OR (t.name IS NULL AND a.name IS NULL)) AS name_diff,
- NOT (t.user_id = a.user_id OR (t.user_id IS NULL AND a.user_id IS NULL)) AS user_id_diff,
- NOT (t.session_id = a.session_id OR (t.session_id IS NULL AND a.session_id IS NULL)) AS session_id_diff,
- NOT (t.release = a.release OR (t.release IS NULL AND a.release IS NULL)) AS release_diff,
- NOT (t.version = a.version OR (t.version IS NULL AND a.version IS NULL)) AS version_diff,
- NOT (t.public = a.public_value OR (t.public IS NULL AND a.public_value IS NULL)) AS public_diff,
- NOT (t.bookmarked = a.bookmarked_value OR (t.bookmarked IS NULL AND a.bookmarked_value IS NULL)) AS bookmarked_diff,
- NOT (arraySort(t.tags) = arraySort(a.tags) OR (t.tags IS NULL AND a.tags IS NULL)) AS tags_diff,
- NOT (t.input = a.input_value OR (t.input IS NULL AND a.input_value IS NULL)) AS input_diff,
- NOT (t.output = a.output_value OR (t.output IS NULL AND a.output_value IS NULL)) AS output_diff,
- -- Order-independent metadata comparison
- NOT (
- -- Check if both are null
- (t.metadata IS NULL AND a.metadata IS NULL) OR
- -- Check if both have same keys and values
- (t.metadata IS NOT NULL AND a.metadata IS NOT NULL AND
- arraySort(mapKeys(t.metadata)) = arraySort(mapKeys(a.metadata)) AND
- arrayAll(k -> t.metadata[k] = a.metadata[k], mapKeys(t.metadata)))
- ) AS metadata_diff,
- NOT (t.environment = a.environment OR (t.environment IS NULL AND a.environment IS NULL)) AS environment_diff,
-
- -- Include original values for comparison
- t.timestamp AS traces_timestamp,
- a.start_time AS amt_start_time,
- t.name AS traces_name,
- a.name AS amt_name,
- t.user_id AS traces_user_id,
- a.user_id AS amt_user_id,
- t.session_id AS traces_session_id,
- a.session_id AS amt_session_id,
- t.environment AS traces_environment,
- a.environment AS amt_environment,
-
- t.metadata AS traces_metadata,
- a.metadata AS amt_metadata,
- arraySort(t.tags) AS traces_tags,
- arraySort(a.tags) AS amt_tags,
- t.bookmarked AS traces_bookmarked,
- a.bookmarked_value AS amt_bookmarked,
- t.public AS traces_public,
- a.public_value AS amt_public,
- t.release AS traces_release,
- a.release AS amt_release,
- t.version AS traces_version,
- a.version AS amt_version,
-
- t.input AS traces_input,
- a.input_value AS amt_input,
- t.output AS traces_output,
- a.output_value AS amt_output,
-
- timestamp_diff +
- name_diff +
- user_id_diff +
- session_id_diff +
- release_diff +
- version_diff +
- public_diff +
- bookmarked_diff +
- tags_diff +
- input_diff +
- output_diff +
- metadata_diff +
- environment_diff
- AS total_diff
-
-FROM traces t FINAL
-LEFT JOIN amt_data a ON t.project_id = a.project_id AND t.id = a.id
-WHERE (t.timestamp >= '2025-07-01' OR a.start_time >= '2025-07-01')
-AND t.project_id IN (
- 'some-project-id'
-)
-ORDER BY total_diff DESC
-LIMIT 1000;
-```
-
-## Traces Table Access Pattern Checklist
-
-This checklist documents all references and invocations to the `traces` table grouped by their access pattern. Use this as a baseline to perform transformations like the one in `@/packages/shared/src/server/repositories/traces.ts` with the `measureAndReturn` utility.
-
-### 1. Single Record Lookups (by ID)
-
-- [ ] **IngestionService.getClickhouseRecord()** - `worker/src/services/IngestionService/index.ts:1047-1065`
- - Can probably be skipped as read for updates won't be a thing in the new flow.
-- [x] **getTraceById()** - `packages/shared/src/server/repositories/traces.ts:443-486`
-- [x] **getTracesByIds()** - `packages/shared/src/server/repositories/traces.ts:233-264`
-
-### 2. Session-Based Queries
-
-- [x] **getTracesBySessionId()** - `packages/shared/src/server/repositories/traces.ts:266-304`
-- [x] **getTracesIdentifierForSession()** - `packages/shared/src/server/repositories/traces.ts:642-688`
-
-### 3. Existence Checks
-
-- [x] **checkTraceExistsAndGetTimestamp()** - `packages/shared/src/server/repositories/traces.ts:73-210`
-- [x] **hasAnyTrace()** - `packages/shared/src/server/repositories/traces.ts:306-356`
-- [x] **hasAnyUser()** - `packages/shared/src/server/repositories/traces.ts:763-787`
-
-### 4. Aggregation and Analytics Queries
-
-- [x] **getTracesCountForPublicApi()** - `web/src/features/public-api/server/traces.ts:299`
-- [x] **generateDailyMetrics()** - `web/src/features/public-api/server/dailyMetrics.ts:93`
-- [x] **getDailyMetricsCount()** - `web/src/features/public-api/server/dailyMetrics.ts:153`
-- [x] **generateObservationsForPublicApi()** - `web/src/features/public-api/server/observations.ts:80`
-- [x] **getObservationsCountForPublicApi()** - `web/src/features/public-api/server/observations.ts:108`
-- [x] **getObservationsTableInternal()** - `packages/shared/src/server/repositories/observations.ts:565`
-- [x] **\_handleGenerateScoresForPublicApi()** - `web/src/features/public-api/server/scores.ts:101`
-- [x] **\_handleGetScoresCountForPublicApi()** - `web/src/features/public-api/server/scores.ts:181`
-- [x] **getScoresUiGeneric()** - `packages/shared/src/server/repositories/scores.ts:825`
-- [x] **getNumericScoreHistogram()** - `packages/shared/src/server/repositories/scores.ts:1074`
-- [x] **getTracesGroupedByName()** - `packages/shared/src/server/repositories/traces.ts:489-535`
-- [x] **getTracesGroupedByUsers()** - `packages/shared/src/server/repositories/traces.ts:537-597`
-- [x] **getTracesGroupedByTags()** - `packages/shared/src/server/repositories/traces.ts:605-640`
-- [x] **getTotalUserCount()** - `packages/shared/src/server/repositories/traces.ts:789-827`
-- [x] **getUserMetrics()** - `packages/shared/src/server/repositories/traces.ts:829-978`
-- [x] **getTracesTableGeneric()** - `packages/shared/src/server/services/traces-ui-table-service.ts:207++`
-- [x] **getSessionsTableGeneric()** - `packages/shared/src/server/services/sessions-ui-table-service.ts:121++`)
-- [x] **generateTracesForPublicApi()** - `web/src/features/public-api/server/traces.ts:36++`
-
-### 5. Data Export and Migration
-
-Note: The measureAndReturn utility does not handle query streams well as it promisifies everything.
-We need to cover these queries manually and cannot run a comparison.
-We could use an opt-in on a projectId basis.
-
-- [x] **getTracesForPostHog()** - `packages/shared/src/server/repositories/traces.ts:1026-1113`
-- [x] **getScoresForPostHog()** - `packages/shared/src/server/repositories/scores.ts:1328`
-- [x] **getGenerationsForPosthog()** - `packages/shared/src/server/repositories/observations.ts:1481`
-- [x] **getTracesForBlobStorageExport()** - `packages/shared/src/server/repositories/traces.ts:980-1024`
-
-### 6. Count and Statistics Queries
-
-- [x] **getTraceCountsByProjectInCreationInterval()** - `packages/shared/src/server/repositories/traces.ts:358-392`
-- [x] **getTraceCountOfProjectsSinceCreationDate()** - `packages/shared/src/server/repositories/traces.ts:394-423`
-
-### 7. Cross-Project Queries
-
-- [x] **getTracesByIdsForAnyProject()** - `packages/shared/src/server/repositories/traces.ts:1115-1141`
-
-### 8. Delete Operations
-
-- [x] **deleteTraces()** - `packages/shared/src/server/repositories/traces.ts:790++`
-- [x] **deleteTracesOlderThanDays()** - `packages/shared/src/server/repositories/traces.ts:814++`
-- [x] **deleteTracesByProjectId()** - `packages/shared/src/server/repositories/traces.ts:841++`
-
-### 9. Writes
-
-- [x] **upsertTrace()** - `packages/shared/src/server/repositories/traces.ts:224`