opentelemetry-js/packages/opentelemetry-context-async...
Marc Pichler 7fde94081e
chore: prepare release 2.0.0/0.200.0 (#5521)
2025-03-17 15:40:53 +00:00
..
src refactor(context-async-hooks): use explicit exports (#4786) 2024-06-12 18:49:08 +00:00
test chore: update `module` config in tsconfig.base.json (#5347) 2025-01-23 10:35:36 +00:00
.eslintignore fix(lint): move tslint to eslint (#892) 2020-05-20 20:42:17 -07:00
.eslintrc.js chore(deps): update dependency eslint to v8.43.0 (#3929) 2023-07-06 15:14:56 +02:00
.npmignore Rename scope to context (#853) 2020-03-13 15:01:07 -04:00
LICENSE Rename scope to context (#853) 2020-03-13 15:01:07 -04:00
README.md chore(lint): refactor Markdown linting to use markdownlint-cli2 (#4713) 2024-05-21 14:44:23 +02:00
package.json chore: prepare release 2.0.0/0.200.0 (#5521) 2025-03-17 15:40:53 +00:00
tsconfig.json chore: automatically generate tsconfigs (#3432) 2022-11-23 23:34:41 +08:00

README.md

OpenTelemetry async_hooks-based Context Managers

NPM Published Version Apache License

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:

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:

License

Apache 2.0 - See LICENSE for more information.