[**Documentation**](../../../README)

***

[Documentation](../../../README) / [@zerotal/telemetry](../README) / withSpan

# Function: withSpan()

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

Defined in: [telemetry/src/withSpan.ts:33](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/telemetry/src/withSpan.ts#L33)

Runs `fn` inside a named span using the global tracer registered by
`TelemetryProvider`. If no tracer is registered the callback runs normally
and receives a no-op span — no error is thrown.

## Type Parameters

### T

`T`

## Parameters

### name

`string`

### fn

(`span`) => `Promise`\<`T`\>

### options?

[`SpanOptions`](../interfaces/SpanOptions) = `{}`

## Returns

`Promise`\<`T`\>

## Example

```ts
import { withSpan } from '@zerotal/telemetry';

async function processOrder(id: string) {
  return withSpan('process-order', async (span) => {
    span.setAttribute('order.id', id);
    // ...
  });
}
```
