feat(trace): support remote W3C parent context#57
Conversation
Add OPENCODE_TRACEPARENT and OPENCODE_TRACESTATE config for parenting plugin spans under an external W3C trace context. Parse trace context with the OpenTelemetry W3C propagator, thread the resulting root context through HandlerContext, and use it when creating session, LLM, and tool spans. Document the parent-based sampler caveat where an unsampled remote parent suppresses span export, and cover parsing plus parentage behavior with tests.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (12)
📝 WalkthroughWalkthroughThis PR adds W3C Trace Context propagation for OpenTelemetry by extracting remote parent span context from environment variables, storing it in the handler context, and updating all span creation to use this stored context instead of the active context, enabling distributed tracing with upstream systems. ChangesW3C Trace Context Remote Parent Propagation
Sequence Diagram(s)sequenceDiagram
participant Remote as Upstream System
participant Plugin as Plugin (index.ts)
participant Config as loadConfig
participant Propagator as W3CTraceContextPropagator
participant Handler as Message/Session Handler
participant Span as OpenTelemetry Span
Remote->>Plugin: HTTP with traceparent/tracestate headers
Plugin->>Config: Load env vars OPENCODE_TRACEPARENT, OPENCODE_TRACESTATE
Config-->>Plugin: traceparent, tracestate values
Plugin->>Propagator: Extract remote parent context
Propagator-->>Plugin: Remote parent Context or undefined
Plugin->>Plugin: Store rootContext in ctx
Handler->>Handler: Create child span
Handler->>Span: Use ctx.rootContext() with setSpan(parentCtx, sessionSpan)
Span-->>Handler: Child span parented to remote context
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Description
Add support for parenting plugin spans under an external W3C trace context.
This adds
OPENCODE_TRACEPARENTandOPENCODE_TRACESTATE, parses them with OpenTelemetry’s W3C trace-context propagator, threads the resulting root context throughHandlerContext, and uses it when creating session, LLM, and tool spans.The README documents the default parent-based sampler behavior where a valid unsampled remote parent (
...-00) suppresses span export by design.Type of change
Checklist
bun run lintpasses with no errorsbun run check:jsdoc-coveragepasses with no errorsbun run typecheckpasses with no errorsbun testpasses with no errorsRelated issues
None.
Additional context
This intentionally uses
OPENCODE_-prefixed environment variables rather than automatically reading bareTRACEPARENT/TRACESTATE, keeping plugin configuration explicit and consistent with the rest of the project.With the default
ParentBasedsampler, passing atraceparentwith the sampled flag off (...-00) causes spans to be non-recording and suppresses trace export. This is expected OpenTelemetry behavior and is documented in the README.Summary by CodeRabbit
New Features
OPENCODE_TRACEPARENTandOPENCODE_TRACESTATEenvironment variables.Documentation
Dependencies