Skip to main content
Version: Next

wrapWithSpan

Callable

  • wrapWithSpan<Args, Return>(fn, options): (...args) => Return

  • Wraps a function with OpenTelemetry span instrumentation.

    Args and Return are separate generics so that the argument types flow through to the spanName and spanOptions callbacks. They are inferred from the wrapped function, so annotate its parameters when assigning the result to an option whose type is a union - requestHandler accepts both a router and a plain handler, and TypeScript cannot infer parameter types through a union of function types:

    requestHandler: wrapWithSpan(async ({ request }: CheerioCrawlingContext) => { ... })

    Synchronous functions stay synchronous, asynchronous ones keep their span open until the returned promise settles.

    The wrapper forwards its own this to the wrapped function, so a method keeps working when wrapped. An arrow function still ignores it, as arrow functions always take this from where they were defined.


    Parameters

    • fn: (...args) => Return
      • optionaloptions: { spanName?: string | (...args) => string; spanOptions?: SpanOptions | (...args) => SpanOptions; tracer?: Tracer }
        • optionalspanName: string | (...args) => string
        • optionalspanOptions: SpanOptions | (...args) => SpanOptions
        • optionaltracer: Tracer

      Returns (...args) => Return

        • (...args): Return
        • Parameters

          • rest...args: Args

          Returns Return