GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix: create root span per prompt for OTEL trace isolation by josephwangrb · Pull Request #36179 · anomalyco/opencode

Conversation

@josephwangrb

@josephwangrb josephwangrb commented Jul 10, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

jesse-schein reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@josephwangrb
fix: create root span per prompt for OTEL trace isolation
2685c0d
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

github-actions Bot commented Jul 10, 2026

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions

github-actions Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

Found a potentially related PR:

Related PR:

  • feat(observability): add v2 genai tracing #35935 — feat(observability): add v2 genai tracing
    • Related because it addresses observability and GenAI tracing, which overlaps with the trace span management improvements in PR fix: create root span per prompt for OTEL trace isolation #36179. Depending on the scope of feat(observability): add v2 genai tracing #35935, it may have similar trace context handling logic that should be coordinated.

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@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

github-actions Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@josephwangrb
fix: use GenAI semantic convention for root span name and attributes
a8a91d6
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
@github-actions github-actions Bot mentioned this pull request Jul 10, 2026
📊 AI CLI 工具社区动态日报 2026-07-10 litang9/big_model_radar#184
Open
@CreatorGhost CreatorGhost mentioned this pull request Jul 11, 2026
fix(opencode): create root span per prompt for OTEL trace isolation CreatorGhost/TheCode#31
Merged
CreatorGhost added a commit to CreatorGhost/TheCode that referenced this pull request Jul 12, 2026
@CreatorGhost
fix(opencode): create root span per prompt for OTEL trace isolation
811a590
Ported from upstream anomalyco#36179.
CreatorGhost added a commit to CreatorGhost/TheCode that referenced this pull request Jul 12, 2026
@CreatorGhost
fix(opencode): create root span per prompt for OTEL trace isolation (#31
d47c3b7
)

* fix(opencode): create root span per prompt for OTEL trace isolation

Ported from upstream anomalyco#36179.

* fix(opencode): attach invoke_agent root span to forked work

Addresses Opus review of PR #31: the root span wrapped the ensureRunning caller,
but ensureRunning may store work and start it later from a prior run's fiber, so
queued/steered prompts got child spans mis-parented under the previous prompt's
trace and the span measured queue-wait instead of the agent turn. Wrap runLoop
(the forked work) instead, and keep the named SessionPrompt.loop fn.
@fro-bot fro-bot mentioned this pull request Jul 14, 2026
build(harness): retire low-value OpenCode carries fro-bot/agent#1215
Merged
@josephwangrb
Merge branch 'dev' into fix/otel-trace-per-prompt
c6c84be
@josephwangrb
josephwangrb marked this pull request as draft July 14, 2026 18:21
@josephwangrb
josephwangrb marked this pull request as ready for review July 14, 2026 18:21
@josephwangrb

josephwangrb commented Jul 14, 2026

Copy link
Copy Markdown
Author

Could I get a review on this bug fix? It is unblocking the observability for OpenCode, as the current trace structure isn't usable.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jesse-schein

jesse-schein commented Jul 28, 2026

Copy link
Copy Markdown

I think most effort is going into v2 for otel, check out this PR and other commits.

#35935

in v2 branch

Date Commit What
07-05 c3d26c4912 feat(core): improve runtime observability
07-05 08741f6b93 fix(core): scope observability to server
07-05 baacb2e776 fix(core): tolerate invalid OTLP configuration
07-17 331533deff fix(cli): isolate server request traces (#37395) — the inbound traceparent extraction
07-20 44b6938b2a removed packages/opencode
07-21 e0810753f2 refactor: extract shared util package (#37828) — observability moved to packages/util
07-21 9c38358197 refactor: centralize client identity (#38148)
07-21 8de40be6ea feat(plugin): expose app metadata (#38179)
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@josephwangrb

josephwangrb commented Jul 28, 2026

Copy link
Copy Markdown
Author

@jesse-schein this is pretty cool! Can't wait to see it merged!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions

github-actions Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot closed this Aug 10, 2026
@github-actions github-actions Bot added the automated-pr-cleanup label Aug 10, 2026
@pragmaticivan pragmaticivan mentioned this pull request Sep 7, 2026
[FEATURE]: Add stable agent lifecycle telemetry #47701
Open
1 task done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

automated-pr-cleanup

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Prompt details missing from OTEL

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@josephwangrb @jesse-schein
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.