Tracer is the singleton exported by require('dd-trace'). It must be initialized before importing other libraries.
Methods
init(options?)
Initializes the tracer. This should be called before importing any other libraries to ensure all automatic instrumentation is set up correctly.
Configuration options for the tracer. All options can also be set via environment variables.
The
Tracer instance, for chaining.init() must be called before importing any other modules. Calling it after importing instrumented libraries (e.g., express, pg) will result in incomplete tracing.TracerOptions
| Option | Type | Default | Env var | Description |
|---|---|---|---|---|
service | string | inferred from package.json | DD_SERVICE | Service name |
env | string | — | DD_ENV | Environment (e.g., prod, staging) |
version | string | inferred from package.json | DD_VERSION | Application version |
hostname | string | 127.0.0.1 | DD_AGENT_HOST | Trace agent hostname |
port | number | 8126 | DD_TRACE_AGENT_PORT | Trace agent port |
url | string | — | DD_TRACE_AGENT_URL | Trace agent URL (takes priority over hostname/port) |
sampleRate | number | — | DD_TRACE_SAMPLE_RATE | Ingestion sample rate (0–1) |
rateLimit | number | — | DD_TRACE_RATE_LIMIT | Global rate limit for ingestion |
logInjection | boolean | false | DD_LOGS_INJECTION | Inject trace IDs into log records |
startupLogs | boolean | false | DD_TRACE_STARTUP_LOGS | Emit startup diagnostic logs |
profiling | boolean | — | DD_PROFILING_ENABLED | Enable continuous profiling |
runtimeMetrics | boolean | false | DD_RUNTIME_METRICS_ENABLED | Enable runtime metrics |
plugins | boolean | true | — | Load all built-in plugins |
tags | object | — | DD_TAGS | Global tags applied to every span |
logLevel | string | debug | DD_TRACE_LOG_LEVEL | Minimum log level |
dbmPropagationMode | string | disabled | DD_DBM_PROPAGATION_MODE | DBM-to-APM link injection mode |
dsmEnabled | boolean | false | DD_DATA_STREAMS_ENABLED | Enable Data Streams Monitoring |
appsec | boolean | object | — | DD_APPSEC_ENABLED | AppSec configuration |
iast | boolean | object | — | DD_IAST_ENABLED | IAST configuration |
llmobs | LLMObsEnableOptions | — | DD_LLMOBS_ENABLED | LLM Observability configuration |
startSpan(name, options?)
Starts and returns a new Span representing a logical unit of work. The span is not automatically activated on the current scope — use trace() or scope().activate() for that.
The name of the operation.
Options for the span.
A new
Span object. Call span.finish() when the operation completes.trace(name, fn) / trace(name, options, fn)
Instruments a function by automatically creating a span that is activated on the current scope. The span is finished automatically when the function returns.
The operation name.
Span and trace options.
The function to instrument. Receives the active
span as the first argument. If the function accepts a second done callback parameter, the span is finished when done is called.The return value of
fn.- The function returns a non-promise value (synchronous)
- The returned promise resolves or rejects
- The
donecallback is called (when the function accepts two parameters)
wrap(name, fn) / wrap(name, options, fn)
Wraps a function so that tracer.trace() is called automatically every time the wrapped function is called.
The operation name.
Static options object, or a function that receives the wrapped function’s arguments and returns options dynamically.
The function to wrap.
A new function with the same signature as
fn.If the last argument of the wrapped function is a function (callback), the span finishes when that callback is called.
inject(spanContext, format, carrier)
Injects a SpanContext into a carrier object for cross-process propagation.
The context to inject. A
Span instance may be passed directly (its .context() will be used).The carrier format. Common values:
'http_headers', 'text_map', 'binary'.The carrier object to inject the context into (e.g., an HTTP headers object).
extract(format, carrier)
Extracts a SpanContext from a carrier object.
The carrier format (e.g.,
'http_headers', 'text_map').The carrier object to extract from (e.g., incoming request headers).
The extracted
SpanContext, or null if no context was found.scope()
Returns a reference to the current Scope manager, used for manual context propagation.
The active
Scope manager.use(plugin, config)
Enables and optionally configures a built-in plugin.
The name of the built-in plugin (e.g.,
'express', 'pg', 'redis').Plugin-specific configuration object, or
false to disable the plugin.The
Tracer instance, for chaining.setUser(user)
Links an authenticated user to the current trace.
User properties. The
id field is required.The
Tracer instance.setUrl(url)
Sets the URL for the trace agent after initialization. Only use this when the URL cannot be set at init() time.
The trace agent URL (e.g.,
'http://localhost:8126').The
Tracer instance, for chaining.getRumData()
Returns an HTML string containing <meta> tags for correlating the current server-side trace with a Real User Monitoring (RUM) view.
HTML
<meta> tag string to include in the document <head>.Requires
experimental.enableGetRumData: true in TracerOptions. The resulting HTML must not be cached, as the tags are time-sensitive and user-specific.Properties
appsec
Access the Application Security Management (AppSec) SDK.
llmobs
Access the LLM Observability SDK.
dogstatsd
Access the DogStatsD metrics client.
dataStreamsCheckpointer
Access the Data Streams Monitoring manual checkpointer.
TracerProvider
OpenTelemetry-compatible TracerProvider constructor for registering dd-trace with @opentelemetry/api.
aiguard
Access the AI Guard SDK for evaluating AI conversation safety.
Enable AI Guard by setting
experimental.aiguard.enabled: true in TracerOptions or DD_AI_GUARD_ENABLED=true. This feature requires a connection to the AI Guard service via DD_AI_GUARD_ENDPOINT.openfeature
OpenFeature-compatible provider for feature flag evaluation, backed by Datadog Remote Configuration.
This feature is in beta and requires
experimental.flaggingProvider.enabled: true or DD_EXPERIMENTAL_FLAGGING_PROVIDER_ENABLED=true. Remote Configuration must be properly configured.