Skip to content

feat: prototype context attach/detach#6387

Draft
pichlermarc wants to merge 4 commits intoopen-telemetry:mainfrom
dynatrace-oss-contrib:feat/context-attach-detach-tracing-channel
Draft

feat: prototype context attach/detach#6387
pichlermarc wants to merge 4 commits intoopen-telemetry:mainfrom
dynatrace-oss-contrib:feat/context-attach-detach-tracing-channel

Conversation

@pichlermarc
Copy link
Member

@pichlermarc pichlermarc commented Feb 9, 2026

Important

While this PR is intended to show possible use of attach/detach functionality for tracingChannel instrumentation, as an alternative to proposal #6088, It does not feature a production ready implementation of it
It's intended as a means to demonstrate how I expect this could be used to accomplish that use case.

Which problem is this PR solving?

Proposal #6088 is to allow accessing the internal AsyncLocalStorage from the outside, for the purpose of binding it to a TracingChannel, so that context propagation works properly. While this is works in practice it has a few downsides:

  • the internal AsyncLocalStorage is internal to avoid tampering, misuse can lead to catastrophic effects and can be hard to debug
  • not every ContextManager is a AsyncLocalStorageContextManager, so many do not have an AsyncLocalStorage. As such, we also cannot add it to the @opentelemetry/api package. Further AsyncLocalStorage is a Node.js type, and the @opentelemetry/api and its types must remain compatible with the Browser

My counter-proposal is implementing the spec-defined attach() and detach() operations to achieve a similar effect. This works by manually attaching context when the TracingChannel emits its events, storing the state on the message, and detaching the context once the end message is received.

This implementation:

  • is specification compliant (we don't add any additional operations that don't exist in the spec)
  • is runtime agnostic (these operations may be implemented however it makes sense for the ContextManager)
  • (possibly) could open up other use-cases that we currently did not think of

The downsides:

  • slightly more boilerplate code (I think most of it can be wrapped in utility functions so this may be acceptable)
  • similar to exposing AsyncLocalStorage, context attach/detach can have potentially catastrophic effects (however, I think it's easier to document that than with exposing AsyncLocalStorage and other language SIGs have been managing fine with this operation available)

You can find an example how tracing channel instrumentation would work at examples/tracing-channels.

Previous work on context attach/detach

@codecov
Copy link

codecov bot commented Feb 9, 2026

Codecov Report

❌ Patch coverage is 63.63636% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 95.47%. Comparing base (819ebb2) to head (b9fbb3f).
⚠️ Report is 9 commits behind head on main.

Files with missing lines Patch % Lines
api/src/api/context.ts 0.00% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6387      +/-   ##
==========================================
- Coverage   95.50%   95.47%   -0.03%     
==========================================
  Files         365      365              
  Lines       11606    11627      +21     
  Branches     2676     2683       +7     
==========================================
+ Hits        11084    11101      +17     
- Misses        522      526       +4     
Files with missing lines Coverage Δ
...async-hooks/src/AsyncLocalStorageContextManager.ts 100.00% <100.00%> (ø)
api/src/api/context.ts 78.57% <0.00%> (-13.10%) ⬇️

... and 4 files with indirect coverage changes

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

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.

1 participant

Comments