Tracer provides an interface for creating Spans.

1.0.0

interface Tracer {
    startActiveSpan<F extends (span: @opentelemetry/api.Span) => unknown>(
        name: string,
        fn: F,
    ): ReturnType<F>;
    startActiveSpan<F extends (span: @opentelemetry/api.Span) => unknown>(
        name: string,
        options: @opentelemetry/api.SpanOptions,
        fn: F,
    ): ReturnType<F>;
    startActiveSpan<F extends (span: @opentelemetry/api.Span) => unknown>(
        name: string,
        options: @opentelemetry/api.SpanOptions,
        context: @opentelemetry/api.Context,
        fn: F,
    ): ReturnType<F>;
    startSpan(
        name: string,
        options?: @opentelemetry/api.SpanOptions,
        context?: @opentelemetry/api.Context,
    ): @opentelemetry/api.Span;
}

Implemented by

Methods

  • Starts a new Span and calls the given function passing it the created span as first argument. Additionally the new span gets set in context and this context is activated for the duration of the function call.

    Important: The callback function is responsible for calling span.end() to finish the span. Unlike some other OpenTelemetry implementations, the span is NOT automatically ended when the callback returns. If span.end() is not called, the span will never be exported and will be silently lost.

    Type Parameters

    Parameters

    • name: string

      The name of the span

    • fn: F

      function called in the context of the span and receives the newly created span as an argument

    Returns ReturnType<F>

    return value of fn

    const something = tracer.startActiveSpan('op', span => {
    try {
    do some work
    span.setStatus({code: SpanStatusCode.OK});
    return something;
    } catch (err) {
    span.setStatus({
    code: SpanStatusCode.ERROR,
    message: err.message,
    });
    throw err;
    } finally {
    span.end();
    }
    });
    const span = tracer.startActiveSpan('op', span => {
    try {
    do some work
    return span;
    } catch (err) {
    span.setStatus({
    code: SpanStatusCode.ERROR,
    message: err.message,
    });
    throw err;
    }
    });
    do some more work
    span.end();
  • Type Parameters

    Parameters

    Returns ReturnType<F>

  • Type Parameters

    Parameters

    Returns ReturnType<F>