Skip to content

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

chore: fill clickhouse trace for observations with missing traceId (#4246)

Steffen Schmitz74e378fe4d21
diff --git a/packages/shared/src/server/ingestion/legacy/EventProcessor.ts b/packages/shared/src/server/ingestion/legacy/EventProcessor.ts
index 33e85c991d..93ae5f5cff 100644
--- a/packages/shared/src/server/ingestion/legacy/EventProcessor.ts
+++ b/packages/shared/src/server/ingestion/legacy/EventProcessor.ts
@@ -20,6 +20,9 @@ import { jsonSchema } from "../../../utils/zod";
import { prisma } from "../../../db";
import { LegacyIngestionAccessScope } from ".";
import { logger } from "../../logger";
+import { env } from "../../../env";
+import { upsertTrace } from "../../repositories";
+import { convertDateToClickhouseDateTime } from "../../clickhouse/client";
export interface EventProcessor {
auth(apiScope: LegacyIngestionAccessScope): void;
@@ -131,19 +134,6 @@ export class ObservationProcessor implements EventProcessor {
})
: undefined;
- const traceId =
- !this.event.body.traceId && !existingObservation
- ? // Create trace if no traceid
- (
- await prisma.trace.create({
- data: {
- projectId: apiScope.projectId,
- name: this.event.body.name,
- },
- })
- ).id
- : this.event.body.traceId;
-
// Token counts
const [newInputCount, newOutputCount] =
"usage" in this.event.body
@@ -230,11 +220,46 @@ export class ObservationProcessor implements EventProcessor {
return newId;
})();
+ let traceId = this.event.body?.traceId;
+ if (!this.event.body.traceId && !existingObservation) {
+ // Create trace if no traceId
+ traceId = observationId;
+
+ // Insert trace into postgres
+ await prisma.trace.upsert({
+ where: {
+ id: observationId,
+ },
+ create: {
+ projectId: apiScope.projectId,
+ name: this.event.body.name,
+ id: observationId,
+ timestamp: this.event.body.startTime || new Date(),
+ },
+ update: {},
+ });
+
+ if (env.CLICKHOUSE_URL) {
+ // Insert trace into clickhouse if enabled
+ await upsertTrace({
+ id: observationId,
+ project_id: apiScope.projectId,
+ timestamp: convertDateToClickhouseDateTime(
+ this.event.body.startTime
+ ? new Date(this.event.body.startTime)
+ : new Date(),
+ ),
+ created_at: convertDateToClickhouseDateTime(new Date()),
+ updated_at: convertDateToClickhouseDateTime(new Date()),
+ });
+ }
+ }
+
return {
id: observationId,
create: {
id: observationId,
- traceId: traceId,
+ traceId,
type: type,
name: this.event.body.name,
startTime: this.event.body.startTime
diff --git a/worker/src/services/IngestionService/index.ts b/worker/src/services/IngestionService/index.ts
index 2b980bc001..97884c1f79 100644
--- a/worker/src/services/IngestionService/index.ts
+++ b/worker/src/services/IngestionService/index.ts
@@ -302,9 +302,8 @@ export class IngestionService {
// Backward compat: create wrapper trace for SDK < 2.0.0 events that do not have a traceId
if (!finalObservationRecord.trace_id) {
- const traceId = randomUUID();
const wrapperTraceRecord: TraceRecordInsertType = {
- id: traceId,
+ id: finalObservationRecord.id,
timestamp: finalObservationRecord.start_time,
project_id: projectId,
created_at: Date.now(),
@@ -318,7 +317,7 @@ export class IngestionService {
};
this.clickHouseWriter.addToQueue(TableName.Traces, wrapperTraceRecord);
- finalObservationRecord.trace_id = traceId;
+ finalObservationRecord.trace_id = finalObservationRecord.id;
}
this.clickHouseWriter.addToQueue(