Skip to content

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

chore(shared): add OTel trace ID to ClickHouse log_comment (#14357)

Niklas Semmler846ab7fe54a4
diff --git a/packages/shared/src/server/repositories/clickhouse.ts b/packages/shared/src/server/repositories/clickhouse.ts
index 01f13f4f7f..9337ecd1f9 100644
--- a/packages/shared/src/server/repositories/clickhouse.ts
+++ b/packages/shared/src/server/repositories/clickhouse.ts
@@ -9,7 +9,13 @@ import { getTracer, instrumentAsync } from "../instrumentation";
import { randomUUID } from "crypto";
import { getClickhouseEntityType } from "../clickhouse/schemaUtils";
import { NodeClickHouseClientConfigOptions } from "@clickhouse/client/dist/config";
-import { type Span, context, SpanKind, trace } from "@opentelemetry/api";
+import {
+ type Span,
+ context,
+ isSpanContextValid,
+ SpanKind,
+ trace,
+} from "@opentelemetry/api";
import { backOff } from "exponential-backoff";
import {
StorageService,
@@ -180,7 +186,7 @@ export async function upsertClickhouse<
],
format: "JSONEachRow",
clickhouse_settings: {
- log_comment: JSON.stringify(opts.tags ?? {}),
+ log_comment: JSON.stringify(tagsWithTraceId(opts.tags)),
},
});
}
@@ -206,7 +212,7 @@ export async function upsertClickhouse<
})),
format: "JSONEachRow",
clickhouse_settings: {
- log_comment: JSON.stringify(opts.tags ?? {}),
+ log_comment: JSON.stringify(tagsWithTraceId(opts.tags)),
},
});
// same logic as for prisma. we want to see queries in development
@@ -370,6 +376,18 @@ function setSpanQueryAttributes(span: Span, query: string): void {
span.setAttribute("db.operation.name", "SELECT");
}
+export function tagsWithTraceId(
+ tags: Record<string, string> | undefined,
+): Record<string, string> {
+ const ctx = trace.getActiveSpan()?.spanContext();
+ if (!ctx || !isSpanContextValid(ctx)) return tags ?? {};
+ // Use a distinct, OTel-specific key so this never collides with a
+ // caller-supplied `traceId` tag (e.g. the Langfuse business trace ID used
+ // for query_log JOINs in dataset-run-items). A single log_comment row can
+ // then carry both identifiers.
+ return { ...tags, otel_trace_id: ctx.traceId };
+}
+
async function sendClickhouseQuery<F extends DataFormat>(opts: {
query: string;
params?: Record<string, unknown>;
@@ -389,7 +407,7 @@ async function sendClickhouseQuery<F extends DataFormat>(opts: {
query_params: opts.params,
clickhouse_settings: {
...opts.clickhouseSettings,
- log_comment: JSON.stringify(opts.tags ?? {}),
+ log_comment: JSON.stringify(tagsWithTraceId(opts.tags)),
},
});
@@ -567,7 +585,7 @@ export async function commandClickhouse(opts: {
...(opts.abortSignal ? { abort_signal: opts.abortSignal } : {}),
clickhouse_settings: {
...opts.clickhouseSettings,
- log_comment: JSON.stringify(opts.tags ?? {}),
+ log_comment: JSON.stringify(tagsWithTraceId(opts.tags)),
},
});
// same logic as for prisma. we want to see queries in development
diff --git a/worker/src/services/ClickhouseWriter/index.ts b/worker/src/services/ClickhouseWriter/index.ts
index c13c4bf4e8..a856425974 100644
--- a/worker/src/services/ClickhouseWriter/index.ts
+++ b/worker/src/services/ClickhouseWriter/index.ts
@@ -13,6 +13,7 @@ import {
TraceNullRecordInsertType,
DatasetRunItemRecordInsertType,
EventRecordInsertType,
+ tagsWithTraceId,
} from "@langfuse/shared/src/server";
import { Decimal } from "decimal.js";
@@ -577,15 +578,16 @@ export class ClickhouseWriter {
format: "JSONEachRow",
values: params.records,
clickhouse_settings: {
- log_comment: JSON.stringify({
- feature: "ingestion",
- type: params.table,
- operation_name: "writeToClickhouse",
- projectId:
- params.records.length > 0
- ? params.records[0].project_id
- : undefined,
- }),
+ log_comment: JSON.stringify(
+ tagsWithTraceId({
+ feature: "ingestion",
+ type: params.table,
+ operation_name: "writeToClickhouse",
+ ...(params.records.length > 0 && params.records[0].project_id
+ ? { projectId: params.records[0].project_id }
+ : {}),
+ }),
+ ),
},
})
.catch((err) => {
diff --git a/worker/src/services/IngestionService/index.ts b/worker/src/services/IngestionService/index.ts
index 790ae43969..7d51d24884 100644
--- a/worker/src/services/IngestionService/index.ts
+++ b/worker/src/services/IngestionService/index.ts
@@ -49,6 +49,7 @@ import {
getDatasetItemById,
normalizeToolsForObservation,
hasNoEvalConfigsCache,
+ tagsWithTraceId,
} from "@langfuse/shared/src/server";
import { tokenCountAsync } from "../../features/tokenisation/async-usage";
@@ -1435,10 +1436,12 @@ export class IngestionService {
format: "JSONEachRow",
query_params: { projectId, entityId, ...additionalFilters.params },
clickhouse_settings: {
- log_comment: JSON.stringify({
- feature: "ingestion",
- projectId,
- }),
+ log_comment: JSON.stringify(
+ tagsWithTraceId({
+ feature: "ingestion",
+ projectId,
+ }),
+ ),
},
});