🪢 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
chore: create virtual trace spans for historic compatibility (#9718)
Steffen Schmitz6d0323e9c4d9
diff --git a/packages/shared/src/server/repositories/definitions.ts b/packages/shared/src/server/repositories/definitions.tsindex 05eb0e0c30..8cd1d2b8d2 100644--- a/packages/shared/src/server/repositories/definitions.ts+++ b/packages/shared/src/server/repositories/definitions.ts@@ -333,6 +333,68 @@ export const convertScoreReadToInsert = (};};+/**+ * Converts a trace record to a staging observation record.+ * The trace is treated as a synthetic "SPAN" where span_id = trace_id.+ * This allows traces to flow through the same batch propagation pipeline as observations.+ */+export const convertTraceToStagingObservation = (+ traceRecord: TraceRecordInsertType,+ s3FirstSeenTimestamp: number,+): ObservationBatchStagingRecordInsertType => {+ return {+ // Identity - trace acts as its own span+ id: traceRecord.id,+ trace_id: traceRecord.id,+ project_id: traceRecord.project_id,++ // Type: pretend trace is a SPAN+ type: "SPAN",++ // No parent since traces are root-level+ parent_observation_id: undefined,++ // Core fields from trace+ name: traceRecord.name,+ environment: traceRecord.environment,+ version: traceRecord.version,+ metadata: traceRecord.metadata,++ // Timing: trace.timestamp -> start_time+ start_time: traceRecord.timestamp,+ end_time: undefined,+ completion_start_time: undefined,++ // IO fields+ input: traceRecord.input,+ output: traceRecord.output,++ // Default values for observation-specific fields+ level: "DEFAULT",+ status_message: undefined,+ provided_model_name: undefined,+ internal_model_id: undefined,+ model_parameters: undefined,+ provided_usage_details: {},+ usage_details: {},+ provided_cost_details: {},+ cost_details: {},+ total_cost: undefined,+ prompt_id: undefined,+ prompt_name: undefined,+ prompt_version: undefined,++ // System fields+ created_at: traceRecord.created_at,+ updated_at: traceRecord.updated_at,+ event_ts: traceRecord.event_ts,+ is_deleted: traceRecord.is_deleted,++ // Staging-specific field+ s3_first_seen_timestamp: s3FirstSeenTimestamp,+ };+};+/*** Expects a single record from a `select * from traces` query. Must be a raw query to keep original* column names, not the Prisma mapped names.diff --git a/worker/src/features/eventPropagation/handleEventPropagationJob.ts b/worker/src/features/eventPropagation/handleEventPropagationJob.tsindex 6a0f532975..6f41e9c334 100644--- a/worker/src/features/eventPropagation/handleEventPropagationJob.ts+++ b/worker/src/features/eventPropagation/handleEventPropagationJob.ts@@ -150,9 +150,12 @@ export const handleEventPropagationJob = async (obs.project_id,obs.trace_id,obs.id AS span_id,- -- if parent_observation_id is null, use trace_id as parent_span_id- -- this way we threat the case as the "wrapper" for spans backfilled this way.- coalesce(obs.parent_observation_id, obs.trace_id) AS parent_span_id,+ -- When the observation IS the trace itself (id = trace_id), parent should be NULL+ -- Otherwise, use standard wrapper logic: parent_observation_id or trace_id as fallback+ CASE+ WHEN obs.id = obs.trace_id THEN NULL+ ELSE coalesce(obs.parent_observation_id, obs.trace_id)+ END AS parent_span_id,-- Convert timestamps from DateTime64(3) to DateTime64(6) via implicit conversion-- Clamp start_time to 1970-01-01 or later (Unix epoch minimum) to avoid toUnixTimestamp() errorsgreatest(obs.start_time, toDateTime64('1970-01-01', 3)) AS start_time,diff --git a/worker/src/services/IngestionService/index.ts b/worker/src/services/IngestionService/index.tsindex 4b1731263d..34e6ec5515 100644--- a/worker/src/services/IngestionService/index.ts+++ b/worker/src/services/IngestionService/index.ts@@ -13,6 +13,7 @@ import {convertObservationReadToInsert,convertScoreReadToInsert,convertTraceReadToInsert,+ convertTraceToStagingObservation,eventTypes,IngestionEntityTypes,IngestionEventType,@@ -577,7 +578,7 @@ export class IngestionService {entityId,createdAtTimestamp,traceEventList,- // createEventTraceRecord,+ createEventTraceRecord,} = params;if (traceEventList.length === 0) return;@@ -664,9 +665,18 @@ export class IngestionService {}}- // TODO: If createEventTraceRecord is true, create a corresponding event in the events table.- // Here, we pretend that the current trace is a "span" where we set the spanId = traceId.- // The corresponding root span, will set the traceId as parentSpanId to keep the existing "wrapper" behaviour.+ // Dual-write to staging table for batch propagation to events table+ // We pretend the trace is a "span" where span_id = trace_id+ if (createEventTraceRecord) {+ const traceAsStagingObservation = convertTraceToStagingObservation(+ finalTraceRecord,+ createdAtTimestamp.getTime(),+ );+ this.clickHouseWriter.addToQueue(+ TableName.ObservationsBatchStaging,+ traceAsStagingObservation,+ );+ }// Add trace into trace upsert queue for eval processing// First check if we already know this project has no job configurations