API Reference

dd-trace

Datadog APM tracing library for Node.js that auto-instruments common libraries and supports custom spans.

Official Documentation ↗
CompatibilityThis documentation covers version v5. Check the official docs for the latest API surface.

Installation

npmnpm install dd-trace
pnpmpnpm add dd-trace

Methods

Overview

dd-trace is a Datadog APM tracing library for Node.js that auto-instruments common libraries and supports custom spans. This reference lists the most frequently used APIs with accurate signatures and runnable snippets.

Core APIs

  • tracer.init — Initializes tracing and auto-instrumentation; must run before other modules are imported.
  • tracer.startSpan — Manually creates a span you finish yourself for custom operations.
  • tracer.trace — Wraps a function in a span, handling activation and finishing automatically.

Example

const tracer = require('dd-trace').init({
  service: 'users-api',
  env: process.env.NODE_ENV,
  logInjection: true,
});

Notes

Initialization order is critical: init must precede other imports or some libraries will not be instrumented. Sampling controls cost.

Verification

Initialize tracing, make a request, and confirm a span for it appears in the trace output or local agent.

FAQ

Why are my traces missing early library calls?
Init must run at the very top of the entry file before other requires, because auto-instrumentation patches modules at load time.
How do I correlate logs with traces?
Enable logInjection so trace and span ids are added to log records, letting logs and traces line up.
How do I reduce trace volume?
Use sampling rules through DD_TRACE_SAMPLE_RATE or the tracer sample method to keep high-volume endpoints affordable.