@opentelemetry/context-async-hooks
OpenTelemetry AsyncHooks-based Context Manager
Last updated 10 months ago by GitHub Actions .
Apache-2.0 · Repository · Bugs · Original npm · Tarball · package.json
$ npm install @opentelemetry/context-async-hooks 
SYNC missed versions from official npm registry.

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][async-hooks-doc] module. If you're looking for a ContextManager to use in browser environments, consider opentelemetry-context-zone or opentelemetry-context-zone-peer-dep.

See the definition of the ContextManager interface and the problem it solves.

API

Two ContextManager implementations are exported:

  • AsyncLocalStorageContextManager, based on AsyncLocalStorage
  • AsyncHooksContextManager, based on AsyncHook. This is deprecated and will be removed in v3 (planned for mid-2025). AsyncLocalStorage is simpler, faster, available in Node.js v14.8.0 and later, and avoids this possible DoS vulnerability.

Using attach()

AsyncLocalStorageContextManager supports the optional attach() method for imperative context management. It sets a context as active and returns a disposable Token. Disposing the token restores the previously active context.

What is an "execution unit"? In Node.js, this refers to the current asynchronous execution chain - the currently running code plus all async operations that will be spawned from it (Promises, callbacks, async/await, timers, etc.). When you attach() a context, it becomes active for this entire chain until the token is disposed.

import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';
import { ROOT_CONTEXT } from '@opentelemetry/api';

const contextManager = new AsyncLocalStorageContextManager();
contextManager.enable();

const myContext = ROOT_CONTEXT.setValue('key', 'value');

// Attach a context - it becomes active for the current async execution chain
const token = contextManager.attach(myContext);
console.log(contextManager.active()); // myContext

// Context propagates to async operations automatically
await someAsyncOperation();
console.log(contextManager.active()); // still myContext

// Restore the previous context by disposing the token
token.dispose();
console.log(contextManager.active()); // ROOT_CONTEXT

Important: You should ensure that every attach() call has a corresponding token.dispose() call to avoid context leaks. Use a try/finally block for safety:

const token = contextManager.attach(myContext);
try {
  await doWork();
} finally {
  token.dispose(); // Always restore
}

For most use cases, prefer using with() as it automatically handles context restoration. Not restoring context properly can lead to catastrophic effects on your telemetry, even outside the bounds of your instrumentation library.

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:

Useful links

License

Apache 2.0 - See LICENSE for more information.

Current Tags

  • 3.0.0-rc.0                                ...           canary (a month ago)
  • 3.1.0                                ...           latest (22 days ago)
  • 1.8.0                                ...           next (4 years ago)

