Repository navigation
[WO-1570] Add tracing.withActiveSpanContext() - #8562
Draft
jmorrell-cloudflare wants to merge 1 commit into
Draft
jmorrell-cloudflare wants to merge 1 commit into
jmorrell-cloudflare wants to merge 1 commit into
Conversation
Codecov Report❌ Patch coverage is 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. 🚀 New features to boost your workflow:
|
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
force-pushed
the
jmorrell/span-trace-context
branch
from
November 18, 2026 01:17
aebe418 to
eb4a1ef
Compare
jmorrell-cloudflare
force-pushed
the
jmorrell/with-active-span-context-v2
branch
from
November 18, 2026 01:17
88a33dd to
6a2b2c6
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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'scontext.with(trace.setSpanContext(ctx, spanContext), fn), which OpenTelemetry instrumentations use to continue a trace they've extracted: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. Withundefinedor 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-rangetraceFlagscounts 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()andwithActiveSpan(), this isn't behind a compat flag.Stacked on #8561. The internal build needs the matching
jmorrell/with-active-span-context-v2edgeworker branch.