Skip to content

fix: create root span per prompt for OTEL trace isolation#36179

Open
josephwangrb wants to merge 2 commits into
anomalyco:devfrom
josephwangrb:fix/otel-trace-per-prompt
Open

fix: create root span per prompt for OTEL trace isolation#36179
josephwangrb wants to merge 2 commits into
anomalyco:devfrom
josephwangrb:fix/otel-trace-per-prompt

Conversation

@josephwangrb

@josephwangrb josephwangrb commented Jul 10, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #32920

Type of change

  • Bug fix

What does this PR do?

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 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.loop was defined as Effect.fn("SessionPrompt.loop"), which creates a child span of the ambient context — not a new root span. The Effect runtime's AsyncLocalStorageContextManager propagates the trace context from server boot, so every Effect.fn() call inherits the same trace ID.

Fix: Wrap SessionPrompt.loop in Effect.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.

// Before:
const loop = Effect.fn("SessionPrompt.loop")(function* (input) {
  return yield* state.ensureRunning(input.sessionID, lastAssistant(input.sessionID), runLoop(input.sessionID))
})

// After:
const loop = (input: LoopInput) =>
  state
    .ensureRunning(input.sessionID, lastAssistant(input.sessionID), runLoop(input.sessionID))
    .pipe(Effect.withSpan("SessionPrompt.loop", { root: true, attributes: { "session.id": input.sessionID } }))

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:

  • Prompt 1: trace 195697cf... (488 spans) with 2 SessionProcessor.process turns and ai.streamText.doStream carrying gen_ai.request.model=glm-5.2, token usage, and finish reason
  • Prompt 2: trace 82b2b0cd... (450 spans) with 2 turns, same GenAI attributes

Each SessionPrompt.loop span has parent=(none) confirming it's a root span. Typecheck passes (bun turbo typecheck — 30/30 packages).

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

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.
@github-actions github-actions Bot added needs:compliance This means the issue will auto-close after 2 hours. needs:issue labels Jul 10, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions

Copy link
Copy Markdown
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.

@github-actions github-actions Bot removed needs:issue needs:compliance This means the issue will auto-close after 2 hours. labels Jul 10, 2026
@github-actions

Copy link
Copy Markdown
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
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.

Prompt details missing from OTEL

1 participant