The tracer must be initialized before any other require calls. If libraries such as express, pg, or http are loaded before dd-trace, they will not be instrumented and their spans will not appear in traces.
The recommended pattern is to create a dedicated initialization file and require it first.
Using the NODE_OPTIONS environment variable is the safest approach because it guarantees the tracer is loaded before any user code, regardless of how Node.js is invoked:
NODE_OPTIONS='-r dd-trace/init' node app.js
This uses the built-in dd-trace/init entry point which calls init() with no arguments. Provide configuration via environment variables:
For TypeScript projects, import and initialize the tracer before any other imports. Use a separate file and import it first:
instrument.ts
import tracer from 'dd-trace'tracer.init({ service: 'my-api', env: 'production',})export default tracer
In your application entry point:
app.ts
import './instrument' // must be the first importimport express from 'express'const app = express()// ...
With TypeScript import statements, the order of imports is not always guaranteed by the module bundler. If you observe missing instrumentation, use NODE_OPTIONS='-r ts-node/register -r ./instrument' or compile TypeScript and use -r ./instrument.js.
By default, the tracer connects to a Datadog Agent at localhost:8126. Override this with environment variables:
# Different hostDD_AGENT_HOST=my-agent-host node app.js# Different portDD_TRACE_AGENT_PORT=8126 node app.js# Full URL (takes priority over host/port)DD_TRACE_AGENT_URL=http://my-agent-host:8126 node app.js
Or programmatically:
require('dd-trace').init({ hostname: 'my-agent-host', port: 8126, // or url: 'http://my-agent-host:8126',})
All built-in plugins are enabled by default. Configure a specific plugin using tracer.use():
const tracer = require('dd-trace').init()// Configure a single integrationtracer.use('pg', { service: 'postgres-cluster',})// Disable a plugintracer.use('fs', false)
Or pass plugin configuration directly to init():
require('dd-trace').init({ plugins: true, // enable all built-in plugins (default)})