🪢 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
exp: write observation and score info to traces_mt (#7590)
Steffen Schmitza13ddd41ae0e
diff --git a/worker/src/services/IngestionService/README.md b/worker/src/services/IngestionService/README.mdindex 9937be5508..f91c80c87c 100644--- a/worker/src/services/IngestionService/README.md+++ b/worker/src/services/IngestionService/README.md@@ -71,7 +71,11 @@ CREATE TABLE traces_all_amt`name` SimpleAggregateFunction(anyLast, String),-- Metadata properties- `metadata` AggregateFunction(argMax, Map(LowCardinality(String), String), DateTime64(3)),+ -- Metadata takes the last seen value in this form.+ -- If we spread it across multiple rows (keys + values) we might be able to allow updates/overwrites.+ -- Indexing speed in this case is unclear though.+ `metadata_argmax` AggregateFunction(argMax, Map(LowCardinality(String), String), DateTime64(3)),+ `metadata` SimpleAggregateFunction(anyLast, Map(String, String)),`user_id_argmax` AggregateFunction(argMax, String, DateTime64(3)),`session_id_argmax` AggregateFunction(argMax, String, DateTime64(3)),`environment_argmax` AggregateFunction(argMax, String, DateTime64(3)),@@ -121,14 +125,15 @@ SELECTanyLast(t0.name) as name,-- Metadata properties- argMaxState(t0.metadata, t0.event_ts) as metadata,+ argMaxState(t0.metadata, if(t0.metadata <> '{}', t0.event_ts, toDateTime64(0,3))) as metadata_argmax,+ anyLastMap(t0.metadata) 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,+ 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,@@ -165,7 +170,8 @@ CREATE TABLE traces_7d_amt`name` SimpleAggregateFunction(anyLast, String),-- Metadata properties- `metadata` AggregateFunction(argMax, Map(LowCardinality(String), String), DateTime64(3)),+ `metadata_argmax` AggregateFunction(argMax, Map(LowCardinality(String), String), DateTime64(3)),+ `metadata` SimpleAggregateFunction(anyLast, Map(String, String)),`user_id_argmax` AggregateFunction(argMax, String, DateTime64(3)),`session_id_argmax` AggregateFunction(argMax, String, DateTime64(3)),`environment_argmax` AggregateFunction(argMax, String, DateTime64(3)),@@ -215,14 +221,15 @@ SELECTanyLast(t0.name) as name,-- Metadata properties- argMaxState(t0.metadata, t0.event_ts) as metadata,+ argMaxState(t0.metadata, if(t0.metadata <> '{}', t0.event_ts, toDateTime64(0,3))) as metadata_argmax,+ anyLastMap(t0.metadata) 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,+ 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,@@ -259,7 +266,8 @@ CREATE TABLE traces_30d_amt`name` SimpleAggregateFunction(anyLast, String),-- Metadata properties- `metadata` AggregateFunction(argMax, Map(LowCardinality(String), String), DateTime64(3)),+ `metadata_argmax` AggregateFunction(argMax, Map(LowCardinality(String), String), DateTime64(3)),+ `metadata` SimpleAggregateFunction(anyLast, Map(String, String)),`user_id_argmax` AggregateFunction(argMax, String, DateTime64(3)),`session_id_argmax` AggregateFunction(argMax, String, DateTime64(3)),`environment_argmax` AggregateFunction(argMax, String, DateTime64(3)),@@ -309,14 +317,15 @@ SELECTanyLast(t0.name) as name,-- Metadata properties- argMaxState(t0.metadata, t0.event_ts) as metadata,+ argMaxState(t0.metadata, if(t0.metadata <> '{}', t0.event_ts, toDateTime64(0,3))) as metadata_argmax,+ anyLastMap(t0.metadata) 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,+ 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,@@ -342,6 +351,8 @@ FROM traces_mt t0GROUP BY project_id, id;```+## Query AggregatingMergeTrees+We can query the properties of the resulting AggregatingMergeTree via (make sure to pick the right timeframe:```sqlSELECT@@ -386,3 +397,129 @@ SELECTFROM traces_all_amtLIMIT 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,+ finalizeAggregation(name_argmax) AS name_argmax_value,+ name AS name_anylast_value,++ finalizeAggregation(metadata_argmax) AS metadata_argmax_value,+ metadata AS metadata_anylast_value,+ finalizeAggregation(user_id_argmax) AS user_id_argmax_value,+ user_id AS user_id_anylast_value,+ finalizeAggregation(session_id_argmax) AS session_id_argmax_value,+ session_id AS session_id_anylast_value,+ finalizeAggregation(environment_argmax) AS environment_argmax_value,+ environment AS environment_anylast_value,+ tags,+ finalizeAggregation(version) AS version_value,+ finalizeAggregation(release) AS release_value,++ finalizeAggregation(bookmarked) AS bookmarked_value,+ finalizeAggregation(public) AS public_value,++ finalizeAggregation(input_argmax) AS input_argmax_value,+ input AS input_anylast_value,+ finalizeAggregation(output_argmax) AS output_argmax_value,+ output AS output_anylast_value,++ created_at,+ updated_at+ FROM traces_all_amt+)++-- Main query to compare AMT with original traces table+SELECT t.project_id,+ t.id,+ -- Identify differences between tables+ t.timestamp != a.start_time AS timestamp_diff,+ t.name != a.name_anylast_value AS name_diff,+ t.user_id != a.user_id_anylast_value AS user_id_diff,+ t.session_id != a.session_id_anylast_value AS session_id_diff,+ t.release != a.release_value AS release_diff,+ t.version != a.version_value AS version_diff,+ t.public != a.public_value AS public_diff,+ t.bookmarked != a.bookmarked_value AS bookmarked_diff,+ arraySort(t.tags) != arraySort(a.tags) AS tags_diff,+ t.input != a.input_anylast_value AS input_diff,+ t.output != a.output_anylast_value AS output_diff,+ t.metadata != a.metadata_anylast_value AS metadata_diff,++ a.name_argmax_value != a.name_anylast_value AS amt_name_diff,+ a.user_id_argmax_value != a.user_id_anylast_value AS amt_user_id_diff,+ a.session_id_argmax_value != a.session_id_anylast_value AS amt_session_id_diff,+ a.environment_argmax_value != a.environment_anylast_value AS amt_environment_diff,+ a.input_argmax_value != a.input_anylast_value AS amt_input_diff,+ a.output_argmax_value != a.output_anylast_value AS amt_output_diff,+ a.metadata_argmax_value != a.metadata_anylast_value AS amt_metadata_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_anylast_value AS amt_name_anylast,+ a.name_argmax_value AS amt_name_argmax,+ t.user_id AS traces_user_id,+ a.user_id_anylast_value AS amt_user_id_anylast,+ a.user_id_argmax_value AS amt_user_id_argmax,+ t.session_id AS traces_session_id,+ a.session_id_anylast_value AS amt_session_id_anylast,+ a.session_id_argmax_value AS amt_session_id_argmax,+ t.environment AS traces_environment,+ a.environment_anylast_value AS amt_environment_anylast,+ a.environment_argmax_value AS amt_environment_argmax,++ t.metadata AS traces_metadata,+ a.metadata_anylast_value AS amt_metadata_anylast,+ a.metadata_argmax_value AS amt_metadata_argmax,+ 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_value AS amt_release,+ t.version AS traces_version,+ a.version_value AS amt_version,++ t.input AS traces_input,+ a.input_anylast_value AS amt_input_anylast,+ a.input_argmax_value AS amt_input_argmax,+ t.output AS traces_output,+ a.output_anylast_value AS amt_output_anylast,+ a.output_argmax_value AS amt_output_argmax,++ 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+ 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;+```\ No newline at end of filediff --git a/worker/src/services/IngestionService/index.ts b/worker/src/services/IngestionService/index.tsindex ec62e4eeef..2590d9651b 100644--- a/worker/src/services/IngestionService/index.ts+++ b/worker/src/services/IngestionService/index.ts@@ -220,6 +220,14 @@ export class IngestionService {clickhouseScoreRecord?.created_at ?? createdAtTimestamp.getTime();this.clickHouseWriter.addToQueue(TableName.Scores, finalScoreRecord);++ if (+ env.LANGFUSE_EXPERIMENT_INSERT_INTO_AGGREGATING_MERGE_TREES === "true" &&+ finalScoreRecord.trace_id+ ) {+ const traceMtRecord = this.convertScoreToTraceMt(finalScoreRecord);+ this.clickHouseWriter.addToQueue(TableName.TracesMt, traceMtRecord);+ }}private async processTraceEventList(params: {@@ -453,6 +461,14 @@ export class IngestionService {TableName.Observations,finalObservationRecord,);++ if (+ env.LANGFUSE_EXPERIMENT_INSERT_INTO_AGGREGATING_MERGE_TREES === "true" &&+ finalObservationRecord.trace_id+ ) {+ const traceMtRecord = this.convertObservationToTraceMt(finalObservationRecord);+ this.clickHouseWriter.addToQueue(TableName.TracesMt, traceMtRecord);+ }}private async mergeScoreRecords(params: {@@ -543,6 +559,90 @@ export class IngestionService {};}+ private convertObservationToTraceMt(+ observationRecord: ObservationRecordInsertType,+ ): TraceMtRecordInsertType {+ return {+ // Identifiers+ project_id: observationRecord.project_id,+ // Use trace_id as the id in traces_mt. Always set given the conditions around calling the function+ id: observationRecord.trace_id || "",+ start_time: observationRecord.start_time,+ end_time: observationRecord.end_time || null,+ name: "",++ // Metadata properties+ metadata: {},+ user_id: '',+ session_id: '',+ environment: observationRecord.environment,+ tags: [],+ version: null,+ release: null,++ // UI properties - nullable to prevent absent values being interpreted as overwrites+ bookmarked: null,+ public: null,++ // Aggregations - include this observation ID+ observation_ids: [observationRecord.id],+ score_ids: [],+ // We can fill the cost details here, but we shouldn't trust them.+ // Only used for verification to estimate how big the double-counting is.+ cost_details: observationRecord.cost_details || {},+ usage_details: observationRecord.usage_details || {},++ // Input/Output+ input: "",+ output: "",++ created_at: observationRecord.created_at,+ updated_at: observationRecord.updated_at,+ event_ts: observationRecord.event_ts,+ };+ }++ private convertScoreToTraceMt(+ scoreRecord: ScoreRecordInsertType,+ ): TraceMtRecordInsertType {+ return {+ // Identifiers+ project_id: scoreRecord.project_id,+ // Use trace_id as the id in traces_mt. Always set given the conditions around calling the function+ id: scoreRecord.trace_id || "",+ start_time: scoreRecord.timestamp,+ end_time: null, // scores don't have end_time+ name: "",++ // Metadata properties+ metadata: {},+ user_id: '',+ session_id: '',+ environment: scoreRecord.environment,+ tags: [], // scores don't have tags+ version: null, // scores don't have version+ release: null, // scores don't have release++ // UI properties - nullable to prevent absent values being interpreted as overwrites+ bookmarked: null,+ public: null,++ // Aggregations - include this score ID+ observation_ids: [],+ score_ids: [scoreRecord.id],+ cost_details: {},+ usage_details: {},++ // Input/Output+ input: "", // scores don't have input+ output: "", // scores don't have output++ created_at: scoreRecord.created_at,+ updated_at: scoreRecord.updated_at,+ event_ts: scoreRecord.event_ts,+ };+ }+private async mergeObservationRecords(params: {projectId: string;observationRecords: ObservationRecordInsertType[];