149 Versions

  • 3.1.0                                ...           22 days ago
  • 3.0.0                                ...           a month ago
  • 3.0.0-rc.0                                ...           a month ago
  • 2.12.0                                ...           2 months ago
  • 3.0.0-development.1                                ...           2 months ago
  • 3.0.0-development.0                                ...           2 months ago
  • 2.11.0                                ...           3 months ago
  • 2.11.0-development.0                                ...           3 months ago
  • 2.10.0                                ...           4 months ago
  • 2.9.0                                ...           5 months ago
  • 2.8.0                                ...           6 months ago
  • 2.7.1                                ...           7 months ago
  • 2.7.0                                ...           7 months ago
  • 2.6.1                                ...           8 months ago
  • 2.6.0                                ...           9 months ago
  • 2.5.1                                ...           9 months ago
  • 2.5.0                                ...           10 months ago
  • 2.4.0                                ...           10 months ago
  • 2.3.0                                ...           a year ago
  • 2.2.0                                ...           a year ago
  • 2.1.0                                ...           a year ago
  • 2.0.1                                ...           2 years ago
  • 2.0.0                                ...           2 years ago
  • 2.0.0-rc.1                                ...           2 years ago
  • 2.0.0-dev.1                                ...           2 years ago
  • 2.0.0-dev.0                                ...           2 years ago
  • 1.30.1                                ...           2 years ago
  • 1.30.0                                ...           2 years ago
  • 1.29.0                                ...           2 years ago
  • 1.28.0                                ...           2 years ago
  • 1.27.0                                ...           2 years ago
  • 1.26.0                                ...           2 years ago
  • 1.25.1                                ...           2 years ago
  • 1.25.0                                ...           2 years ago
  • 1.24.1                                ...           3 years ago
  • 1.24.0                                ...           3 years ago
  • 1.23.0                                ...           3 years ago
  • 1.22.0                                ...           3 years ago
  • 1.21.0                                ...           3 years ago
  • 1.20.0                                ...           3 years ago
  • 1.19.0                                ...           3 years ago
  • 1.18.1                                ...           3 years ago
  • 1.18.0                                ...           3 years ago
  • 1.17.1                                ...           3 years ago
  • 1.17.0                                ...           3 years ago
  • 1.16.0                                ...           3 years ago
  • 1.15.2                                ...           3 years ago
  • 1.15.1                                ...           3 years ago
  • 1.15.0                                ...           3 years ago
  • 1.14.0                                ...           3 years ago
  • 1.13.0                                ...           4 years ago
  • 1.12.0                                ...           4 years ago
  • 1.11.0                                ...           4 years ago
  • 1.10.1                                ...           4 years ago
  • 1.10.0                                ...           4 years ago
  • 1.9.1                                ...           4 years ago
  • 1.9.0                                ...           4 years ago
  • 1.8.0                                ...           4 years ago
  • 1.7.0                                ...           4 years ago
  • 1.6.0                                ...           4 years ago
  • 1.5.0                                ...           4 years ago
  • 1.4.0                                ...           4 years ago
  • 1.3.1                                ...           4 years ago
  • 1.3.0                                ...           5 years ago
  • 1.2.0                                ...           5 years ago
  • 1.1.1                                ...           5 years ago
  • 1.1.0                                ...           5 years ago
  • 1.0.1                                ...           5 years ago
  • 1.0.0                                ...           5 years ago
  • 0.26.0                                ...           5 years ago
  • 0.25.1-alpha.13                                ...           5 years ago
  • 0.25.1-alpha.4                                ...           5 years ago
  • 0.25.1-alpha.1                                ...           5 years ago
  • 0.25.0                                ...           5 years ago
  • 0.25.1-alpha.21                                ...           5 years ago
  • 0.24.1-alpha.5                                ...           5 years ago
  • 0.24.1-alpha.1                                ...           5 years ago
  • 0.24.0                                ...           5 years ago
  • 0.24.1-alpha.31                                ...           5 years ago
  • 0.23.1-alpha.23                                ...           5 years ago
  • 0.23.1-alpha.15                                ...           5 years ago
  • 0.23.1-alpha.3                                ...           5 years ago
  • 0.23.1-alpha.18                                ...           5 years ago
  • 0.23.0                                ...           5 years ago
  • 0.23.1-alpha.17                                ...           5 years ago
  • 0.22.1-alpha.16                                ...           5 years ago
  • 0.22.1-alpha.14                                ...           5 years ago
  • 0.22.1-alpha.13                                ...           5 years ago
  • 0.22.1-alpha.8                                ...           5 years ago
  • 0.22.0                                ...           5 years ago
  • 0.22.1-alpha.5                                ...           5 years ago
  • 0.21.1-alpha.1                                ...           5 years ago
  • 0.21.0                                ...           5 years ago
  • 0.21.1-alpha.7                                ...           5 years ago
  • 0.20.1-alpha.4                                ...           5 years ago
  • 0.20.1-alpha.3                                ...           5 years ago
  • 0.20.0                                ...           5 years ago
  • 0.20.1-alpha.49                                ...           5 years ago
  • 0.19.1-alpha.43                                ...           5 years ago
  • 0.19.1-alpha.31                                ...           6 years ago
  • 0.19.1-alpha.11                                ...           6 years ago
  • 0.19.1-alpha.37                                ...           6 years ago
  • 0.19.0                                ...           6 years ago
  • 0.19.1-alpha.36                                ...           6 years ago
  • 0.18.3-alpha.25                                ...           6 years ago
  • 0.18.3-alpha.15                                ...           6 years ago
  • 0.18.3-alpha.5                                ...           6 years ago
  • 0.18.3-alpha.4                                ...           6 years ago
  • 0.18.3-alpha.1                                ...           6 years ago
  • 0.18.2                                ...           6 years ago
  • 0.18.3-alpha.2                                ...           6 years ago
  • 0.18.2-alpha.1                                ...           6 years ago
  • 0.18.1                                ...           6 years ago
  • 0.18.2-alpha.81                                ...           6 years ago
  • 0.18.1-alpha.24                                ...           6 years ago
  • 0.18.1-alpha.21                                ...           6 years ago
  • 0.18.1-alpha.20                                ...           6 years ago
  • 0.18.0                                ...           6 years ago
  • 0.18.1-alpha.12                                ...           6 years ago
  • 0.17.1-alpha.8                                ...           6 years ago
  • 0.17.1-alpha.5                                ...           6 years ago
  • 0.17.1-alpha.3                                ...           6 years ago
  • 0.17.1-alpha.21                                ...           6 years ago
  • 0.17.0                                ...           6 years ago
  • 0.16.1-alpha.11                                ...           6 years ago
  • 0.16.0                                ...           6 years ago
  • 0.15.0                                ...           6 years ago
  • 0.14.0                                ...           6 years ago
  • 0.13.0                                ...           6 years ago
  • 0.12.0                                ...           6 years ago
  • 0.12.1-alpha.54                                ...           6 years ago
  • 0.11.1-alpha.36                                ...           6 years ago
  • 0.11.0                                ...           6 years ago
  • 0.11.1-alpha.48                                ...           6 years ago
  • 0.10.3-canary.0                                ...           6 years ago
  • 0.10.2                                ...           6 years ago
  • 0.10.1                                ...           6 years ago
  • 0.10.0                                ...           6 years ago
  • 0.9.0                                ...           6 years ago
  • 0.8.3                                ...           6 years ago
  • 0.8.2                                ...           7 years ago
  • 0.8.1                                ...           7 years ago
  • 0.8.0                                ...           7 years ago
  • 0.7.0                                ...           7 years ago
  • 0.6.1                                ...           7 years ago
  • 0.6.0                                ...           7 years ago
  • 0.5.2                                ...           7 years ago
  • 0.5.1                                ...           7 years ago
  • 0.5.0                                ...           7 years ago
Maintainers (2)
Downloads
Total 227
Today 0
This Week 0
This Month 84
Last Day 0
Last Week 1
Last Month 134
Dependencies (0)
None
Dev Dependencies (6)

© 2010 - cnpmjs.org x YWFE | Home | YWFE