https://github.com/socketsomeone/nestjs-hot-shots
🐶 Hot-shots Module for Nest.js Framework
https://github.com/socketsomeone/nestjs-hot-shots
datadog hot-shots metrics nest nestjs telegraf typescript
Last synced: 7 months ago
JSON representation
🐶 Hot-shots Module for Nest.js Framework
- Host: GitHub
- URL: https://github.com/socketsomeone/nestjs-hot-shots
- Owner: SocketSomeone
- License: mit
- Created: 2021-12-05T12:28:00.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-11-22T22:58:56.000Z (over 1 year ago)
- Last Synced: 2024-11-24T20:49:50.146Z (over 1 year ago)
- Topics: datadog, hot-shots, metrics, nest, nestjs, telegraf, typescript
- Language: TypeScript
- Homepage: https://npmjs.com/package/nestjs-hot-shots
- Size: 2.69 MB
- Stars: 18
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
# NestJS Hot-shots    

Hot-shots Module for Nest.js Framework. A Node.js client for [Etsy](http://etsy.com)'s [StatsD](https://github.com/statsd/statsd) server,
Datadog's [DogStatsD](https://docs.datadoghq.com/developers/dogstatsd/?tab=hostagent) server,
and [InfluxDB's](https://github.com/influxdata/telegraf) Telegraf
StatsD server.
**Features**
- TypeScript types
- Telegraf support
- Events
- Child clients
- TCP/UDS Protocol support
- Raw Stream Protocol support
- Mock mode
- Much more, including many bug fixes
For questions and support please use
the [Issues](https://github.com/SocketSomeone/nestjs-hot-shots/issues/new?assignees=&labels=question&template=question.yml).
## Installation
```bash
$ npm i nestjs-hot-shots hot-shots
$ yarn add nestjs-hot-shots hot-shots
$ pnpm add nestjs-hot-shots hot-shots
```
## Usage
Once the installation process is complete, we can import the `HotShotsModule` into the root `AppModule`:
```typescript
import { Module } from '@nestjs/common'
import { HotShotsModule } from 'nestjs-hot-shots';
@Module({
imports: [
HotShotsModule.forRoot({
port: 8020,
globalTags: { env: process.env.NODE_ENV }
})
]
})
export class AppModule {
}
```
Then inject `StatsD` provider for use `hot-shots`:
```typescript
import { Injectable } from '@nestjs/common';
import { StatsD } from 'hot-shots';
@Injectable()
export class AppMetrics {
public constructor(private readonly metrics: StatsD) {
}
public metricStuff() {
this.metrics.increment('somecounter');
}
}
```
### Metrics
You can use the `MetricsService` for metrics collection. It`s factory for creating metrics. It provides a set of methods to create different
types of metrics, such as counters, gauges, and histograms.
```typescript
import { Controller, Post } from '@nestjs/common';
import { MetricsService } from 'nestjs-hot-shots';
import { StatsD } from 'hot-shots';
@Controller
export class BooksController {
private readonly booksAdded = this.metricsService.getCounter('books.added.count');
public constructor(private readonly metricsService: MetricsService) {
}
@Post()
public async addBook() {
// some logic
this.booksAdded.add();
}
}
```
| Method | Description |
|----------------------------------|--------------------------------------------------------|
| `getCounter(name: string)` | Returns a counter metric with the given name. |
| `getGauge(name: string)` | Returns a gauge metric with the given name. |
| `getHistogram(name: string)` | Returns a histogram metric with the given name. |
| `getTimer(name: string)` | Returns a timer metric with the given name. |
| `getUpDownCounter(name: string)` | Returns an up-down counter metric with the given name. |
### HTTP Metrics via Middleware
You can use the `HttpMetricsMiddleware` to collect HTTP metrics. It will automatically collect metrics for all incoming requests and
outgoing responses.
```typescript
import { Module } from '@nestjs/common';
import { HotShotsModule } from 'nestjs-hot-shots';
import { HttpMetricsMiddleware } from 'nestjs-hot-shots';
@Module({
imports: [
HotShotsModule.forRoot({
...
})
]
})
export class AppModule {
public configure(consumer: MiddlewareConsumer) {
consumer
.apply(HttpMetricsMiddleware)
.forRoutes('*');
}
}
```
| Metric | Description | Type |
|--------------------------------------|-------------------------------------------------|-----------|
| `http_server_request_count` | Total number of requests received by the server | Counter |
| `http_server_response_count` | Total number of responses sent by the server | Counter |
| `http_server_duration` | Total time taken to process requests | Histogram |
| `http_server_request_size` | Size of incoming bytes. | Histogram |
| `http_server_response_size` | Size of outgoing bytes. | Histogram |
| `http_server_response_success_count` | Total number of all successful responses. | Counter |
| `http_server_response_error_count` | Total number of server error responses. | Counter |
| `http_client_request_error_count` | Total number of client error requests. | Counter |
| `http_server_abort_count` | Total number of aborted requests | Counter |
> Inspired by [nestjs-otel](https://github.com/pragmaticivan/nestjs-otel)
See the [hot-shots](https://www.npmjs.com/package/hot-shots) module for more details.
## Stay in touch
* Author - [Alexey Filippov](https://t.me/socketsomeone)
* Twitter - [@SocketSomeone](https://twitter.com/SocketSomeone)
## License
[MIT](https://github.com/SocketSomeone/nestjs-hot-shots/blob/master/LICENSE) © [Alexey Filippov](https://github.com/SocketSomeone)