Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
@opentelemetry/context-async-hooks
Advanced tools
OpenTelemetry AsyncHooks-based Context Manager
The @opentelemetry/context-async-hooks package is part of the OpenTelemetry project, which provides a set of APIs, libraries, agents, and instrumentation to create and manage telemetry data (metrics, logs, and traces) for cloud-native software. Specifically, this package leverages Node.js' async_hooks module to implement context propagation for asynchronous operations. This is crucial for tracing and logging in asynchronous applications, as it allows developers to maintain context across asynchronous boundaries.
Context Management
This feature allows for the management of context across asynchronous operations. It uses the AsyncLocalStorage class to create a new context storage, run a function within this context, and retrieve values from this context later in the async flow.
const { AsyncLocalStorage } = require('@opentelemetry/context-async-hooks');
const asyncLocalStorage = new AsyncLocalStorage();
asyncLocalStorage.run(new Map(), () => {
asyncLocalStorage.getStore().set('key', 'value');
someAsyncOperation().then(() => {
console.log(asyncLocalStorage.getStore().get('key')); // Outputs: 'value'
});
});
Context Propagation
This feature demonstrates how to set up the AsyncLocalStorageContextManager to work with OpenTelemetry's API for global context propagation. This setup is essential for tracing asynchronous operations across different parts of an application.
const { propagation } = require('@opentelemetry/api');
const { AsyncLocalStorageContextManager } = require('@opentelemetry/context-async-hooks');
const contextManager = new AsyncLocalStorageContextManager();
propagation.setGlobalPropagator(contextManager);
// Now, the context is propagated across asynchronous calls automatically.
The cls-hooked package provides a similar functionality to @opentelemetry/context-async-hooks by using the async_hooks module to create context-like namespaces that are preserved across asynchronous calls. It differs in its API and is not specifically tailored for OpenTelemetry or tracing but can be used for general context propagation purposes.
While not a separate npm package, Node.js' built-in async_hooks module is the underlying technology that both @opentelemetry/context-async-hooks and cls-hooked rely on for tracking asynchronous operations. It provides the primitives for creating hooks that monitor the lifecycle of asynchronous operations, which can be used directly for custom context propagation implementations.
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.
Two ContextManager
implementations are exported:
AsyncLocalStorageContextManager
, based on AsyncLocalStorage
AsyncHooksContextManager
, based on AsyncHook
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).
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.
Context propagation is a big subject when talking about tracing in Node.js. If you want more information about it here are some resources:
Apache 2.0 - See LICENSE for more information.
FAQs
OpenTelemetry AsyncHooks-based Context Manager
The npm package @opentelemetry/context-async-hooks receives a total of 4,529,961 weekly downloads. As such, @opentelemetry/context-async-hooks popularity was classified as popular.
We found that @opentelemetry/context-async-hooks demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.