method ITelemetryService.withSpan
ITelemetryService.withSpan<T>(
name: string,
fn: (span: ISpan) => Promise<T>,
options?: SpanOptions
): Promise<T>

Creates a span, runs the callback, and ends the span.

The span is guaranteed to be ended exactly once, even if the callback throws. The callback receives an ISpan that it can attach attributes to.

Examples

Example 1

const telemetry = ctx.services.get<ITelemetryService>(CAPABILITIES.TELEMETRY);
const result = await telemetry.withSpan('create-order', async (span) => {
  span.setAttribute('order.id', orderId);
  return await orderService.create(orderId);
});

Type Parameters

The return type of the callback

Parameters

name: string

The span name

fn: (span: ISpan) => Promise<T>

Callback receiving the span; its return value is forwarded

optional
options: SpanOptions

Optional span options (kind, attributes, parentSpan)

Return Type

Promise<T>

The value returned by fn

Usage

import { type ITelemetryService } from "common/src/index.ts";