Hot Shots StatsD Client
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
-
Error: getaddrinfo ENOTFOUND <host>
cause The configured host for the StatsD agent could not be resolved by DNS or is unreachable.fixVerify the `host` option in your StatsD client configuration. Ensure the hostname is correct, the DNS server is reachable, or the IP address is valid and the agent is running. -
TypeError: StatsD is not a constructor
cause Attempting to instantiate `StatsD` incorrectly due to CommonJS/ESM module mismatch or incorrect import syntax.fixFor ESM, use `import StatsD from 'hot-shots';`. For CommonJS, use `const StatsD = require('hot-shots');`. Do not use destructuring or named imports for the main `StatsD` class. -
TS2307: Cannot find module 'hot-shots' or its corresponding type declarations.
cause TypeScript compiler cannot locate the type definitions for the `hot-shots` package.fixEnsure `hot-shots` is installed (`npm install hot-shots`) and your `tsconfig.json` is correctly configured to include `node_modules/@types` (usually by default) and a TypeScript version of 4.0 or higher. -
Metrics are not appearing in monitoring system.
cause Incorrect host, port, protocol, or a firewall blocking UDP/TCP traffic to the StatsD agent.fixDouble-check the `host`, `port`, and `protocol` options in your `hot-shots` client configuration. Verify that the StatsD agent is running and listening on the specified port and that no firewall rules are preventing communication.
Warnings
- breaking hot-shots officially supports Node.js version 18.x and higher. Running on older Node.js versions may lead to unexpected behavior or compatibility issues.
- breaking When using TypeScript types, hot-shots requires TypeScript 4.0 or higher. Older TypeScript versions may not correctly interpret the provided type definitions.
- gotcha The `mock` option creates a mock StatsD instance with an internal buffer (`mockBuffer`) that stores all generated stats. This buffer continuously grows and is not automatically cleared, which can lead to excessive memory consumption if used in long-running tests or production environments.
- gotcha The `tagPrefix` and `tagSeparator` configuration options are specifically designed for DogStatsD format and do not work when the `telegraf` option is enabled, as Telegraf uses a different tag serialization format.
- gotcha If `host` is not explicitly set in the client options, the constructor first attempts to retrieve it from the `DD_AGENT_HOST` environment variable. If `DD_AGENT_HOST` is also undefined, Node.js's `dgram` module defaults to `127.0.0.1` (IPv4) or `::1` (IPv6) for UDP/datagram sockets. This implicit default might not be the intended target for your StatsD agent.
Install
-
npm install hot-shots -
yarn add hot-shots -
pnpm add hot-shots
Imports
- StatsD
import { StatsD } from 'hot-shots';import StatsD from 'hot-shots';
- StatsD (CommonJS)
const { StatsD } = require('hot-shots');const StatsD = require('hot-shots'); - StatsDOptions
import { StatsDOptions } from 'hot-shots';import type { StatsDOptions } from 'hot-shots';
Quickstart
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.');
});