traced() function

Builds a method decorator that wraps each call to the decorated method in its own span, finishing 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 and extra tags. kind defaults to TraceKind.SERVER; tags given here are applied last and override the automatic ones.

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 imq.request/imq.response 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, tags the span with the error, finishes 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 tag (ClassName.methodName), with the host package name reported separately as package.name. The span attaches to the active span when there is one, so a traced method called while handling an RPC nests inside that call's span.

Example

import { traced, TraceKind } from '@imqueue/datadog';

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.