Skip to content

Commit b0ca847

Browse files
committed
Fix OTEL trace_id seeding: use is_remote=True and valid span_id
The OTEL SDK ignores NonRecordingSpan parents with INVALID_SPAN_ID. Using is_remote=True with a valid span_id makes start_span inherit the trace_id from the seeded parent context. Signed-off-by: Dor Zohar <dor@emcie.co>
1 parent d86213d commit b0ca847

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

src/parlant/adapters/modules/parlant_cloud.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,12 +243,15 @@ def span(
243243
trace_id_reset_token = self._trace_id.set(custom_trace_id)
244244

245245
# Inject Parlant trace_id into the OTEL context so the SDK uses
246-
# the same ID (UUID hex → 128-bit int).
246+
# the same ID (UUID hex → 128-bit int). Must use is_remote=True
247+
# and a valid span_id so the SDK treats it as a real parent.
248+
import random
249+
247250
otel_trace_id = int(custom_trace_id.replace("-", ""), 16)
248251
span_ctx = trace.SpanContext(
249252
trace_id=otel_trace_id,
250-
span_id=trace.INVALID_SPAN_ID,
251-
is_remote=False,
253+
span_id=random.getrandbits(64),
254+
is_remote=True,
252255
trace_flags=trace.TraceFlags(trace.TraceFlags.SAMPLED),
253256
)
254257
seeded_span = trace.NonRecordingSpan(span_ctx)

0 commit comments

Comments
 (0)