wrapWithSpan
Callable
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
Parameters
rest...args: Args
Returns Return
Wraps a function with OpenTelemetry span instrumentation.
ArgsandReturnare separate generics so that the argument types flow through to thespanNameandspanOptionscallbacks. They are inferred from the wrapped function, so annotate its parameters when assigning the result to an option whose type is a union -requestHandleraccepts both a router and a plain handler, and TypeScript cannot infer parameter types through a union of function types:Synchronous functions stay synchronous, asynchronous ones keep their span open until the returned promise settles.
The wrapper forwards its own
thisto the wrapped function, so a method keeps working when wrapped. An arrow function still ignores it, as arrow functions always takethisfrom where they were defined.