Skip to content

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

exp: add aggregating merge trees experiment for traces tables (#7574)

Steffen Schmitz98cc1bb7a175
diff --git a/packages/shared/src/server/repositories/definitions.ts b/packages/shared/src/server/repositories/definitions.ts
index 66322ec844..fe28136dfd 100644
--- a/packages/shared/src/server/repositories/definitions.ts
+++ b/packages/shared/src/server/repositories/definitions.ts
@@ -122,6 +122,43 @@ export const traceRecordInsertSchema = traceRecordBaseSchema.extend({
});
export type TraceRecordInsertType = z.infer<typeof traceRecordInsertSchema>;
+export const traceMtRecordInsertSchema = z.object({
+ // Identifiers
+ project_id: z.string(),
+ id: z.string(),
+ start_time: z.number(),
+ end_time: z.number().nullish(),
+ name: z.string(),
+
+ // Metadata properties
+ metadata: z.record(z.string(), z.string()),
+ user_id: z.string(),
+ session_id: z.string(),
+ environment: z.string(),
+ tags: z.array(z.string()),
+ version: z.string().nullish(),
+ release: z.string().nullish(),
+
+ // UI properties - nullable to prevent absent values being interpreted as overwrites
+ bookmarked: z.boolean().nullish(),
+ public: z.boolean().nullish(),
+
+ // Aggregations
+ observation_ids: z.array(z.string()),
+ score_ids: z.array(z.string()),
+ cost_details: z.record(z.string(), z.number()),
+ usage_details: z.record(z.string(), z.number()),
+
+ // Input/Output
+ input: z.string(),
+ output: z.string(),
+
+ created_at: z.number(),
+ updated_at: z.number(),
+ event_ts: z.number(),
+});
+export type TraceMtRecordInsertType = z.infer<typeof traceMtRecordInsertSchema>;
+
export const scoreRecordBaseSchema = z.object({
id: z.string(),
project_id: z.string(),
diff --git a/worker/src/env.ts b/worker/src/env.ts
index aec93bf167..8275fdd1ef 100644
--- a/worker/src/env.ts
+++ b/worker/src/env.ts
@@ -225,6 +225,10 @@ const EnvSchema = z.object({
.number()
.positive()
.default(120_000), // 2 minutes
+
+ LANGFUSE_EXPERIMENT_INSERT_INTO_AGGREGATING_MERGE_TREES: z
+ .enum(["true", "false"])
+ .default("false"),
});
export const env: z.infer<typeof EnvSchema> =
diff --git a/worker/src/services/ClickhouseWriter/index.ts b/worker/src/services/ClickhouseWriter/index.ts
index c374680b28..f54c2afddb 100644
--- a/worker/src/services/ClickhouseWriter/index.ts
+++ b/worker/src/services/ClickhouseWriter/index.ts
@@ -9,6 +9,7 @@ import {
recordIncrement,
ScoreRecordInsertType,
TraceRecordInsertType,
+ TraceMtRecordInsertType,
} from "@langfuse/shared/src/server";
import { env } from "../../env";
@@ -36,6 +37,7 @@ export class ClickhouseWriter {
this.queue = {
[TableName.Traces]: [],
+ [TableName.TracesMt]: [],
[TableName.Scores]: [],
[TableName.Observations]: [],
[TableName.BlobStorageFileLog]: [],
@@ -101,6 +103,7 @@ export class ClickhouseWriter {
recordIncrement("langfuse.queue.clickhouse_writer.request");
await Promise.all([
this.flush(TableName.Traces, fullQueue),
+ this.flush(TableName.TracesMt, fullQueue),
this.flush(TableName.Scores, fullQueue),
this.flush(TableName.Observations, fullQueue),
this.flush(TableName.BlobStorageFileLog, fullQueue),
@@ -237,6 +240,7 @@ export class ClickhouseWriter {
export enum TableName {
Traces = "traces",
+ TracesMt = "traces_mt",
Scores = "scores",
Observations = "observations",
BlobStorageFileLog = "blob_storage_file_log",
@@ -248,9 +252,11 @@ type RecordInsertType<T extends TableName> = T extends TableName.Scores
? ObservationRecordInsertType
: T extends TableName.Traces
? TraceRecordInsertType
- : T extends TableName.BlobStorageFileLog
- ? BlobStorageFileLogInsertType
- : never;
+ : T extends TableName.TracesMt
+ ? TraceMtRecordInsertType
+ : T extends TableName.BlobStorageFileLog
+ ? BlobStorageFileLogInsertType
+ : never;
type ClickhouseQueue = {
[T in TableName]: ClickhouseWriterQueueItem<T>[];
diff --git a/worker/src/services/IngestionService/README.md b/worker/src/services/IngestionService/README.md
new file mode 100644
index 0000000000..9937be5508
--- /dev/null
+++ b/worker/src/services/IngestionService/README.md
@@ -0,0 +1,388 @@
+# 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 create a new `traces_mt` table which acts as a proxy for a bunch of AggregatingMergeTrees that collapse
+trace writes into a single row.
+Eventually, we may move the `traces_mt` table to a `traces_null` table that doesn't store intermediate results.
+
+We can enable experimental writes to the `traces_mt` 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
+
+-- Create a MergeTree table that serves as a trigger for all others.
+-- We use a MergeTree here to track what was being inserted. Could be replaced
+-- with a NULL table in future to save on storage.
+CREATE TABLE traces_mt
+(
+ -- Identifiers
+ `project_id` String,
+ `id` String,
+ `start_time` DateTime64(3),
+ `end_time` Nullable(DateTime64(3)),
+ `name` String,
+
+ -- Metadata properties
+ `metadata` Map(LowCardinality(String), String),
+ `user_id` String,
+ `session_id` 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 = MergeTree()
+ ORDER BY (project_id, id)
+ PARTITION BY toYYYYMM(start_time);
+
+-- Create the all AMT
+CREATE TABLE traces_all_amt
+(
+ -- Identifiers
+ `project_id` String,
+ `id` String,
+ `start_time` SimpleAggregateFunction(min, DateTime64(3)),
+ `end_time` SimpleAggregateFunction(max, Nullable(DateTime64(3))),
+ `name_argmax` AggregateFunction(argMax, String, DateTime64(3)),
+ `name` SimpleAggregateFunction(anyLast, String),
+
+ -- Metadata properties
+ `metadata` AggregateFunction(argMax, Map(LowCardinality(String), String), DateTime64(3)),
+ `user_id_argmax` AggregateFunction(argMax, String, DateTime64(3)),
+ `session_id_argmax` AggregateFunction(argMax, String, DateTime64(3)),
+ `environment_argmax` AggregateFunction(argMax, String, DateTime64(3)),
+ `user_id` SimpleAggregateFunction(anyLast, String),
+ `session_id` SimpleAggregateFunction(anyLast, String),
+ `environment` SimpleAggregateFunction(anyLast, String),
+ `tags` SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
+ `version` AggregateFunction(argMax, Nullable(String), DateTime64(3)),
+ `release` AggregateFunction(argMax, Nullable(String), DateTime64(3)),
+
+ -- 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
+ `input_argmax` AggregateFunction(argMax, String, DateTime64(3)) CODEC (ZSTD(3)),
+ `output_argmax` AggregateFunction(argMax, String, DateTime64(3)) CODEC (ZSTD(3)),
+ `input` SimpleAggregateFunction(anyLast, String),
+ `output` SimpleAggregateFunction(anyLast, String),
+
+ `created_at` SimpleAggregateFunction(min, DateTime64(3)),
+ `updated_at` SimpleAggregateFunction(max, DateTime64(3)),
+
+ -- Indexes
+ INDEX idx_environment environment 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
+) 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
+ t0.project_id as project_id,
+ t0.id as id,
+ min(t0.start_time) as start_time,
+ max(coalesce(t0.end_time, t0.start_time)) as end_time,
+ argMaxState(t0.name, if(t0.name <> '', t0.event_ts, toDateTime64(0, 3))) as name_argmax,
+ anyLast(t0.name) as name,
+
+ -- Metadata properties
+ argMaxState(t0.metadata, t0.event_ts) as metadata,
+ argMaxState(t0.user_id, if(t0.user_id <> '', t0.event_ts, toDateTime64(0, 3))) as user_id_argmax,
+ argMaxState(t0.session_id, if(t0.session_id <> '', t0.event_ts, toDateTime64(0, 3))) as session_id_argmax,
+ argMaxState(t0.environment, if(t0.environment <> '', t0.event_ts, toDateTime64(0, 3))) as environment_argmax,
+ anyLast(t0.user_id) as user_id,
+ anyLast(t0.session_id) as session_id,
+ anyLast(t0.environment) as environment,
+ groupUniqArrayArray(t0.tags) as tags,
+ argMaxState(t0.version, if(t0.version <> '', t0.event_ts, toDateTime64(0, 3))) as version,
+ argMaxState(t0.release, if(t0.release <> '', t0.event_ts, toDateTime64(0, 3))) as release,
+
+ -- UI properties
+ argMaxState(t0.bookmarked, if(t0.bookmarked is not null, t0.event_ts, toDateTime64(0, 3))) as bookmarked,
+ argMaxState(t0.public, if(t0.public is not null, t0.event_ts, toDateTime64(0, 3))) as public,
+
+ -- Aggregations
+ groupUniqArrayArray(t0.observation_ids) as observation_ids,
+ groupUniqArrayArray(t0.score_ids) as score_ids,
+ sumMap(t0.cost_details) as cost_details,
+ sumMap(t0.usage_details) as usage_details,
+
+ -- Input/Output
+ argMaxState(t0.input, if(t0.input <> '', t0.event_ts, toDateTime64(0, 3))) as input_argmax,
+ argMaxState(t0.output, if(t0.output <> '', t0.event_ts, toDateTime64(0, 3))) as output_argmax,
+ anyLast(input) as input,
+ anyLast(output) as output,
+
+ min(t0.created_at) as created_at,
+ max(t0.updated_at) as updated_at
+FROM traces_mt t0
+GROUP BY project_id, id;
+
+-- Create the 7-day TTL AMT
+CREATE TABLE traces_7d_amt
+(
+ -- Identifiers
+ `project_id` String,
+ `id` String,
+ `start_time` SimpleAggregateFunction(min, DateTime64(3)),
+ `end_time` SimpleAggregateFunction(max, Nullable(DateTime64(3))),
+ `name_argmax` AggregateFunction(argMax, String, DateTime64(3)),
+ `name` SimpleAggregateFunction(anyLast, String),
+
+ -- Metadata properties
+ `metadata` AggregateFunction(argMax, Map(LowCardinality(String), String), DateTime64(3)),
+ `user_id_argmax` AggregateFunction(argMax, String, DateTime64(3)),
+ `session_id_argmax` AggregateFunction(argMax, String, DateTime64(3)),
+ `environment_argmax` AggregateFunction(argMax, String, DateTime64(3)),
+ `user_id` SimpleAggregateFunction(anyLast, String),
+ `session_id` SimpleAggregateFunction(anyLast, String),
+ `environment` SimpleAggregateFunction(anyLast, String),
+ `tags` SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
+ `version` AggregateFunction(argMax, Nullable(String), DateTime64(3)),
+ `release` AggregateFunction(argMax, Nullable(String), DateTime64(3)),
+
+ -- 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
+ `input_argmax` AggregateFunction(argMax, String, DateTime64(3)) CODEC (ZSTD(3)),
+ `output_argmax` AggregateFunction(argMax, String, DateTime64(3)) CODEC (ZSTD(3)),
+ `input` SimpleAggregateFunction(anyLast, String),
+ `output` SimpleAggregateFunction(anyLast, String),
+
+ `created_at` SimpleAggregateFunction(min, DateTime64(3)),
+ `updated_at` SimpleAggregateFunction(max, DateTime64(3)),
+
+ -- Indexes
+ INDEX idx_environment environment 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
+) 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
+ t0.project_id as project_id,
+ t0.id as id,
+ min(t0.start_time) as start_time,
+ max(coalesce(t0.end_time, t0.start_time)) as end_time,
+ argMaxState(t0.name, if(t0.name <> '', t0.event_ts, toDateTime64(0, 3))) as name_argmax,
+ anyLast(t0.name) as name,
+
+ -- Metadata properties
+ argMaxState(t0.metadata, t0.event_ts) as metadata,
+ argMaxState(t0.user_id, if(t0.user_id <> '', t0.event_ts, toDateTime64(0, 3))) as user_id_argmax,
+ argMaxState(t0.session_id, if(t0.session_id <> '', t0.event_ts, toDateTime64(0, 3))) as session_id_argmax,
+ argMaxState(t0.environment, if(t0.environment <> '', t0.event_ts, toDateTime64(0, 3))) as environment_argmax,
+ anyLast(t0.user_id) as user_id,
+ anyLast(t0.session_id) as session_id,
+ anyLast(t0.environment) as environment,
+ groupUniqArrayArray(t0.tags) as tags,
+ argMaxState(t0.version, if(t0.version <> '', t0.event_ts, toDateTime64(0, 3))) as version,
+ argMaxState(t0.release, if(t0.release <> '', t0.event_ts, toDateTime64(0, 3))) as release,
+
+ -- UI properties
+ argMaxState(t0.bookmarked, if(t0.bookmarked is not null, t0.event_ts, toDateTime64(0, 3))) as bookmarked,
+ argMaxState(t0.public, if(t0.public is not null, t0.event_ts, toDateTime64(0, 3))) as public,
+
+ -- Aggregations
+ groupUniqArrayArray(t0.observation_ids) as observation_ids,
+ groupUniqArrayArray(t0.score_ids) as score_ids,
+ sumMap(t0.cost_details) as cost_details,
+ sumMap(t0.usage_details) as usage_details,
+
+ -- Input/Output
+ argMaxState(t0.input, if(t0.input <> '', t0.event_ts, toDateTime64(0, 3))) as input_argmax,
+ argMaxState(t0.output, if(t0.output <> '', t0.event_ts, toDateTime64(0, 3))) as output_argmax,
+ anyLast(input) as input,
+ anyLast(output) as output,
+
+ min(t0.created_at) as created_at,
+ max(t0.updated_at) as updated_at
+FROM traces_mt t0
+GROUP BY project_id, id;
+
+-- Create the 30-day TTL AMT
+CREATE TABLE traces_30d_amt
+(
+ -- Identifiers
+ `project_id` String,
+ `id` String,
+ `start_time` SimpleAggregateFunction(min, DateTime64(3)),
+ `end_time` SimpleAggregateFunction(max, Nullable(DateTime64(3))),
+ `name_argmax` AggregateFunction(argMax, String, DateTime64(3)),
+ `name` SimpleAggregateFunction(anyLast, String),
+
+ -- Metadata properties
+ `metadata` AggregateFunction(argMax, Map(LowCardinality(String), String), DateTime64(3)),
+ `user_id_argmax` AggregateFunction(argMax, String, DateTime64(3)),
+ `session_id_argmax` AggregateFunction(argMax, String, DateTime64(3)),
+ `environment_argmax` AggregateFunction(argMax, String, DateTime64(3)),
+ `user_id` SimpleAggregateFunction(anyLast, String),
+ `session_id` SimpleAggregateFunction(anyLast, String),
+ `environment` SimpleAggregateFunction(anyLast, String),
+ `tags` SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
+ `version` AggregateFunction(argMax, Nullable(String), DateTime64(3)),
+ `release` AggregateFunction(argMax, Nullable(String), DateTime64(3)),
+
+ -- 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
+ `input_argmax` AggregateFunction(argMax, String, DateTime64(3)) CODEC (ZSTD(3)),
+ `output_argmax` AggregateFunction(argMax, String, DateTime64(3)) CODEC (ZSTD(3)),
+ `input` SimpleAggregateFunction(anyLast, String),
+ `output` SimpleAggregateFunction(anyLast, String),
+
+ `created_at` SimpleAggregateFunction(min, DateTime64(3)),
+ `updated_at` SimpleAggregateFunction(max, DateTime64(3)),
+
+ -- Indexes
+ INDEX idx_environment environment 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
+) 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
+ t0.project_id as project_id,
+ t0.id as id,
+ min(t0.start_time) as start_time,
+ max(coalesce(t0.end_time, t0.start_time)) as end_time,
+ argMaxState(t0.name, if(t0.name <> '', t0.event_ts, toDateTime64(0, 3))) as name_argmax,
+ anyLast(t0.name) as name,
+
+ -- Metadata properties
+ argMaxState(t0.metadata, t0.event_ts) as metadata,
+ argMaxState(t0.user_id, if(t0.user_id <> '', t0.event_ts, toDateTime64(0, 3))) as user_id_argmax,
+ argMaxState(t0.session_id, if(t0.session_id <> '', t0.event_ts, toDateTime64(0, 3))) as session_id_argmax,
+ argMaxState(t0.environment, if(t0.environment <> '', t0.event_ts, toDateTime64(0, 3))) as environment_argmax,
+ anyLast(t0.user_id) as user_id,
+ anyLast(t0.session_id) as session_id,
+ anyLast(t0.environment) as environment,
+ groupUniqArrayArray(t0.tags) as tags,
+ argMaxState(t0.version, if(t0.version <> '', t0.event_ts, toDateTime64(0, 3))) as version,
+ argMaxState(t0.release, if(t0.release <> '', t0.event_ts, toDateTime64(0, 3))) as release,
+
+ -- UI properties
+ argMaxState(t0.bookmarked, if(t0.bookmarked is not null, t0.event_ts, toDateTime64(0, 3))) as bookmarked,
+ argMaxState(t0.public, if(t0.public is not null, t0.event_ts, toDateTime64(0, 3))) as public,
+
+ -- Aggregations
+ groupUniqArrayArray(t0.observation_ids) as observation_ids,
+ groupUniqArrayArray(t0.score_ids) as score_ids,
+ sumMap(t0.cost_details) as cost_details,
+ sumMap(t0.usage_details) as usage_details,
+
+ -- Input/Output
+ argMaxState(t0.input, if(t0.input <> '', t0.event_ts, toDateTime64(0, 3))) as input_argmax,
+ argMaxState(t0.output, if(t0.output <> '', t0.event_ts, toDateTime64(0, 3))) as output_argmax,
+ anyLast(input) as input,
+ anyLast(output) as output,
+
+ min(t0.created_at) as created_at,
+ max(t0.updated_at) as updated_at
+FROM traces_mt t0
+GROUP BY project_id, id;
+```
+
+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,
+ finalizeAggregation(name_argmax) AS name_argmax_value,
+ name,
+
+ -- Metadata properties
+ finalizeAggregation(metadata) AS metadata_value,
+ finalizeAggregation(user_id_argmax) AS user_id_argmax_value,
+ finalizeAggregation(session_id_argmax) AS session_id_argmax_value,
+ finalizeAggregation(environment_argmax) AS environment_argmax_value,
+ user_id,
+ session_id,
+ environment,
+ tags,
+ finalizeAggregation(version) AS version_value,
+ finalizeAggregation(release) AS release_value,
+
+ -- 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_argmax) AS input_argmax_value,
+ finalizeAggregation(output_argmax) AS output_argmax_value,
+ input,
+ output,
+
+ created_at,
+ updated_at
+FROM traces_all_amt
+LIMIT 100;
+```
diff --git a/worker/src/services/IngestionService/index.ts b/worker/src/services/IngestionService/index.ts
index dcc21d8ffd..ec62e4eeef 100644
--- a/worker/src/services/IngestionService/index.ts
+++ b/worker/src/services/IngestionService/index.ts
@@ -33,6 +33,7 @@ import {
TraceEventType,
traceRecordInsertSchema,
TraceRecordInsertType,
+ TraceMtRecordInsertType,
traceRecordReadSchema,
TraceUpsertQueue,
UsageCostType,
@@ -318,6 +319,14 @@ export class IngestionService {
this.clickHouseWriter.addToQueue(TableName.Traces, finalTraceRecord);
+ // Experimental: Also write to traces_mt table if experiment flag is enabled
+ if (
+ env.LANGFUSE_EXPERIMENT_INSERT_INTO_AGGREGATING_MERGE_TREES === "true"
+ ) {
+ const traceMtRecord = this.convertTraceToTraceMt(finalTraceRecord);
+ this.clickHouseWriter.addToQueue(TableName.TracesMt, traceMtRecord);
+ }
+
// Add trace into trace upsert queue for eval processing
const traceUpsertQueue = TraceUpsertQueue.getInstance();
if (!traceUpsertQueue) {
@@ -494,13 +503,52 @@ export class IngestionService {
return traceRecordInsertSchema.parse(mergedRecord);
}
+ private convertTraceToTraceMt(
+ traceRecord: TraceRecordInsertType,
+ ): TraceMtRecordInsertType {
+ return {
+ // Identifiers
+ project_id: traceRecord.project_id,
+ id: traceRecord.id,
+ start_time: traceRecord.timestamp,
+ end_time: null, // traces don't have end_time, will be null
+ name: traceRecord.name || "",
+
+ // Metadata properties
+ metadata: traceRecord.metadata,
+ user_id: traceRecord.user_id || "",
+ session_id: traceRecord.session_id || "",
+ environment: traceRecord.environment,
+ tags: traceRecord.tags,
+ version: traceRecord.version,
+ release: traceRecord.release,
+
+ // UI properties - nullable to prevent absent values being interpreted as overwrites
+ bookmarked: traceRecord.bookmarked,
+ public: traceRecord.public,
+
+ // Aggregations - empty for now, will be populated by aggregation processes
+ observation_ids: [],
+ score_ids: [],
+ cost_details: {},
+ usage_details: {},
+
+ // Input/Output
+ input: traceRecord.input || "",
+ output: traceRecord.output || "",
+
+ created_at: traceRecord.created_at,
+ updated_at: traceRecord.updated_at,
+ event_ts: traceRecord.event_ts,
+ };
+ }
+
private async mergeObservationRecords(params: {
projectId: string;
observationRecords: ObservationRecordInsertType[];
clickhouseObservationRecord?: ObservationRecordInsertType | null;
}): Promise<ObservationRecordInsertType> {
- const { projectId, observationRecords, clickhouseObservationRecord } =
- params;
+ const { observationRecords, clickhouseObservationRecord } = params;
// Set clickhouse first as this is the baseline for immutable fields
const recordsToMerge = [