Tracing Node.js Applications
Datadog's Research Report: The State of Serverless Report: The State of Serverless

Tracing Node.js Applications

Installation And Getting Started

If you already have a Datadog account you can find step-by-step instructions in our in-app guides for host-based and container-based set ups.

For descriptions of terminology used in APM, take a look at the official documentation.

For details about configuration and using the API, see Datadog’s API documentation.

For details about contributing, check out the development guide.

Quickstart

This library MUST be imported and initialized before any instrumented module. When using a transpiler, you MUST import and initialize the tracer library in an external file and then import that file as a whole when building your application. This prevents hoisting and ensures that the tracer library gets imported and initialized before importing any other instrumented module.

To begin tracing Node.js applications, first install and configure the Datadog Agent, see the additional documentation for tracing Docker applications or Kubernetes applications.

Next, install the Datadog Tracing library using npm:

npm install --save dd-trace

Finally, import and initialize the tracer:

JavaScript
// This line must come before importing any instrumented module.
const tracer = require('dd-trace').init();
TypeScript
// server.js
import './tracer'; // must come before importing any instrumented module.

// tracer.js
import tracer from 'dd-trace';
tracer.init(); // initialized in a different file to avoid hoisting.
export default tracer;

See the tracer settings for the list of initialization options.

Configuration

Tracer settings can be configured as a parameter to the init() method or as environment variables.

ConfigEnvironment VariableDefaultDescription
enabledDD_TRACE_ENABLEDtrueWhether to enable the tracer.
debugDD_TRACE_DEBUGfalseEnable debug logging in the tracer.
serviceDD_SERVICE_NAMEnullThe service name to be used for this program.
urlDD_TRACE_AGENT_URLnullThe 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.
hostnameDD_TRACE_AGENT_HOSTNAMElocalhostThe address of the Agent that the tracer submits to.
portDD_TRACE_AGENT_PORT8126The port of the Trace Agent that the tracer submits to.
dogstatsd.portDD_DOGSTATSD_PORT8125The port of the DogStatsD Agent that metrics are submitted to.
envDD_ENVnullSet an application’s environment e.g. prod, pre-prod, stage, etc.
logInjectionDD_LOGS_INJECTIONfalseEnable automatic injection of trace IDs in logs for supported logging libraries.
tagsDD_TAGS{}Set global tags that should be applied to all spans and metrics. When passed as an environment variable, the format is key:value, key:value.
sampleRate-1Percentage of spans to sample as a float between 0 and 1.
flushInterval-2000Interval (in milliseconds) at which the tracer submits traces to the Agent.
runtimeMetricsDD_RUNTIME_METRICS_ENABLEDfalseWhether to enable capturing runtime metrics. Port 8125 (or configured with dogstatsd.port) must be opened on the Agent for UDP.
reportHostnameDD_TRACE_REPORT_HOSTNAMEfalseWhether to report the system’s hostname for each trace. When disabled, the hostname of the Agent is used instead.
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-trueWhether or not to enable automatic instrumentation of external libraries using the built-in plugins.
clientTokenDD_CLIENT_TOKENnullClient token for browser tracing. Can be generated in Datadog in Integrations -> APIs.
logLevelDD_TRACE_LOG_LEVELdebugA string for the minimum log level for the tracer to use when debug logging is enabled, e.g. error, debug.

Change Agent Hostname

Configure your application level tracers to submit traces to a custom Agent hostname:

The NodeJS Tracing Module automatically looks for and initializes with the 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

Compatibility

Node >=8 is supported by this library. 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.

Node 4 or Node 6 versions are supported by version 0.13 of the dd-trace-js tracer. This version will be supported until April 30th, 2020, but no new feature will be added.

Note: The global policy is that the Datadog JS tracer supports (only for bug fixes) a Node version until 1 year after its release reached its end-of-life.

Integrations

APM provides out-of-the-box instrumentation for many popular frameworks and libraries by using a plugin system. If you would like support for a module that is not listed, contact support to share a request.

For details about how to how to toggle and configure plugins, check out the API documentation.

Web Framework Compatibility

ModuleVersionsSupport TypeNotes
connect>=2Fully supported
express>=4Fully supportedSupports Sails, Loopback, and more
fastify>=1Fully supported
graphql>=0.10Fully supportedSupports Apollo Server and express-graphql
gRPC>=1.13Fully supported
hapi>=2Fully supported
koa>=2Fully supported
paperplane>=2.3Fully supportedNot supported in serverless-mode
restify>=3Fully supported

Native Module Compatibility

ModuleSupport Type
dnsFully supported
fsFully supported
httpFully supported
httpsFully supported
netFully supported

Data Store Compatibility

ModuleVersionsSupport TypeNotes
cassandra-driver>=3Fully supported
couchbase^2.4.2Fully supported
elasticsearch>=10Fully supportedSupports @elastic/elasticsearch versions >=5
ioredis>=2Fully supported
knex>=0.8Fully supportedThis integration is only for context propagation
memcached>=2.2Fully supported
mongodb-core>=2Fully supportedSupports Mongoose
mysql>=2Fully supported
mysql2>=1Fully supported
pg>=4Fully supportedSupports pg-native when used with pg
redis>=0.12Fully supported
tedious>=1Fully supportedSQL Server driver for mssql and sequelize

Worker Compatibility

ModuleVersionsSupport TypeNotes
amqp10>=3Fully supportedSupports AMQP 1.0 brokers (i.e. ActiveMQ, Apache Qpid)
amqplib>=0.5Fully supportedSupports AMQP 0.9 brokers (i.e. RabbitMQ, Apache Qpid)
generic-pool>=2Fully supported
kafka-nodeComing Soon
rhea>=1Fully supported

Promise Library Compatibility

ModuleVersionsSupport Type
bluebird>=2Fully supported
promise>=7Fully supported
promise-js>=0.0.3Fully supported
q>=1Fully supported
when>=3Fully supported

Logger Compatibility

ModuleVersionsSupport Type
bunyan>=1Fully supported
paperplane>=2.3.2Fully supported
pino>=2Fully supported
winston>=1Fully supported

Further Reading