New:Introducing Socket Scanning for VS Code Marketplace Extensions.Learn more →
Get Started

@opentelemetry/context-async-hooks

Package Overview
Dependencies
Maintainers
6
Versions
146
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@opentelemetry/context-async-hooks

OpenTelemetry AsyncLocalStorage-based Context Manager

canary
Source
npmnpm
Version
3.0.0-development.1
Version published
Weekly downloads
36M
-33.83%
Maintainers
6
Weekly downloads
 
Created
Source

OpenTelemetry async_hooks-based Context Managers

NPM Published Version Apache License

This package provides a ContextManager implementation 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

A ContextManager implementations is exported:

  • AsyncLocalStorageContextManager, based on AsyncLocalStorage

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:

License

Apache 2.0 - See LICENSE for more information.

Keywords

opentelemetry

FAQs

Package last updated on 05 Oct 2026

Related posts