{"_id":"@opentelemetry/context-async-hooks@2.11.0","bugs":{"url":"https://github.com/open-telemetry/opentelemetry-js/issues"},"dist":{"shasum":"1377ad3cb1125106ba36d4cd3c4933484d157691","tarball":"http://registry-npm.ywwl.com/@opentelemetry/context-async-hooks/download/@opentelemetry/context-async-hooks-2.11.0.tgz","fileCount":21,"integrity":"sha512-Tr79DyWI8itsBdg+jH+opjfrwLzX+erk1/ExkIwhWoAVjVrJIn2y5+cGjTC0Vy8fyNIA/y8wuJPZwr1T3xCZeQ==","signatures":[{"sig":"MEUCIA7Y0eJHg90seqIuBjFI7Yn+cV8dHz5vkie/QS9iXZcHAiEAgQg9QRBwhn8NS/HWZ9XXEc554kacz3UA1B35titMgXU=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"},{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEQCIHbUaA49rYCMw+1O6938nh44qnneaOaIMxk+9q11GD7uAiB8SEuVwkfp9kCgR+fAvOLcW7BFARlkuirdZlFccvitUw=="}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/@opentelemetry%2fcontext-async-hooks@2.11.0","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":62640,"size":15333,"noattachment":false,"key":"/@opentelemetry/context-async-hooks/-/@opentelemetry/context-async-hooks-2.11.0.tgz"},"main":"build/src/index.js","name":"@opentelemetry/context-async-hooks","types":"build/src/index.d.ts","author":{"name":"OpenTelemetry Authors"},"engines":{"node":"^18.19.0 || >=20.6.0"},"gitHead":"0b72a81636fa476e8f1f1afd2ae0c90a1362194c","license":"Apache-2.0","scripts":{"tdd":"npm run test -- --watch-extensions ts --watch","lint":"eslint .","test":"nyc mocha 'test/**/*.test.ts'","clean":"tsc --build --clean","compile":"tsc --build","version":"node ../../scripts/version-update.js","lint:fix":"eslint . --fix","prewatch":"npm run precompile","test:bench":"node test/performance/benchmark/attach.js","align-api-deps":"node ../../scripts/align-api-deps.js","peer-api-check":"node ../../scripts/peer-api-check.js","prepublishOnly":"npm run compile"},"version":"2.11.0","_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"oidc:ed8b4ee6-a665-4520-8aa4-5fca27442ac9"}},"homepage":"https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-context-async-hooks","keywords":["opentelemetry","nodejs","tracing","profiling","metrics","stats"],"repository":{"url":"git+https://github.com/open-telemetry/opentelemetry-js.git","type":"git"},"_npmVersion":"lerna/10.0.1/node@v24.19.0+x64 (linux)","description":"OpenTelemetry AsyncLocalStorage-based Context Manager","directories":{},"maintainers":[{"name":"dyladan","email":"dyladan@gmail.com"},{"name":"pichlermarc","email":"marc.pichler@dynatrace.com"},{"name":"overbalance","email":"overbalancedev@proton.me"},{"name":"npmjs-account","email":"npmjs-account@opentelemetry.io"},{"name":"trentm","email":"trentm@gmail.com"},{"name":"martinkuba","email":"martin@martinkuba.com"}],"sideEffects":false,"_nodeVersion":"24.19.0","publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"nyc":"17.1.0","mocha":"11.8.0","benchmark":"2.1.4","typescript":"5.2.2","@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":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/context-async-hooks_2.11.0_1788194468605_0.5916144497161149"},"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":1788194468691,"_cnpm_publish_time":1788194468691,"dist-tags":{"canary":"3.0.0-rc.0","latest":"3.1.0","next":"1.8.0"}}