Skip to content

feat(observability): propagate trace context to spawned subprocesses#27085

Closed
jlguerreiro wants to merge 1 commit into
anomalyco:devfrom
jlguerreiro:feat/traceparent-tool-subprocesses
Closed

feat(observability): propagate trace context to spawned subprocesses#27085
jlguerreiro wants to merge 1 commit into
anomalyco:devfrom
jlguerreiro:feat/traceparent-tool-subprocesses

Conversation

@jlguerreiro

Copy link
Copy Markdown

Issue for this PR

Closes #27031

Type of change

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

What does this PR do?

When OpenCode spawns a tool subprocess (shell tool, stdio MCP transports, LSP servers, etc.), inject the active OTel context as TRACEPARENT (and TRACESTATE when non-empty) per the OTel env-carrier spec. Subprocess instrumentation that follows the spec then chains into OpenCode's invocation trace instead of starting a disconnected root.

How did you verify your code works?

Unit tests — added in packages/core/test/effect/observability.test.ts covering: no-op without active span, TRACEPARENT injection from a given context, sampled vs. unsampled flag encoding, invalid span
context, stale TRACEPARENT overwrite, default-arg fallback.

End-to-end check

Ran opencode with OTel enabled, prompted it to spawn an OTel-instrumented child via the shell tool, and confirmed that the child's span sits on the same trace and lists OpenCode's ShellTool.run as its parent. That parent_span_id match across process boundaries.

Script used for the E2E check

import { context, ROOT_CONTEXT, trace, TraceFlags } from "@opentelemetry/api"
import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node"
import { SimpleSpanProcessor } from "@opentelemetry/sdk-trace-base"
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http"

process.on("unhandledRejection", (err) => console.error("[child] export error:", err?.message ?? err))

function parentContextFromEnv() {
  const tp = process.env.TRACEPARENT
  if (!tp) return ROOT_CONTEXT
  const [, traceId, spanId, flags] = tp.split("-")
  return trace.setSpanContext(ROOT_CONTEXT, {
    traceId, spanId,
    traceFlags: Number.parseInt(flags, 16) || TraceFlags.NONE,
    isRemote: true,
  })
}

const provider = new NodeTracerProvider({
  spanProcessors: [new SimpleSpanProcessor(new OTLPTraceExporter())],
  const [, traceId, spanId, flags] = tp.split("-")
  return trace.setSpanContext(ROOT_CONTEXT, {
    traceId, spanId,
    traceFlags: Number.parseInt(flags, 16) || TraceFlags.NONE,
    isRemote: true,
  })
}

const provider = new NodeTracerProvider({
  spanProcessors: [new SimpleSpanProcessor(new OTLPTraceExporter())],
})
provider.register()

await context.with(parentContextFromEnv(), async () => {
  await new Promise((resolve) => {
    trace.getTracer("otel-subprocess-test").startActiveSpan("subprocess-work", (span) => {
      span.setAttribute("test.tag", "subprocess-propagation")
      setTimeout(() => { span.end(); resolve() }, 50)
    })
  })
})
try { await provider.shutdown() } catch {}

Checklist

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

When opencode spawns a tool subprocess (shell tool, stdio MCP transports,
LSP servers, etc.), inject the active OTel context as TRACEPARENT (and
TRACESTATE when non-empty) per the OTel env-carrier spec. Subprocess
instrumentation that follows the spec then chains into opencode's
invocation trace instead of starting a disconnected root.

Closes anomalyco#27031.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@github-actions

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.

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.

[FEATURE]: propagate outgoing trace context to spawned tool subprocesses

1 participant