fix: create root span per prompt for OTEL trace isolation#36179
Open
josephwangrb wants to merge 2 commits into
Open
fix: create root span per prompt for OTEL trace isolation#36179josephwangrb wants to merge 2 commits into
josephwangrb wants to merge 2 commits into
Conversation
When OTEL_EXPORTER_OTLP_ENDPOINT is set, all spans across all prompts
in a session inherit the server's boot-time trace context, producing
one giant trace per session instead of one trace per prompt.
This wraps SessionPrompt.loop in Effect.withSpan with { root: true }
so each prompt starts a new trace. All child spans (SessionProcessor.process,
LLM.run, ai.streamText, tool execution) then inherit the new trace ID,
making traces queryable per-prompt in backends like Jaeger/Tempo.
The session.id attribute is added to the root span for correlation.
Contributor
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
Contributor
|
The following comment was made by an LLM, it may be inaccurate: Found a potentially related PR: Related PR:
The other result (#5245 — "feat: integrate OpenTelemetry") appears to be an older PR and may not be directly relevant to the current span isolation fix. |
Contributor
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
Per the OpenTelemetry GenAI semantic conventions, the root span for an agent invocation should be named 'invoke_agent' with gen_ai.operation.name and gen_ai.conversation.id attributes. https://github.com/open-telemetry/semantic-conventions-genai/blob/main/docs/gen-ai/gen-ai-agent-spans.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue for this PR
Closes #32920
Type of change
What does this PR do?
When
OTEL_EXPORTER_OTLP_ENDPOINTis set, all spans across all prompts in a session inherit the server's boot-time trace context, producing one giant trace per session instead of one trace per prompt. This also causes the<trace-without-root-span>issue reported in #32920 — there's no root span boundary per prompt, so backends like Jaeger can't group spans into meaningful traces.Root cause:
SessionPrompt.loopwas defined asEffect.fn("SessionPrompt.loop"), which creates a child span of the ambient context — not a new root span. The Effect runtime'sAsyncLocalStorageContextManagerpropagates the trace context from server boot, so everyEffect.fn()call inherits the same trace ID.Fix: Wrap
SessionPrompt.loopinEffect.withSpan("SessionPrompt.loop", { root: true })so each prompt starts a new trace. All child spans (SessionProcessor.process,LLM.run,ai.streamText, tool execution) then inherit the new trace ID.The
{ root: true }option tells Effect's tracer to create a new root span (new trace ID) instead of parenting to the ambient context.How did you verify your code works?
Built opencode with the fix and tested via
opencode serve+ SDK with 2 prompts (4 turns total) against a local OTel collector:Before — 1 trace for entire session (1429 spans, 30.4s), all turns in one trace.
After — 1 trace per prompt:
195697cf...(488 spans) with 2SessionProcessor.processturns andai.streamText.doStreamcarryinggen_ai.request.model=glm-5.2, token usage, and finish reason82b2b0cd...(450 spans) with 2 turns, same GenAI attributesEach
SessionPrompt.loopspan hasparent=(none)confirming it's a root span. Typecheck passes (bun turbo typecheck— 30/30 packages).Checklist