Datadog Metrics Reporter

0.12.1 · active · verified Wed Apr 22

datadog-metrics is a Node.js library designed for buffering and reporting application metrics to Datadog via its HTTP API. It supports common metric types including gauges, increments, histograms, and distributions. The current stable version is 0.12.1, with a pre-release (0.13.0-pre.1) indicating a future focus on reducing the bundle size and resource footprint by decoupling from heavier official Datadog clients. The library features automatic metric flushing before process exit (since v0.12.1), automatic retries for failed submissions (since v0.12.0), and includes built-in TypeScript definitions (since v0.11.0). Its release cadence is moderate, with significant feature updates often accompanied by breaking changes.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates initializing the global metrics reporter and sending various metric types (gauge, increment, histogram, distribution). Also shows how to create and use a separate BufferedMetricsLogger instance for isolated metric collection, and how to perform an explicit flush operation.

import { init, BufferedMetricsLogger } from 'datadog-metrics';

// Initialize the global metrics object
init({
  apiKey: process.env.DD_API_KEY ?? '',
  appKey: process.env.DD_APP_KEY ?? '', // Optional, for advanced features/error details
  host: 'my-app-server',
  prefix: 'my_app.',
  tags: ['env:production', 'region:us-east-1'],
  flushIntervalSeconds: 15,
  // Optional: configure to use custom logger for more control
  // logger: console,
});

// Send some metrics using the global instance
metrics.gauge('user_sessions', 1234, ['browser:chrome']);
metrics.increment('request_count', 1, ['status:200']);
metrics.histogram('api_response_time', 150.5, ['endpoint:/data']);
metrics.distribution('task_duration_ms', 55.2, ['task_name:process_batch']);

console.log('Metrics sent via global instance.');

// Create and use a separate buffered logger instance
const customLogger = new BufferedMetricsLogger({
  apiKey: process.env.DD_API_KEY ?? '',
  host: 'custom-worker',
  prefix: 'worker_process.',
  tags: ['worker_id:42', 'queue:high_prio'],
});

customLogger.gauge('queue_depth', 5);
customLogger.increment('job_processed');
console.log('Metrics sent via custom logger instance.');

// Manual flush (optional, auto-flush on exit since v0.12.1)
// In a real application, you might want to wait for these promises
Promise.all([
  metrics.flush(),
  customLogger.flush()
]).then(() => {
  console.log('All metrics manually flushed.');
}).catch(err => {
  console.error('Error flushing metrics:', err);
});

view raw JSON →