Skip to content

Commit c37fa7e

Browse files
authored
Propagate OTEL trace id across client-workflow boundary (#1601)
The OTEL-aware OpenAI Agents interceptor propagated and seeded the OTEL span id but never the trace id, so the client-side root span and the workflow's reconstructed spans ended up in different traces. This made test_sdk_trace_to_otel_span_parenting flaky. Wire up the existing seed_trace_id so the workflow's root span reuses the caller's trace id.
1 parent f129be7 commit c37fa7e

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

temporalio/contrib/openai_agents/_otel_trace_interceptor.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,11 @@ def header_contents(self) -> dict[str, Any]:
4545
otel_span = opentelemetry.trace.get_current_span()
4646

4747
if otel_span and otel_span.get_span_context().is_valid:
48-
otel_span_id = otel_span.get_span_context().span_id
48+
span_context = otel_span.get_span_context()
4949
return {
5050
**super().header_contents(),
51-
"otelSpanId": otel_span_id,
51+
"otelSpanId": span_context.span_id,
52+
"otelTraceId": span_context.trace_id,
5253
}
5354
else:
5455
return super().header_contents()
@@ -63,6 +64,12 @@ def context_from_header(
6364
if span_info is None:
6465
return
6566
otel_span_id = span_info.get("otelSpanId")
67+
otel_trace_id = span_info.get("otelTraceId")
68+
69+
# Seed the trace id before the trace is reconstructed so the workflow's root
70+
# OTEL span shares the caller's trace id rather than generating a new one.
71+
if otel_trace_id and self._otel_id_generator:
72+
self._otel_id_generator.seed_trace_id(otel_trace_id)
6673

6774
# If only a trace was propagated from the caller, we need to seed for trace context
6875
if otel_span_id and self._otel_id_generator and span_info.get("spanId") is None:

0 commit comments

Comments
 (0)