dd-trace: Datadog APM Tracer for Node.js

5.97.0 · active · verified Sun Apr 19

dd-trace is the official Datadog APM tracing client library for Node.js applications, designed to automatically and manually capture performance monitoring data. It provides distributed tracing capabilities, allowing developers to instrument their Node.js services to gain insights into request lifecycles, identify bottlenecks, and monitor service health within the Datadog platform. The current stable release line is `v5`, with the latest version being `5.97.0`. Datadog maintains a regular release cadence for patch and minor versions, often weekly or bi-weekly, to introduce new features, improvements, and bug fixes, while major versions are released less frequently, with `v5` being released in January 2024. A key differentiator is its deep integration with the Datadog ecosystem, offering automatic instrumentation for popular Node.js frameworks and libraries, support for Single-Step Install (SSI), and Kubernetes Injection. It requires a Datadog Agent to be running to collect and forward the captured trace data.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart initializes the Datadog tracer, sets up a basic Express.js server, and demonstrates both automatic (Express request) and manual (`custom.db.query`) span creation with custom tags and error handling. It highlights the importance of `tracer.init()` and provides guidance on checking the Datadog Agent connectivity.

import tracer from 'dd-trace';
import express from 'express';

// Initialize the Datadog tracer early in your application lifecycle.
// Configuration can also be done via environment variables (DD_SERVICE, DD_ENV, DD_VERSION).
tracer.init({
  service: process.env.DD_SERVICE || 'my-node-app',
  env: process.env.DD_ENV || 'development',
  version: process.env.DD_VERSION || '1.0.0',
  logInjection: true,
  debug: false // Set to true for verbose logging
});

const app = express();
const port = process.env.PORT || 3000;

app.get('/', async (req, res) => {
  // Automatic instrumentation will create a span for the Express request.
  // We can also create a custom span manually within it.
  const customSpan = tracer.startSpan('custom.db.query', {
    tags: { 'db.name': 'users_db', 'db.statement': 'SELECT * FROM users' }
  });

  try {
    await new Promise(resolve => setTimeout(resolve, 100)); // Simulate async DB call
    customSpan.addTags({ 'db.row_count': 5, 'http.status_code': 200 });
    res.send('Data retrieved successfully via Datadog Tracing!');
  } catch (error) {
    customSpan.setTag('error', true);
    customSpan.log({ 'event': 'error', 'error.message': error.message });
    res.status(500).send('Error during data retrieval!');
  } finally {
    customSpan.finish();
  }
});

app.listen(port, () => {
  console.log(`Server listening on port ${port}`);
  console.log(`Datadog Tracer initialized for service: ${tracer.dogstatsd.service}`);
  console.log(`Ensure a Datadog Agent is running and accessible at DD_AGENT_HOST:${process.env.DD_AGENT_PORT || 8126}`);
});

view raw JSON →