traced() function

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:

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>

(Optional) span kind, extra attributes and tracer name. kind defaults to TraceKind.SERVER; 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, which does establish context for RPC handlers.

Example

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 }`);
    }
}

Read this page as plain markdown — no HTML, no navigation. For pasting into an LLM, or for an agent to fetch.