Skip to content

[WO-1570] Add tracing.withActiveSpanContext() - #8562

Draft
jmorrell-cloudflare wants to merge 1 commit into
jmorrell/span-trace-contextfrom
jmorrell/with-active-span-context-v2
Draft

jmorrell-cloudflare wants to merge 1 commit into
jmorrell/span-trace-contextfrom
jmorrell/with-active-span-context-v2

Conversation

@jmorrell-cloudflare

@jmorrell-cloudflare jmorrell-cloudflare commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Adds tracing.withActiveSpanContext(spanContext, callback, ...args), which runs a callback with a span context from somewhere else as the active parent, for example one read from a queue message or an MCP request's _meta. It's the runtime equivalent of OpenTelemetry's context.with(trace.setSpanContext(ctx, spanContext), fn), which OpenTelemetry instrumentations use to continue a trace they've extracted:

await tracing.withActiveSpanContext(message.body.trace, async () => {
  tracing.getActiveSpan().spanContext(); // message.body.trace
  await fetch(url); // a child of that span, in its trace
});

As in OpenTelemetry, getActiveSpan() returns a non-recording span with that context, and new user spans and built-in instrumentation become its children, in its trace. With undefined or an invalid context there is no parent, so each span that would otherwise be top-level starts a new trace. Input is lenient in the same way as OpenTelemetry: bad IDs make the context invalid, and a missing or out-of-range traceFlags counts as 0.

One difference from OpenTelemetry: spans under a context whose trace flags say unsampled are still recorded, as they already are under an unsampled incoming request.

Like startSpan(), getActiveSpan() and withActiveSpan(), this isn't behind a compat flag.

Stacked on #8561. The internal build needs the matching jmorrell/with-active-span-context-v2 edgeworker branch.

@codecov-commenter

codecov-commenter commented Nov 18, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 26.08696% with 85 lines in your changes missing coverage. Please review.
✅ Project coverage is 66.44%. Comparing base (eb4a1ef) to head (6a2b2c6).

Files with missing lines Patch % Lines
src/workerd/api/tracing.c++ 5.68% 83 Missing ⚠️
src/workerd/api/tracing.h 83.33% 0 Missing and 1 partial ⚠️
src/workerd/io/io-context.c++ 94.11% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@                       Coverage Diff                       @@
##           jmorrell/span-trace-context    #8562      +/-   ##
===============================================================
- Coverage                        66.47%   66.44%   -0.03%     
===============================================================
  Files                              798      798              
  Lines                           204111   204209      +98     
  Branches                         31694    31712      +18     
===============================================================
+ Hits                            135680   135690      +10     
- Misses                           49858    49942      +84     
- Partials                         18573    18577       +4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Adds `tracing.withActiveSpanContext(spanContext | undefined, callback, ...args)`,
which runs the callback with a span context from elsewhere, such as one read
from a queue message, as the active parent. It's the runtime equivalent of
OpenTelemetry's `context.with(trace.setSpanContext(ctx, spanContext), fn)`:

```js
await tracing.withActiveSpanContext(message.body.trace, async () => {
  tracing.getActiveSpan().spanContext(); // message.body.trace
  await fetch(url); // a child of that span, in its trace
});
```

As in OpenTelemetry, getActiveSpan() returns a non-recording span with that
context, and new user spans and built-in instrumentation are its children, in
its trace. They are recorded even if its trace flags say unsampled, as spans
are today under an unsampled incoming request. With undefined or an invalid
context there is no parent: each span that would otherwise have none starts a
new trace, including a fetch() with no span around it.

Like startSpan(), getActiveSpan() and withActiveSpan(), this is not behind a
compat flag.
@jmorrell-cloudflare
jmorrell-cloudflare force-pushed the jmorrell/span-trace-context branch from aebe418 to eb4a1ef Compare November 18, 2026 01:17
@jmorrell-cloudflare
jmorrell-cloudflare force-pushed the jmorrell/with-active-span-context-v2 branch from 88a33dd to 6a2b2c6 Compare November 18, 2026 01:17
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.

2 participants