Skip to content

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

chore: update validations and avoid spanName/traceId conflicts (#9822)

Steffen Schmitz3986dc6a9cf3
diff --git a/packages/shared/clickhouse/scripts/dev-tables.sh b/packages/shared/clickhouse/scripts/dev-tables.sh
index 98d4949f4f..02dddd33be 100644
--- a/packages/shared/clickhouse/scripts/dev-tables.sh
+++ b/packages/shared/clickhouse/scripts/dev-tables.sh
@@ -226,7 +226,8 @@ CREATE TABLE IF NOT EXISTS events
ENGINE = ReplacingMergeTree(event_ts, is_deleted)
-- ENGINE = (Replicated)ReplacingMergeTree(event_ts, is_deleted)
PARTITION BY toYYYYMM(start_time)
- ORDER BY (project_id, toUnixTimestamp(start_time), trace_id, span_id);
+ ORDER BY (project_id, toUnixTimestamp(start_time), xxHash32(trace_id), span_id)
+ SAMPLE BY xxHash32(trace_id);
EOF
diff --git a/packages/shared/src/server/repositories/definitions.ts b/packages/shared/src/server/repositories/definitions.ts
index d12155737c..4fb09a8a11 100644
--- a/packages/shared/src/server/repositories/definitions.ts
+++ b/packages/shared/src/server/repositories/definitions.ts
@@ -344,8 +344,8 @@ export const convertTraceToStagingObservation = (
s3FirstSeenTimestamp: number,
): ObservationBatchStagingRecordInsertType => {
return {
- // Identity - trace acts as its own span
- id: traceRecord.id,
+ // Identity - trace acts as its own span. Modify traceId to avoid cases where users set spanId = traceId.
+ id: `t-${traceRecord.id}`,
trace_id: traceRecord.id,
project_id: traceRecord.project_id,
diff --git a/worker/src/features/eventPropagation/EVENT_BACKFILL_VALIDATION_QUERIES.md b/worker/src/features/eventPropagation/EVENT_BACKFILL_VALIDATION_QUERIES.md
index 720da565d1..d889e49d6c 100644
--- a/worker/src/features/eventPropagation/EVENT_BACKFILL_VALIDATION_QUERIES.md
+++ b/worker/src/features/eventPropagation/EVENT_BACKFILL_VALIDATION_QUERIES.md
@@ -7,8 +7,8 @@ WITH
-- Time range configuration
time_range AS (
SELECT
- toDateTime('2025-10-14 13:00:00') AS min_time,
- toDateTime('2025-10-14 14:45:00') AS max_time
+ toDateTime('2025-10-17 09:00:00') AS min_time,
+ toDateTime('2025-10-17 13:45:00') AS max_time
),
-- Get filtered observations
@@ -22,7 +22,7 @@ WITH
-- Get filtered events
filtered_events AS (
SELECT *
- FROM events
+ FROM events e
WHERE start_time >= (SELECT min_time FROM time_range)
AND start_time <= (SELECT max_time FROM time_range)
and e.source = 'ingestion-api'
@@ -95,8 +95,8 @@ WITH
-- Time range configuration
time_range AS (
SELECT
- toDateTime('2025-10-14 13:00:00') AS min_time,
- toDateTime('2025-10-14 14:45:00') AS max_time
+ toDateTime('2025-10-17 09:00:00') AS min_time,
+ toDateTime('2025-10-17 13:45:00') AS max_time
),
sampled_projects AS (
@@ -105,7 +105,7 @@ WITH
WHERE o.start_time >= (SELECT min_time FROM time_range)
AND o.start_time <= (SELECT max_time FROM time_range)
ORDER BY rand()
- LIMIT 100
+ LIMIT 10
),
-- Join observations with events
@@ -336,8 +336,8 @@ WITH
-- Time range configuration
time_range AS (
SELECT
- toDateTime('2025-10-14 13:00:00') AS min_time,
- toDateTime('2025-10-14 14:45:00') AS max_time
+ toDateTime('2025-10-17 09:00:00') AS min_time,
+ toDateTime('2025-10-17 13:45:00') AS max_time
),
sampled_projects AS (
@@ -407,3 +407,87 @@ WHERE NOT arrayAll(k -> has(e_metadata_names, k), mapKeys(t_metadata))
SETTINGS join_algorithm = 'partial_merge'
```
+
+## Query 4: Root Span Validation
+
+```sql
+WITH
+ -- Time range configuration
+ time_range AS (
+ SELECT
+ toDateTime('2025-10-17 09:00:00') AS min_time,
+ toDateTime('2025-10-17 13:45:00') AS max_time
+ ),
+
+ sampled_projects AS (
+ SELECT project_id
+ FROM observations o
+ WHERE o.start_time >= (SELECT min_time FROM time_range)
+ AND o.start_time <= (SELECT max_time FROM time_range)
+ ORDER BY rand()
+ LIMIT 10
+ ),
+
+ -- Join with events to find root spans
+ root_span_check AS (
+ SELECT
+ t.project_id,
+ t.id AS trace_id,
+ t.name AS trace_name,
+ e.span_id AS root_span_id,
+ e.parent_span_id AS root_parent_span_id,
+ e.name AS root_span_name
+ FROM traces t
+ LEFT JOIN events e
+ ON t.project_id = e.project_id
+ AND e.trace_id = t.id
+ AND e.parent_span_id = ''
+ AND e.start_time >= (SELECT min_time FROM time_range)
+ AND e.start_time <= (SELECT max_time FROM time_range)
+ WHERE t.project_id IN (SELECT project_id FROM sampled_projects)
+ AND t.timestamp >= (SELECT min_time FROM time_range)
+ AND t.timestamp <= (SELECT max_time FROM time_range)
+ AND (e.source is NULL or e.source = 'ingestion-api')
+ )
+
+-- Missing root spans
+SELECT
+ 'missing_root_span' AS issue_type,
+ project_id,
+ trace_id,
+ trace_name AS expected_name,
+ concat('t-', trace_id) AS expected_span_id,
+ '' AS actual_value
+FROM root_span_check
+WHERE root_span_id IS NULL
+
+UNION ALL
+
+-- Root spans with non-null parent_span_id
+SELECT
+ 'root_span_has_parent' AS issue_type,
+ project_id,
+ trace_id,
+ trace_name AS expected_name,
+ root_span_id AS expected_span_id,
+ root_parent_span_id AS actual_value
+FROM root_span_check
+WHERE root_span_id IS NOT NULL
+ AND root_parent_span_id != ''
+
+UNION ALL
+
+-- Root spans with name mismatch
+SELECT
+ 'root_span_name_mismatch' AS issue_type,
+ project_id,
+ trace_id,
+ trace_name AS expected_name,
+ root_span_id AS expected_span_id,
+ root_span_name AS actual_value
+FROM root_span_check
+WHERE root_span_id IS NOT NULL
+ AND root_span_name != trace_name
+
+SETTINGS join_algorithm = 'partial_merge'
+```
diff --git a/worker/src/features/eventPropagation/handleEventPropagationJob.ts b/worker/src/features/eventPropagation/handleEventPropagationJob.ts
index 5b1f6de5bd..1ff26d3480 100644
--- a/worker/src/features/eventPropagation/handleEventPropagationJob.ts
+++ b/worker/src/features/eventPropagation/handleEventPropagationJob.ts
@@ -278,10 +278,10 @@ export const handleEventPropagationJob = async (
obs.trace_id,
obs.id AS 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
+ -- Otherwise, use standard wrapper logic: parent_observation_id or prefixed trace_id as fallback
CASE
WHEN obs.id = obs.trace_id THEN NULL
- ELSE coalesce(obs.parent_observation_id, obs.trace_id)
+ ELSE coalesce(obs.parent_observation_id, concat('t-', 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() errors