The NodeJS Tracer officially supports versions >=8
. Only even versions like 8.x and 10.x are officially supported. Odd versions like 9.x and 11.x should work but are not officially supported. For a full list of supported libraries, visit the Compatibility Requirements page.
To add the Datadog tracing library to your Node.js applications, follow these steps:
npm install dd-trace --save
Import and initialize the tracer either in code or via command line arguments. The Node.js tracing library needs to be imported and initialized before any other module.
Once you have completed setup, if you are not receiving complete traces, including missing URL routes for web requests, or disconnected or missing spans, confirm step 2 has been correctly done. The tracing library being initialized first is necessary for the tracer to properly patch all of the required libraries for automatic instrumentation.
When using a transpiler such as TypeScript, Webpack, Babel, or others, import and initialize the tracer library in an external file and then import that file as a whole when building your application.
Add any desired configuration to the tracer, such as service
, env
, and version
for Unified Service Tagging.
// This line must come before importing any instrumented module.
const tracer = require('dd-trace').init();
For TypeScript and bundlers that support EcmaScript Module syntax, initialize the tracer in a separate file in order to maintain correct load order.
// server.ts
import './tracer'; // must come before importing any instrumented module.
// tracer.ts
import tracer from 'dd-trace';
tracer.init(); // initialized in a different file to avoid hoisting.
export default tracer;
If the default config is sufficient, or all configuration is done
via environment variables, you can also use dd-trace/init
, which loads and
initializes in one step.
import `dd-trace/init`;
Use the --require
option to Node.js to load and initialize the tracer in once
step.
node --require dd-trace/init app.js
Note: This approach requires using environment variables for all configuration of the tracer.
Install and configure the Datadog Agent to receive traces from your now instrumented application. By default the Datadog Agent is enabled in your datadog.yaml
file under apm_enabled: true
and listens for trace traffic at localhost:8126
. For containerized environments, follow the links below to enable trace collection within the Datadog Agent.
Set apm_non_local_traffic: true
in your main datadog.yaml
configuration file
See the specific setup instructions to ensure that the Agent is configured to receive traces in a containerized environment:
localhost:8126
by default. If this is not the correct host and port change it by setting the below env variables:DD_AGENT_HOST
and DD_TRACE_AGENT_PORT
.
DD_AGENT_HOST=<HOSTNAME> DD_TRACE_AGENT_PORT=<PORT> node server
To use a different protocol such as UDS, specify the entire URL as a single ENV variable DD_TRACE_AGENT_URL
.
DD_TRACE_AGENT_URL=unix:<SOCKET_PATH> node server
To set up Datadog APM in AWS Lambda, see the Tracing Serverless Functions documentation.
Tracing is available for a number of other environments, such as Heroku, Cloud Foundry, AWS Elastic Beanstalk, and Azure App Services Extension.
For other environments, please refer to the Integrations documentation for that environment and contact support if you are encountering any setup issues.
See the tracer settings for the list of initialization options.
Tracer settings can be configured as a parameter to the init()
method or as environment variables.
Config | Environment Variable | Default | Description |
---|---|---|---|
env | DD_ENV | null | Set an application’s environment e.g. prod , pre-prod , stage , etc. Available for versions 0.20+ |
service | DD_SERVICE | null | The service name to be used for this program. Available for versions 0.20+ |
version | DD_VERSION | null | The version number of the application. Defaults to value of the version field in package.json. Available for versions 0.20+ |
tags | DD_TAGS | {} | Set global tags that should be applied to all spans and runtime metrics. When passed as an environment variable, the format is key:value,key:value . When setting this programmatically: tracer.init({ tags: { foo: 'bar' } }) Available for versions 0.20+ |
It is recommended that you use DD_ENV
, DD_SERVICE
, and DD_VERSION
to set env
, service
, and version
for your services. Review the Unified Service Tagging documentation for recommendations on how to configure these environment variables.
Config | Environment Variable | Default | Description |
---|---|---|---|
enabled | DD_TRACE_ENABLED | true | Whether to enable the tracer. |
debug | DD_TRACE_DEBUG | false | Enable debug logging in the tracer. |
url | DD_TRACE_AGENT_URL | null | The URL of the Trace Agent that the tracer submits to. Takes priority over hostname and port, if set. Supports Unix Domain Sockets in combination with the apm_config.receiver_socket in your datadog.yaml file, or the DD_APM_RECEIVER_SOCKET environment variable. |
hostname | DD_TRACE_AGENT_HOSTNAME | localhost | The address of the Agent that the tracer submits to. |
port | DD_TRACE_AGENT_PORT | 8126 | The port of the Trace Agent that the tracer submits to. |
dogstatsd.port | DD_DOGSTATSD_PORT | 8125 | The port of the DogStatsD Agent that metrics are submitted to. |
logInjection | DD_LOGS_INJECTION | false | Enable automatic injection of trace IDs in logs for supported logging libraries. |
sampleRate | - | 1 | Percentage of spans to sample as a float between 0 and 1 . |
flushInterval | - | 2000 | Interval (in milliseconds) at which the tracer submits traces to the Agent. |
runtimeMetrics | DD_RUNTIME_METRICS_ENABLED | false | Whether to enable capturing runtime metrics. Port 8125 (or configured with dogstatsd.port ) must be opened on the Agent for UDP. |
experimental | - | {} | Experimental features can be enabled all at once using Boolean true or individually using key/value pairs. Contact support to learn more about the available experimental features. |
plugins | - | true | Whether or not to enable automatic instrumentation of external libraries using the built-in plugins. |
- | DD_TRACE_DISABLED_PLUGINS | - | A comma-separated string of integration names automatically disabled when tracer is initialized. Environment variable only e.g. DD_TRACE_DISABLED_PLUGINS=express,dns . |
clientToken | DD_CLIENT_TOKEN | null | Client token for browser tracing. Can be generated in Datadog in Integrations -> APIs. |
logLevel | DD_TRACE_LOG_LEVEL | debug | A string for the minimum log level for the tracer to use when debug logging is enabled, e.g. error , debug . |
Additional helpful documentation, links, and articles: