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?
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('indexPreprint', async () => {
const preprint = await fetchPreprint(uri);
await indexToElasticsearch(preprint);
return preprint;
});
// With attributes
const result = await withSpan(
'processRequest',
async () => {
return await handleRequest(req);
},
{ attributes: { 'http.method': 'GET', 'http.route': '/api/preprints' } }
);
// 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