Creates and starts a new span, sets it as the active span in the current context, and executes a provided function within this context.

This method creates a span, makes it active during the execution of the provided function, and automatically ends the span when the function completes (or throws). This provides automatic span lifecycle management and context propagation. If the function is asynchronous the span will be ended when the returned Promise resolves or rejects. Note: The callback will be executed even if the traceHost is unable to create a span (returns null).

This overloaded version supports both optional span creation options and the this argument for the callback.

The name of the span, should be descriptive of the operation being traced

Optional configuration for span creation (parent context, attributes, etc.) or the function to execute within the span's active context

The function to execute within the span's active context or the this argument for the callback

The this argument for the callback. If not provided, ISpanScope is used as this

The result of executing the provided function

  • Creates and starts a new span, sets it as the active span in the current context, and executes a provided function within this context.

    This method creates a span, makes it active during the execution of the provided function, and automatically ends the span when the function completes (or throws). This provides automatic span lifecycle management and context propagation. If the function is asynchronous the span will be ended when the returned Promise resolves or rejects. Note: The callback will be executed even if the traceHost is unable to create a span (returns null).

    Type Parameters

    Parameters

    • traceHost: T
    • name: string

      The name of the span, should be descriptive of the operation being traced

    • fn: F

      The function to execute within the span's active context

    • OptionalthisArg: ThisParameterType<F>

      The this argument for the callback. If not provided, ISpanScope is used as this

    Returns ReturnType<F>

    The result of executing the provided function

    • The span is automatically ended when the function completes or throws an exception
    • The span becomes the active parent for any spans created within the function
    • If the function throws an error, the span status is automatically set to ERROR
    • This is the recommended method for most tracing scenarios due to automatic lifecycle management
    • Multiple overloads available for different parameter combinations
  • Creates and starts a new span, sets it as the active span in the current context, and executes a provided function within this context.

    This method creates a span, makes it active during the execution of the provided function, and automatically ends the span when the function completes (or throws). This provides automatic span lifecycle management and context propagation. If the function is asynchronous the span will be ended when the returned Promise resolves or rejects. Note: The callback will be executed even if the traceHost is unable to create a span (returns null).

    Type Parameters

    Parameters

    • traceHost: T
    • name: string

      The name of the span, should be descriptive of the operation being traced

    • options: IOTelSpanOptions

      Optional configuration for span creation (parent context, attributes, etc.)

    • fn: F

      The function to execute within the span's active context

    • OptionalthisArg: ThisParameterType<F>

      The this argument for the callback. If not provided, ISpanScope is used as this

    Returns ReturnType<F>

    The result of executing the provided function

    • The span is automatically ended when the function completes or throws an exception
    • The span becomes the active parent for any spans created within the function
    • If the function throws an error, the span status is automatically set to ERROR
    • This is the recommended method for most tracing scenarios due to automatic lifecycle management
    • Multiple overloads available for different parameter combinations