Skip to main content

Function: withSpan()

withSpan<T>(name, fn, options?): Promise<T>

Defined in: src/observability/tracer.ts:215

Wraps an operation in a span.

Type Parameters​

• T

Parameters​

name​

string

Span name

fn​

() => T | Promise<T>

Function to execute within the span

options?​

WithSpanOptions

Optional span configuration

Returns​

Promise<T>

Result of the wrapped function

Remarks​

Creates a span, executes the function within that span's context, and properly ends the span (including error recording on failure).

Supports both synchronous and asynchronous functions.

Example​

// Async operation
const result = await withSpan('indexEprint', async () => {
const eprint = await fetchEprint(uri);
await indexToElasticsearch(eprint);
return eprint;
});

// With attributes
const result = await withSpan(
'processRequest',
async () => {
return await handleRequest(req);
},
{ attributes: { 'http.method': 'GET', 'http.route': '/api/eprints' } }
);

// Sync operation
const hash = withSpan('computeHash', () => {
return crypto.createHash('sha256').update(data).digest('hex');
});

// Nested spans (automatic parent-child relationship)
await withSpan('parentOperation', async () => {
await withSpan('childOperation1', async () => { ... });
await withSpan('childOperation2', async () => { ... });
});

Since​

0.1.0