StackContextManager can track context for synchronous code (the call "stack"). It does not track context for asynchronous code (Promises, async/await).

This can be useful as a minimal ContextManager in the web/browser environment, which does not provide a native system for async context tracking. Web users might consider @opentelemetry/context-zone as an alternative. Node.js users will almost certainly want to use AsyncLocalStorageContextManager.

Implements

  • ContextManager

Constructors

Properties

_bindFunction: any

Param: context

Param: target

Function to be executed within the context

_currentContext: Context

Keeps the reference to current context

_enabled: any

whether the context manager is enabled or not

Methods

  • Returns the active context

    Returns Context

  • Binds a the certain context or the active one to the target function and then returns the target

    Type Parameters

    • T

    Parameters

    • context: Context

      A context (span) to be bind to target

    • target: T

      a function or event emitter. When target or one of its callbacks is called, the provided context will be used as the active context for the duration of the call.

    Returns T

  • Disable the context manager (clears the current context)

    Returns this

  • Enables the context manager and creates a default(root) context

    Returns this

  • Calls the callback function [fn] with the provided [context]. If [context] is undefined then it will use the window. The context will be set as active

    Type Parameters

    • A extends unknown[]
    • F extends ((...args) => ReturnType<F>)

    Parameters

    • context: Context
    • fn: F

      Callback function

    • Optional thisArg: ThisParameterType<F>

      optional receiver to be used for calling fn

    • Rest ...args: A

      optional arguments forwarded to fn

    Returns ReturnType<F>