method NoopTelemetryService.prototype.withSpan
NoopTelemetryService.prototype.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
fn: (span: ISpan) => Promise<T>

Callback receiving the span; its return value is forwarded

optional
_options: SpanOptions

Return Type

Promise<T>

The value returned by fn

Usage

import { NoopTelemetryService } from "telemetry-plugin/src/index.ts";