Skip to content

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

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

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>
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.

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

1 participant