Hot Shots StatsD Client

14.3.1 · active · verified Wed Apr 22

Hot Shots is a robust and actively maintained Node.js client designed for sending metrics to StatsD, DogStatsD (Datadog), Telegraf, and OpenTelemetry Collector StatsD receivers. As of version 14.3.1, it provides comprehensive support for various protocols including UDP, Unix Domain Sockets (UDS), and TCP, catering to diverse deployment environments. The library differentiates itself from its `node-statsd` origin by offering advanced features such as TypeScript types, raw stream protocol, child clients for scoped metrics, mock mode for testing without real sockets, and asynchronous timer methods. It is regularly updated, with a stable release cadence, and requires Node.js 18.x or higher, and TypeScript 4.0+ for its type definitions. Its extensive configuration options allow for fine-grained control over host, port, prefixes, suffixes, global tags, and automatic Datadog tag inclusion from environment variables.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates initializing a StatsD client with common options, sending various metric types (increment, gauge, timer), and sending a DogStatsD event. It also includes proper client cleanup.

import StatsD, { StatsDOptions } from 'hot-shots';

const options: StatsDOptions = {
  host: process.env.STATSD_HOST ?? '127.0.0.1',
  port: parseInt(process.env.STATSD_PORT ?? '8125', 10),
  prefix: 'my_app.',
  globalTags: ['env:dev', 'service:api'],
  mock: false, // Set to true for testing without sending real metrics
  protocol: 'udp' // Or 'tcp', 'uds'
};

const client = new StatsD(options);

// Increment a counter
client.increment('requests.total');

// Gauge a value
client.gauge('memory.usage', process.memoryUsage().heapUsed);

// Time a function execution
const timer = client.startTimer();
setTimeout(() => {
  client.endTimer(timer, 'operation.duration');
}, 100);

// Send a custom event (DogStatsD specific)
client.event('Deployment Alert', 'New version deployed to production', {
  tags: ['version:1.0.1'],
  alert_type: 'success'
});

console.log('Metrics sent: requests.total, memory.usage, operation.duration, Deployment Alert');

// Best practice: close the client when the application shuts down
// For UDP, this might not be strictly necessary, but good for TCP/UDS.
process.on('SIGTERM', () => {
  client.close();
  console.log('StatsD client closed.');
});

view raw JSON →