# traced() function · @imqueue/opentelemetry

Source: https://imqueue.org/api/opentelemetry/latest/opentelemetry.traced/
Published: 2026-08-01
Author: @imqueue maintainers (https://github.com/imqueue)
Package: @imqueue/opentelemetry 4.0.0 — generated reference, not hand-written

Builds a method decorator that wraps each call to the decorated method in its own span, ending it when the method returns — or when the promise it returned settles.

**Signature:**

```typescript
export declare function traced(options?: Partial<TracedOptions>): (target: any, methodName: string | symbol, descriptor: TypedPropertyDescriptor<(...args: any[]) => any>) => void;
```

## Parameters


| Parameter | Type | Description |
| --- | --- | --- |
| options | Partial<[TracedOptions](https://imqueue.org/api/opentelemetry/latest/opentelemetry.tracedoptions/)> | _(Optional)_ span kind, extra attributes and tracer name. `kind` defaults to [TraceKind.SERVER](https://imqueue.org/api/opentelemetry/latest/opentelemetry.tracekind/); `tracerName` defaults to `'basic'`. Attributes given in `tags` are applied last, so they override the ones set automatically. |


**Returns:**

(target: any, methodName: string \| symbol, descriptor: TypedPropertyDescriptor<(...args: any\[\]) => any>) => void

a method decorator to apply to the methods you want traced

## Remarks

Use this for work worth seeing in a trace that is not itself an RPC, so the automatic client/server spans do not already cover it: a cache rebuild, a report query, a third-party call.

Async methods are handled: a returned thenable keeps the span open until it settles, so the span duration reflects the real work rather than the time to return a promise. A rejection, or a synchronous throw, records the error on the span, marks it `ERROR`, ends it, and re-throws — the decorator never swallows a failure.

Every span it creates is named `method.call`; the decorated method is identified by the `resource.name` attribute (`ClassName.methodName`), not by the span name. The span is not made the active context, so spans created \*inside\* the method do not nest under it — for nesting, rely on [ImqueueInstrumentation](https://imqueue.org/api/opentelemetry/latest/opentelemetry.imqueueinstrumentation/), which does establish context for RPC handlers.

## Example


```typescript
import { traced, TraceKind } from '@imqueue/opentelemetry';

class Reports {
    @traced()
    public async rebuild(day: string): Promise<void> {
        // span stays open until this promise settles
    }

    @traced({ kind: TraceKind.CLIENT, tags: { 'peer.service': 'billing' } })
    public async fetchInvoices(userId: string): Promise<Invoice[]> {
        return this.http.get(`/invoices/${ userId }`);
    }
}
```

