Skip to content

Propagate OTEL trace id across client-workflow boundary#1601

Open
brianstrauch wants to merge 1 commit into
mainfrom
fix-otel-trace-id-propagation
Open

Propagate OTEL trace id across client-workflow boundary#1601
brianstrauch wants to merge 1 commit into
mainfrom
fix-otel-trace-id-propagation

Conversation

@brianstrauch

Copy link
Copy Markdown
Member

What

The OTEL-aware OpenAI Agents interceptor (_otel_trace_interceptor.py) propagated and seeded the OTEL span id across the client→workflow boundary (otelSpanIdseed_span_id), but never propagated the trace id. As a result the client-side "Client SDK trace" root span (created outside a workflow, so it gets a random trace id) and the workflow's reconstructed spans (created inside a workflow, so they get a deterministic workflow-random trace id) ended up in different traces.

This made test_sdk_trace_to_otel_span_parenting flaky: its "all spans belong to the same trace" assertion only failed when context-var timing caused the client-side root span to be exported and picked by next(...), which is why it was intermittent on CI (example failure).

Fix

Wire up the already-present-but-unused seed_trace_id:

  • header_contents: also emit otelTraceId from the current span's context.
  • context_from_header: call seed_trace_id(otel_trace_id) before the trace is reconstructed, so the workflow's root OTEL span reuses the caller's trace id.

Now every span shares one trace id regardless of which root span the test selects — fixing the flake at its source rather than in the test.

Testing

tests/contrib/openai_agents/test_openai_tracing.py passes (7/7), including the workflow→activity span-propagation cases. Ran the target test repeatedly to confirm stability.

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.
@brianstrauch brianstrauch requested review from a team as code owners June 15, 2026 19:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant