@opentelemetry/context-async-hooks
v1.28.0
Published
OpenTelemetry AsyncHooks-based Context Manager
Downloads
21,669,317
Readme
OpenTelemetry async_hooks-based Context Managers
This package provides two ContextManager
implementations built on APIs from Node.js's async_hooks
module. If you're looking for a ContextManager
to use in browser environments, consider opentelemetry-context-zone or opentelemetry-context-zone-peer-dep.
The definition of the ContextManager
interface and the problem it solves can be found here.
API
Two ContextManager
implementations are exported:
AsyncLocalStorageContextManager
, based onAsyncLocalStorage
AsyncHooksContextManager
, based onAsyncHook
The former should be preferred over the latter as its implementation is substantially simpler than the latter's, and according to Node.js docs,
While you can create your own implementation [of
AsyncLocalStorage
] on top of [AsyncHook
],AsyncLocalStorage
should be preferred as it is a performant and memory safe implementation that involves significant optimizations that are non-obvious to implement.
AsyncLocalStorage
is available in node ^12.17.0 || >= 13.10.0
, however AsyncLocalStorageContextManager
is not enabled by default for node <14.8.0
because of some important bugfixes which were introduced in v14.8.0
(e.g., nodejs/node#34573).
Limitations
It's possible that this package won't track context perfectly when used with certain packages. In particular, it inherits any bugs present in async_hooks. See here for known issues.
async_hooks is still seeing significant correctness and performance fixes, it's recommended to run the latest Node.js LTS release to benefit from said fixes.
Prior art
Context propagation is a big subject when talking about tracing in Node.js. If you want more information about it here are some resources:
- https://www.npmjs.com/package/continuation-local-storage (which was the old way of doing context propagation)
- Datadog's own implementation for their JavaScript tracer: here
- OpenTracing implementation: here
- Discussion about context propagation by the Node.js Diagnostics Working Group: here
Useful links
- For more information on OpenTelemetry, visit: https://opentelemetry.io/
- For more about OpenTelemetry JavaScript: https://github.com/open-telemetry/opentelemetry-js
- For help or feedback on this project, join us in GitHub Discussions
License
Apache 2.0 - See LICENSE for more information.