OpenTelemetry SDK
    Preparing search index...

    1.0.0

    interface ContextManager {
        active(): Context;
        attach?(context: Context): ContextManagementToken;
        bind<T>(context?: Context, target: T): T;
        disable(): this;
        enable(): this;
        with<A extends unknown[], F extends (...args: A) => ReturnType<F>>(
            context: Context,
            fn: F,
            thisArg?: ThisParameterType<F>,
            ...args: A,
        ): ReturnType<F>;
    }

    Implemented by

    Index
    • Experimental

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

      This is a delicate, low-level API - prefer with/bind, which restore context automatically. attach exists only to bridge callback boundaries that with cannot wrap. The caller is responsible for disposing the token via token.dispose() when the operation is complete. Support is optional and best-effort; when omitted, ContextAPI.attach logs a warning and returns a no-op token.

      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.

      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 an object as the current context (or a specific one)

      Type Parameters

      • T

      Parameters

      • Optionalcontext: Context

        Optionally specify the context which you want to assign

      • target: T

        Any object to which a context need to be set

      Returns T

    • Run the fn callback with object set as the current active context

      Type Parameters

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

      Parameters

      • context: Context

        Any object to set as the current active context

      • fn: F

        A callback to be immediately run within a specific context

      • OptionalthisArg: ThisParameterType<F>

        optional receiver to be used for calling fn

      • ...args: A

        optional arguments forwarded to fn

      Returns ReturnType<F>