fix: ensure 'root_span_id' column in 'traces' table is not null

Using the telemetry API to query the sqlite store of traces and spans
requires that each trace record references the first span recorded
locally in the 'spans' table.

This fix ensures such a reference is always in place whether or
not a traceparent is passed in.

Signed-off-by: Gordon Sim <gsim@redhat.com>
This commit is contained in:
Gordon Sim 2025-06-23 16:36:51 +01:00
parent dbdc811d16
commit 7a2ecb691f
2 changed files with 13 additions and 2 deletions

View file

@ -14,6 +14,8 @@ from opentelemetry.sdk.trace import SpanProcessor
from opentelemetry.trace import Span
from opentelemetry.trace.span import format_span_id, format_trace_id
from llama_stack.providers.utils.telemetry.tracing import LOCAL_ROOT_SPAN_MARKER
class SQLiteSpanProcessor(SpanProcessor):
def __init__(self, conn_string):
@ -124,7 +126,7 @@ class SQLiteSpanProcessor(SpanProcessor):
(
trace_id,
service_name,
(span_id if span.attributes.get("__root_span__") == "true" else None),
(span_id if span.attributes.get(LOCAL_ROOT_SPAN_MARKER) else None),
datetime.fromtimestamp(span.start_time / 1e9, UTC).isoformat(),
datetime.fromtimestamp(span.end_time / 1e9, UTC).isoformat(),
),