OpenTelemetry SDK
    Preparing search index...

    Singleton object which represents the entry point to the OpenTelemetry Context API

    1.0.0

    interface ContextAPI {
        active(): Context;
        attach(context: Context): ContextManagementToken;
        bind<T>(context: Context, target: T): T;
        disable(): void;
        setGlobalContextManager(contextManager: ContextManager): boolean;
        with<A extends unknown[], F extends (...args: A) => ReturnType<F>>(
            context: Context,
            fn: F,
            thisArg?: ThisParameterType<F>,
            ...args: A,
        ): ReturnType<F>;
    }
    Index
    • Experimental

      Imperatively sets context as active, returning a ContextManagementToken whose ContextManagementToken.dispose restores the previous context.

      This is a delicate, low-level API - prefer with/bind, which restore context automatically. Use attach only to bridge callback boundaries that with cannot wrap. Call token.dispose() when the operation is done.

      Caveat for async functions: If called inside an async function before the first await, the context change may leak into the caller's context and remain active there. Prefer with for async code.

      Support is best-effort and varies by the active ContextManager (see ContextManager.attach); if it does not implement attach, this logs a warning and returns a no-op token.

      Parameters

      • context: Context

        The Context to attach

      Returns ContextManagementToken

      A ContextManagementToken whose dispose() restores the previous Context

      1.10.0 This API is experimental and may change in minor releases without prior notice.

    • Bind a context to a target function or event emitter

      Type Parameters

      • T

      Parameters

      • context: Context

        context to bind to the event emitter or function. Defaults to the currently active context

      • target: T

        function or event emitter to bind

      Returns T

    • Disable and remove the global context manager

      Returns void

    • Set the current context manager.

      Parameters

      Returns boolean

      true if the context manager was successfully registered, else false

    • Execute a function with an active context

      Type Parameters

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

      Parameters

      • context: Context

        context to be active during function execution

      • fn: F

        function to execute in a context

      • OptionalthisArg: ThisParameterType<F>

        optional receiver to be used for calling fn

      • ...args: A

        optional arguments forwarded to fn

      Returns ReturnType<F>