Node.js StatsD Client (Legacy)

0.1.1 · abandoned · verified Sun Apr 19

This entry describes `node-statsd`, a client library for sending metrics to a StatsD server from Node.js applications. The specific version provided, 0.1.1, is exceptionally old, predating the 1.0.0 release. The package allows sending common StatsD metric types such as timings, increments, decrements, histograms, and gauges over UDP. It offers basic configuration for host, port, prefix, suffix, and optional global tags. Due to its age (last significant update over 7 years ago, with the provided version being even older), this client is considered largely abandoned. Modern Node.js applications should consider more actively maintained alternatives like `hot-shots` or `statsd-client` which offer broader compatibility, better error handling, and more features. The release cadence for this specific package is effectively non-existent.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates initializing the StatsD client with optional parameters and sending various metric types like timing, increment, gauge, histogram, and set, including basic socket error handling.

const StatsD = require('node-statsd');
const client = new StatsD({
  host: process.env.STATSD_HOST ?? 'localhost',
  port: parseInt(process.env.STATSD_PORT ?? '8125', 10),
  prefix: 'my_app.',
  global_tags: ['env:development', 'service:backend']
});

// Timing: records the duration of an event in milliseconds
client.timing('api_response_time', 150.7, 0.5, ['endpoint:users', 'method:GET']);

// Increment: increments a counter by a value (default is 1)
client.increment('user_logins');
client.increment('failed_attempts', 5);

// Decrement: decrements a counter by a value (default is -1)
client.decrement('active_sessions');

// Gauge: sets a metric to an absolute value
client.gauge('current_memory_usage', process.memoryUsage().heapUsed / 1024 / 1024);

// Histogram: sends data for histogram stat
client.histogram('request_duration_ms', 250, 0.1);

// Set: counts unique occurrences of a stat
client.set('unique_visitors', 'user-abc-123');

// Error handling for the underlying socket
client.socket.on('error', (error) => {
  console.error('StatsD socket error:', error);
});

console.log('Metrics sent to StatsD (or attempted, check server logs).');

view raw JSON →