Skip to main content

withSpan()

function withSpan\<T\>(
name: string,
fn: (span: Span) => T | Promise\<T\>,
attributes?: Attributes): Promise\<T\>;

Defined in: src/lib/logging/context.ts:320

Executes a function within a new span context. Automatically ends the span when the function completes.

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
namestringName for the span.
fn(span: Span) => T | Promise<T>Function to execute within the span.
attributes?AttributesOptional attributes for the span.

Returns

Promise<T>

The result of the function.

Example

const result = await withSpan("fetch-user", async (span) => {
span.attributes = { ...span.attributes, userId: "123" };
return await fetchUser("123");
});