For code that is not covered by automatic instrumentation, useDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/datadog/dd-trace-js/llms.txt
Use this file to discover all available pages before exploring further.
tracer.trace() and tracer.wrap() to create spans manually. Both methods handle the span lifecycle and scope activation automatically.
tracer.trace(name, [options], fn)
Instruments a function by creating a new span for the duration of the function’s execution. The span is automatically finished when the function completes, the returned promise resolves/rejects, or the provided callback is called.
Type signatures
Options
tracer.trace() accepts standard SpanOptions plus these additional TraceOptions fields:
| Option | Type | Description |
|---|---|---|
resource | string | The resource being traced (max 5000 chars). |
service | string | Override the service name for this span (max 100 chars). |
type | string | The type of request (e.g. 'web', 'db'). |
childOf | Span | SpanContext | null | Explicit parent. Pass null to force a root span. |
tags | { [key: string]: any } | Tags to set on the span at creation. |
measured | boolean | Whether to measure the span for metrics. |
links | { context: SpanContext, attributes?: Object }[] | Span links to add at creation. |
Synchronous
The span finishes when the callback returns. Any thrown error is automatically recorded on the span.Callback (async)
Pass a second parameterdone to the callback. The span finishes when done is called. Any error passed to done is automatically recorded.
Promise
Return aPromise from the callback. The span finishes when the promise resolves or rejects. Rejected promise errors are automatically recorded.
Async/await
Return anasync function or await inside the callback. The span lifecycle follows the returned promise.
With options
tracer.wrap(name, [options], fn)
Returns a wrapped version of the provided function. Every time the wrapped function is called, tracer.trace() is called automatically. This is useful for patching functions that are defined elsewhere.
Type signatures
Basic usage
Callback functions
If the last argument of the wrapped function is a callback, the span finishes when that callback is called:Dynamic options
Pass a function asoptions to compute trace options from the wrapped function’s arguments at call time:
tracer.startSpan(name, [options])
For lower-level control, startSpan creates a span without activating it on the current scope. You are responsible for finishing it.
