@sophonz/context-manager
v0.0.8
Published
Readme
@sophonz/context-manager
OpenTelemetry context manager implementation with async context propagation support.
Extends the OTel Web context manager with enhanced tracking for Promise, setTimeout, MutationObserver, MessagePort, and event listener contexts to ensure spans maintain proper parent-child relationships across asynchronous boundaries.
Part of the Sophonz OpenTelemetry suite.
Install
bun add @sophonz/context-manager
# or
pnpm add @sophonz/context-manager
# or
npm install @sophonz/context-managerUsage
import { SophonzContextManager } from '@sophonz/context-manager';
const contextManager = new SophonzContextManager({
async: true, // Enable async context propagation
onBeforeContextStart: () => console.log('Context started'),
onBeforeContextEnd: () => console.log('Context ended')
});
contextManager.enable();
// Use with OTel
const sdk = new NodeSDK({
contextManager
});API
SophonzContextManager
Constructor Options
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| async | boolean | false | Enable async tracking of span parents |
| onBeforeContextStart | function | - | Callback before context activation |
| onBeforeContextEnd | function | - | Callback before context deactivation |
Methods
enable()- Enable the context manager and patch async APIsdisable()- Disable and restore all patchesactive()- Get the currently active contextbind(context, target)- Bind a context to a functionwith(context, fn, thisArg, ...args)- Execute a function with a specific context
Peer Dependencies
@opentelemetry/api@^1.9.1
License
See LICENSE in this package.
Part of sophonz-js.
