Socket
Socket
Sign inDemoInstall

@opentelemetry/context-async-hooks

Package Overview
Dependencies
1
Maintainers
5
Versions
115
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @opentelemetry/context-async-hooks

OpenTelemetry AsyncHooks-based Context Manager


Version published
Maintainers
5
Install size
57.1 kB
Created

Package description

What is @opentelemetry/context-async-hooks?

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.

What are @opentelemetry/context-async-hooks's main functionalities?

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.

Other packages similar to @opentelemetry/context-async-hooks

Changelog

Source

0.5.0

Released 2020-03-16

First official beta release

  • provides almost fully complete metrics, tracing, and context propagation functionality but makes no promises around breaking changes

:boom: Breaking Change

  • #853 Rename scope to context
  • #851 Rename formatter to propagator

:rocket: (Enhancement)

  • #828 feat: metric observer
  • #858 chore: update out-of-date dependencies
  • #856 fix: change loglevel for beta
  • #843 export resource to exporters
  • #846 SDK Resource
  • #625 feat: introduce ended property on Span
  • #837 Simplify SDK registration
  • #818 fix: change SpanContext.traceFlags to mandatory
  • #827 Add getter and setter arguments to propagation API
  • #821 feat: add composite propagator
  • #824 Faster trace id generation
  • #708 Simplify and speed up trace context parsing
  • #802 chore: adding force flush to span processors
  • #816 feat: use context-based tracing
  • #815 Resources API: package, semantic conventions, and test utils
  • #797 Add propagation API
  • #792 Add context API
  • #685 feat: add express plugin #666
  • #769 Separate context propagation (OTEP 66)
  • #653 Prevent loading plugins for incorrect module #626
  • #654 feat: warn user when a instrumented package was already required #636
  • #772 chore: add typing to propagator carrier
  • #735 feat: decode jaeger header
  • #719 feat(plugin-http): sync. specs for statuscode
  • #701 feat: add jaeger http trace format (#696)

:bug: (Bug Fix)

  • #798 Respect sampled bit in probability sampler
  • #743 fix: left pad jaeger trace ids
  • #715 fix: unref jaeger socket to prevent process running indefinitely

Readme

Source

OpenTelemetry AsyncHooks-based Context Manager

Gitter chat NPM Published Version dependencies devDependencies Apache License

This package provides async-hooks based context manager which is used internally by OpenTelemetry plugins to propagate specific context between function calls and async operations. It only targets NodeJS since async-hooks is only available there.

What is a ContextManager ?

The definition and why they exist is available on the readme of the context-base package.

Implementation in NodeJS

NodeJS has a specific API to track async context: async-hooks, it allows to track creation of new async operation and their respective parent. This package only handle storing a specific object for a given async hooks context.

Limitations

Even if the API is native to NodeJS, it doesn't cover all possible cases of context propagation but there is a big effort from the NodeJS team to fix those. That's why we generally advise to be on the latest LTS to benefit from performance and bug fixes.

There are known modules that break context propagation (some of them are listed there), so it's possible that the context manager doesn't work with them.

Prior arts

Context propagation is a big subject when talking about tracing in NodeJS, if you want more information about that here are some resources:

License

Apache 2.0 - See LICENSE for more information.

Keywords

FAQs

Last updated on 16 Mar 2020

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc