{"name":"@opentelemetry/context-async-hooks","version":"2.6.1","keywords":["opentelemetry","nodejs","tracing","profiling","metrics","stats"],"author":{"name":"OpenTelemetry Authors"},"license":"Apache-2.0","_id":"@opentelemetry/context-async-hooks@2.6.1","maintainers":[{"name":"dyladan","email":"dyladan@gmail.com"},{"name":"pichlermarc","email":"marc.pichler@dynatrace.com"},{"name":"npmjs-account","email":"npmjs-account@opentelemetry.io"},{"name":"trentm","email":"trentm@gmail.com"}],"homepage":"https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-context-async-hooks","bugs":{"url":"https://github.com/open-telemetry/opentelemetry-js/issues"},"dist":{"shasum":"06e60d5b3fba992a832af7f034758574e951bba3","tarball":"http://registry-npm.ywwl.com/@opentelemetry/context-async-hooks/download/@opentelemetry/context-async-hooks-2.6.1.tgz","fileCount":18,"integrity":"sha512-XHzhwRNkBpeP8Fs/qjGrAf9r9PRv67wkJQ/7ZPaBQQ68DYlTBBx5MF9LvPx7mhuXcDessKK2b+DcxqwpgkcivQ==","signatures":[{"sig":"MEQCIHSXNZb1SDfsQT0GZyiRqb/K48ZpSUr4EHlhIb7J3ZJVAiAr5Jn96uZOI7Ds6+tPSMgKMivKCBbcIdTEOwmo7kGZ1w==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/@opentelemetry%2fcontext-async-hooks@2.6.1","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":52856,"size":13090,"noattachment":false,"key":"/@opentelemetry/context-async-hooks/-/@opentelemetry/context-async-hooks-2.6.1.tgz"},"main":"build/src/index.js","types":"build/src/index.d.ts","engines":{"node":"^18.19.0 || >=20.6.0"},"gitHead":"7e74509a4d848e94b2970bb5262dd3e8efeed0a2","scripts":{"tdd":"npm run test -- --watch-extensions ts --watch","lint":"eslint . --ext .ts","test":"nyc mocha 'test/**/*.test.ts'","clean":"tsc --build --clean","compile":"tsc --build","version":"node ../../scripts/version-update.js","lint:fix":"eslint . --ext .ts --fix","prewatch":"npm run precompile","align-api-deps":"node ../../scripts/align-api-deps.js","peer-api-check":"node ../../scripts/peer-api-check.js","prepublishOnly":"npm run compile"},"_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"oidc:ed8b4ee6-a665-4520-8aa4-5fca27442ac9"}},"repository":{"url":"git+https://github.com/open-telemetry/opentelemetry-js.git","type":"git"},"_npmVersion":"lerna/9.0.5/node@v24.14.0+x64 (linux)","description":"OpenTelemetry AsyncLocalStorage-based Context Manager","directories":{},"sideEffects":false,"_nodeVersion":"24.14.0","publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"nyc":"17.1.0","mocha":"11.7.5","typescript":"5.0.4","@types/node":"18.19.130","@types/mocha":"10.0.10","@opentelemetry/api":">=1.0.0 <1.10.0"},"peerDependencies":{"@opentelemetry/api":">=1.0.0 <1.10.0"},"_npmOperationalInternal":{"tmp":"tmp/context-async-hooks_2.6.1_1774454039298_0.17134005903074323","host":"s3://npm-registry-packages-npm-production"},"readme":"# OpenTelemetry async_hooks-based Context Managers\n\n[![NPM Published Version][npm-img]][npm-url]\n[![Apache License][license-image]][license-image]\n\nThis package provides two [`ContextManager`](https://open-telemetry.github.io/opentelemetry-js/interfaces/_opentelemetry_api.ContextManager.html) 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](https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-context-zone) or [opentelemetry-context-zone-peer-dep](https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-context-zone-peer-dep).\n\nSee [the definition of the `ContextManager` interface][def-context-manager] and the problem it solves.\n\n## API\n\nTwo `ContextManager` implementations are exported:\n\n- `AsyncLocalStorageContextManager`, based on [`AsyncLocalStorage`](https://nodejs.org/api/async_context.html#class-asynclocalstorage)\n- `AsyncHooksContextManager`, based on [`AsyncHook`](https://nodejs.org/api/async_hooks.html#async_hooks_class_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](https://nodejs.org/en/blog/vulnerability/january-2026-dos-mitigation-async-hooks).\n\n### Using attach()\n\n`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.\n\n**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.\n\n```javascript\nimport { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';\nimport { ROOT_CONTEXT } from '@opentelemetry/api';\n\nconst contextManager = new AsyncLocalStorageContextManager();\ncontextManager.enable();\n\nconst myContext = ROOT_CONTEXT.setValue('key', 'value');\n\n// Attach a context - it becomes active for the current async execution chain\nconst token = contextManager.attach(myContext);\nconsole.log(contextManager.active()); // myContext\n\n// Context propagates to async operations automatically\nawait someAsyncOperation();\nconsole.log(contextManager.active()); // still myContext\n\n// Restore the previous context by disposing the token\ntoken.dispose();\nconsole.log(contextManager.active()); // ROOT_CONTEXT\n```\n\n**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:\n\n```javascript\nconst token = contextManager.attach(myContext);\ntry {\n  await doWork();\n} finally {\n  token.dispose(); // Always restore\n}\n```\n\nFor most use cases, prefer using `with()` as it automatically handles context restoration.\nNot restoring context properly can lead to catastrophic effects on your telemetry, even outside the bounds of your instrumentation library.\n\n## Prior art\n\nContext propagation is a big subject when talking about tracing in Node.js. If you want more information about it here are some resources:\n\n- <https://www.npmjs.com/package/continuation-local-storage> (which was the old way of doing context propagation)\n- [Datadog's own implementation][dd-js-tracer-scope] for their JavaScript tracer\n- [OpenTracing implementation][opentracing-scope]\n- [Discussion about context propagation][diag-team-scope-discussion] by the Node.js Diagnostics Working Group\n\n## Useful links\n\n- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>\n- For more about OpenTelemetry JavaScript: <https://github.com/open-telemetry/opentelemetry-js>\n- For help or feedback on this project, join us in [GitHub Discussions][discussions-url]\n\n## License\n\nApache 2.0 - See [LICENSE][license-url] for more information.\n\n[discussions-url]: https://github.com/open-telemetry/opentelemetry-js/discussions\n[license-url]: https://github.com/open-telemetry/opentelemetry-js/blob/main/LICENSE\n[license-image]: https://img.shields.io/badge/license-Apache_2.0-green.svg?style=flat\n[def-context-manager]: https://opentelemetry.io/docs/instrumentation/js/api/context/#context-manager\n[dd-js-tracer-scope]: https://github.com/DataDog/dd-trace-js/blob/master/packages/dd-trace/src/scope.js\n[opentracing-scope]: https://github.com/opentracing/opentracing-javascript/pull/113\n[diag-team-scope-discussion]: https://github.com/nodejs/diagnostics/issues/300\n[npm-url]: https://www.npmjs.com/package/@opentelemetry/context-async-hooks\n[npm-img]: https://badge.fury.io/js/%40opentelemetry%2Fcontext-async-hooks.svg\n","publish_time":1774454039460,"_cnpm_publish_time":1774454039460,"dist-tags":{"canary":"3.0.0-rc.0","latest":"3.1.0","next":"1.8.0"}}