{"library":"nestjs-hot-shots","title":"NestJS Hot-shots Module","description":"The nestjs-hot-shots package provides a dedicated module for integrating the `hot-shots` (StatsD client) library into NestJS applications. It simplifies sending metrics to StatsD, DogStatsD, or Telegraf servers. Currently stable at version 4.0.0 (released January 2026), it typically sees releases aligning with major NestJS versions or significant feature additions/bug fixes. Key differentiators include full TypeScript support, out-of-the-box integration for common NestJS patterns like dependency injection for the `StatsD` client, a dedicated `MetricsService` for creating various metric types (counters, gauges, histograms), and a convenient `HttpMetricsMiddleware` for automated HTTP request metric collection. This module abstracts much of the boilerplate associated with setting up and using a StatsD client in a NestJS project, offering a streamlined experience for metric reporting.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install nestjs-hot-shots"],"cli":null},"imports":["import { HotShotsModule } from 'nestjs-hot-shots';","import { StatsD } from 'hot-shots';","import { MetricsService } from 'nestjs-hot-shots';","import { HttpMetricsMiddleware } from 'nestjs-hot-shots';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { Injectable, Module, MiddlewareConsumer, NestModule } from '@nestjs/common';\nimport { HotShotsModule, MetricsService, HttpMetricsMiddleware } from 'nestjs-hot-shots';\nimport { StatsD } from 'hot-shots';\n\n@Injectable()\nexport class AppMetrics {\n    private readonly processedEventsCounter;\n\n    public constructor(\n        private readonly metricsClient: StatsD,\n        private readonly metricsService: MetricsService\n    ) {\n        this.processedEventsCounter = this.metricsService.getCounter('app.events.processed');\n    }\n\n    public recordEventProcessed() {\n        this.metricsClient.increment('app.custom_metric');\n        this.processedEventsCounter.add();\n    }\n\n    public sendGauge(value: number) {\n        this.metricsClient.gauge('app.current_value', value);\n    }\n}\n\n@Module({\n    imports: [\n        HotShotsModule.forRoot({\n            host: process.env.STATSD_HOST ?? '127.0.0.1',\n            port: parseInt(process.env.STATSD_PORT ?? '8125', 10),\n            globalTags: { env: process.env.NODE_ENV ?? 'development', service: 'my-nest-app' },\n            // Optional: Enable mock mode for testing without a StatsD server\n            mock: process.env.NODE_ENV === 'test'\n        })\n    ],\n    providers: [AppMetrics],\n    exports: [AppMetrics]\n})\nexport class AppModule implements NestModule {\n    public configure(consumer: MiddlewareConsumer) {\n        consumer\n            .apply(HttpMetricsMiddleware)\n            .forRoutes('*'); // Apply HTTP metrics middleware to all routes\n    }\n}\n\n// Example usage (e.g., in main.ts or another module)\n// async function bootstrap() {\n//   const app = await NestFactory.create(AppModule);\n//   const appMetrics = app.get(AppMetrics);\n//   appMetrics.recordEventProcessed();\n//   appMetrics.sendGauge(42);\n//   await app.listen(3000);\n// }\n// bootstrap();","lang":"typescript","description":"Demonstrates how to configure the `HotShotsModule` with environment variables, inject the underlying `StatsD` client, use the `MetricsService` for custom metrics, and apply `HttpMetricsMiddleware` for automated HTTP request tracking.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}