Skip to content

fix(observability): flush OTel spans on exit for opencode run - #30089

Closed
astefanutti wants to merge 1 commit into
anomalyco:devfrom
astefanutti:fix/otel-flush-on-exit
Closed

astefanutti wants to merge 1 commit into
anomalyco:devfrom
astefanutti:fix/otel-flush-on-exit

Conversation

@astefanutti

@astefanutti astefanutti commented May 31, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #30087

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

opencode run never emits OTel spans: process.exit() in index.ts kills the event loop before the BatchSpanProcessor flushes its buffer.

The embedded server and AppRuntime share a single, memoMap-memoized Observability layer — one BatchSpanProcessor referenced by two scopes — so a flush (NodeSdk.shutdown() → forceFlush()) only fires once both scopes are released. This PR disposes both at their ownership seams:

  • effectCmd disposes AppRuntime in its finally. This is the seam that already lazily imports the runtime, so index.ts never eagerly pulls in the full application graph on every invocation (--help/--version/completion included). This addresses the review feedback on the original index.ts approach.
  • run.ts disposes the embedded server after each local run mode, so the server stops holding the shared span processor open. Server.Default() now exposes the web handler's dispose().

This converges with the approach in the (now-closed) #39756.

How did you verify your code works?

Added test/cli/run/run-process.test.ts → "flushes server telemetry before exiting": spawns opencode run against a stub OTLP collector with a mocked (fast) LLM, and asserts the collector receives /v1/traces. The mock keeps the run short enough that the BatchSpanProcessor's periodic export timer never fires, so the test only passes if disposal forces the flush.

bun test test/cli/run/run-process.test.ts -t "flushes server telemetry before exiting"

Screenshots / recordings

Not a UI change.

Checklist

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

@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label May 31, 2026
@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label May 31, 2026
@github-actions

Copy link
Copy Markdown
Contributor

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

@yapnel

yapnel commented Jun 19, 2026

Copy link
Copy Markdown

When will this be merged into dev and release please?

@astefanutti
astefanutti force-pushed the fix/otel-flush-on-exit branch from 0efbdb0 to 1f42eeb Compare June 23, 2026 14:46
@github-actions

github-actions Bot commented Jul 1, 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.

@EmilienM

EmilienM commented Jul 7, 2026

Copy link
Copy Markdown

We need that PR, please reopen it.

@yapnel

yapnel commented Jul 7, 2026

Copy link
Copy Markdown

Same here! It fixes the bug

@paolovic

Copy link
Copy Markdown

Please, reopen it. We need that functionality

@lalit-aggarwal

Copy link
Copy Markdown

Please reopen! We need this urgently.

@mbartholet

Copy link
Copy Markdown

Very crucial functionality, we need this PR.

@Johona1924

Copy link
Copy Markdown

We need this PR asap, please reopen.

@rekram1-node

Copy link
Copy Markdown
Collaborator

will look sorry

@rekram1-node rekram1-node reopened this Jul 30, 2026

@opencode-agent opencode-agent Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The shutdown belongs at a lifecycle boundary, but this import is the wrong dependency direction for that boundary. index.ts runs for every invocation (including --help/--version), while app-runtime.ts statically pulls the full V1 AppLayer; the existing effectCmd code deliberately imports AppRuntime only when a command actually needs it. This change makes the entire application runtime eager just so the top-level finally can dispose it.

Please preserve lazy runtime initialization and dispose only a runtime that was actually acquired. For this specific path, the natural ownership seam is effectCmd, which imports and runs AppRuntime; alternatively expose a lightweight lifecycle/disposer registry that index.ts can call without importing app-runtime.ts. A dynamic import directly in this finally is not sufficient because it would still load the full runtime on exit for commands that never used it.

The flush itself is correct once disposal is attached to that ownership seam.

@astefanutti

Copy link
Copy Markdown
Author

@rekram1-node thanks, I've addressed the review and rebased it.

@astefanutti
astefanutti force-pushed the fix/otel-flush-on-exit branch from 038fcc3 to 7be1618 Compare July 31, 2026 09:37
@astefanutti astefanutti changed the title fix(observability): dispose AppRuntime before process.exit to flush OTel spans fix(observability): flush OTel spans on exit for opencode run Jul 31, 2026
@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label Jul 31, 2026
The BatchSpanProcessor buffers spans and exports them asynchronously.
process.exit() in index.ts kills the event loop before the buffer flushes,
so `opencode run` never emits traces (#30087).

The embedded server and AppRuntime share a single, memoMap-memoized
Observability layer (one BatchSpanProcessor referenced by two scopes), so
flushing requires releasing both:

- effectCmd disposes AppRuntime at the seam that lazily acquires it, so
  index.ts never eagerly imports the full application graph just to tear it
  down (addresses the review on #30089).
- run.ts disposes the embedded server after each local run mode, so the
  server stops holding the shared span processor open.

Server.Default() now exposes the web handler's dispose(). Adds a subprocess
test that runs against a stub OTLP collector with a mocked (fast) LLM, so the
flush must come from disposal rather than the processor's periodic export timer.

Because shutdown is now graceful, run also dedupes its terminal JSON error
record: a rejected prompt surfaces the same failure both synchronously
(session.prompt result.error) and via the session.error event drained by the
event loop; the old abrupt exit masked the second write.

Fixes #30087
@astefanutti
astefanutti force-pushed the fix/otel-flush-on-exit branch from 7be1618 to 0e3a4df Compare July 31, 2026 10:27
@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Jul 31, 2026
@github-actions

Copy link
Copy Markdown
Contributor

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

@paolovic

paolovic commented Aug 7, 2026

Copy link
Copy Markdown

can we have a review, please?

@thdxr

thdxr commented Oct 10, 2026

Copy link
Copy Markdown
Member

This branch has been upgraded to V2. We're no longer taking PRs for V1. If you think this is still relevant, please port it over and open it for V2.

— from 𝕺𝖕𝖊𝖓𝕮𝖔𝖉𝖊

@thdxr thdxr closed this Oct 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

opencode run: OTel spans lost because AppRuntime is not disposed before process.exit()

9 participants