0.67.0 • Published 3 months ago

@rnw-community/nestjs-rxjs-metrics v0.67.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

NestJS RxJS metrics

NestJS prometheus metrics wrapper for using with RxJS streams with full TypeScript support.

npm version npm downloads

Installation

Install additional peer dependencies:

Configuration

  • Create custom metrics objects for each type of metrics, this is needed for safe TS usage inside the service operators:
export const counterMetrics = { my_counter_metric: 'My counter metric description' };
export const gaugeMetrics = { my_gauge_metric: 'My gauge metric description' };
export const histogramMetrics = { my_histogram_metric: 'My histogram metric description' };
export const summaryMetrics = { my_summary_metric: 'My summary metric description' };
  • Create custom histogram and summary metrics labels objects, this is needed for safe TS usage inside the service operators:

Using [...] as const is important for TS type checking to work

const histogramLabels = {
    my_histogram_metric: ['my_histogram_metric_label'] as const,
};

const summaryLabels = {
    my_summary_metric: ['my_summary_label'] as const,
};
  • Create metrics module and service for NestJS DI, this module and service should be used in the project:
import { Inject, Injectable } from '@nestjs/common';

import { MetricsServiceMixin } from '@rnw-community/nestjs-rxjs-metrics';

import type { counterMetrics } from './counter.metrics';
import type { gaugeMetrics } from './gauge.metrics';
import type { histogramMetrics } from './histogram.metrics';
import type { summaryMetrics } from './summary.metrics';

export const [BaseMetricsModule, BaseMetricsService] = NestJSRxJSMetricsModule.create({
    counterMetrics,
    gaugeMetrics,
    histogramMetrics,
    summaryMetrics,
    summaryLabels,
    histogramLabels,
    controller: PrometheusController,
});

@Injectable()
export class MetricsService extends BaseMetricsService {}

@Module({
    imports: [BaseMetricsModule],
    providers: [MetricsService],
    exports: [MetricsService],
})
export class MetricsModule {}

Usage

This package provides a set of RxJS operators for beautiful usage of prometheus metrics inside the streams. Take a look at best practises and other useful docs from official prometheus documentation.

Counter

Counter supports next operator:

  • counter(CounterMetric, value = 1) operator - increment counter metric by value
const counterMetrics = { my_counter_metric: 'Text counter metric' };

@Injectable()
export class MyService {
    constructor(private readonly metrics: MetricsService) {}

    externalAction$() {
        return of(true).pipe(
            // perform actions
            this.metrics.counter('my_counter_metric' /* defaut value is 1, you can proide another number */)
        );
    }
}

Gauge

Gauge supports next operators:

  • gauge(GaugeMetric, handler: (gauge: Gauge<string>) => void) operator - observe Gauge metric and perform callback on it
  • gaugeInc(GaugeMetric, value = 1) => void) operator - increment Gauge metric by value
  • gaugeDec(GaugeMetric, value = 1) => void) operator - decrement Gauge metric by value
const gaugeMetrics = { my_gauge_metric: 'Text gauge metric' };

@Injectable()
export class MyService {
    constructor(private readonly metrics: MetricsService) {}

    activateAction$() {
        return of(true).pipe(
            // perform actions
            this.metrics.gaugeInc('my_gauge_metric' /* defaut value is 1, you can proide another number */)
        );
    }

    deactivateAction$() {
        return of(true).pipe(
            // perform actions
            this.metrics.gaugeDec('my_gauge_metric' /* defaut value is 1, you can proide another number */)
        );
    }
}

Histogram

Histogram supports next operators:

  • histogramStart(HistogramMetric, labels?: LabelValues<L>) operator - start observing Histogram metric with labels
  • histogramEnd(HistogramMetric, labels?: LabelValues<L>)) operator - finish observing Histogram metric with labels
const histogramMetrics = { my_histogram_metric: 'Text histogram metric' };
const histogramLabels = { my_histogram_metric: ['my_histogram_metric_label1'] as const };

@Injectable()
export class MyService {
    constructor(private readonly metrics: MetricsService) {}

    exampleAction$() {
        return of(true).pipe(
            this.metrics.histogramStart('my_histogram_metric', { my_histogram_metric_label1: 1 }),
            // perform actions
            this.metrics.histogramEnd('my_histogram_metric', { my_histogram_metric_label1: 2 })
        );
    }
}

Summary

Summary supports next operators:

  • histogramStart(SumaryMetric, labels?: LabelValues<L>) operator - start observing Summary metric with labels
  • histogramEnd(SummaryMetric, labels?: LabelValues<L>)) operator - finish observing Summary metric with labels
const summaryMetrics = { my_summary_metric: 'Text summary metric' };
const summaryLabels = { my_summary_metric: ['my_summary_metric_label1'] as const };

@Injectable()
export class MyService {
    constructor(private readonly metrics: MetricsService) {}

    exampleAction$() {
        return of(true).pipe(
            this.metrics.histogramStart('my_summary_metric', { my_summary_metric_label1: 1 }),
            // perform actions
            this.metrics.histogramEnd('my_summary_metric', { my_summary_metric_label1: 2 })
        );
    }
}

License

This library is licensed under The MIT License.

0.67.0

3 months ago

0.66.0

3 months ago

0.65.2

4 months ago

0.65.3

4 months ago

0.64.0

5 months ago

0.65.0

5 months ago

0.63.0

5 months ago

0.62.1

6 months ago

0.62.0

7 months ago

0.62.2

6 months ago

0.59.3

7 months ago

0.55.0

7 months ago

0.59.6

7 months ago

0.59.4

7 months ago

0.59.5

7 months ago

0.56.0

7 months ago

0.60.0

7 months ago

0.57.2

7 months ago

0.57.3

7 months ago

0.57.0

7 months ago

0.57.1

7 months ago

0.54.12

10 months ago

0.61.0

7 months ago

0.53.0

1 year ago

0.51.0

1 year ago

0.52.3

1 year ago

0.54.0

1 year ago

0.52.0

1 year ago

0.50.0

1 year ago

0.49.1

1 year ago

0.49.2

1 year ago

0.49.0

1 year ago

0.43.0

2 years ago

0.48.2

1 year ago

0.48.0

2 years ago

0.46.0

2 years ago

0.44.0

2 years ago

0.42.0

2 years ago

0.42.1

2 years ago

0.47.0

2 years ago

0.43.1

2 years ago

0.45.0

2 years ago

0.43.2

2 years ago

0.41.0

2 years ago

0.38.2

2 years ago

0.38.1

2 years ago

0.38.0

2 years ago

0.36.1

2 years ago

0.36.0

2 years ago

0.34.0

2 years ago

0.32.1

2 years ago

0.38.3

2 years ago

0.32.0

2 years ago

0.40.0

2 years ago

0.39.0

2 years ago

0.37.0

2 years ago

0.35.1

2 years ago

0.35.0

2 years ago

0.33.0

2 years ago

0.31.0

2 years ago

0.21.0

2 years ago

0.20.0

2 years ago

0.19.0

2 years ago

0.18.1

2 years ago

0.19.1

2 years ago

0.18.3

2 years ago

0.18.4

2 years ago

0.11.0

2 years ago

0.12.0

2 years ago

0.13.0

2 years ago

0.12.1

2 years ago

0.14.0

2 years ago

0.15.0

2 years ago

0.14.1

2 years ago

0.16.0

2 years ago

0.14.2

2 years ago

0.17.0

2 years ago

0.18.0

2 years ago

0.17.1

2 years ago

0.30.0

2 years ago

0.29.0

2 years ago

0.27.2

2 years ago

0.28.0

2 years ago

0.27.1

2 years ago

0.27.0

2 years ago

0.26.1

2 years ago

0.26.0

2 years ago

0.24.2

2 years ago

0.25.0

2 years ago

0.24.1

2 years ago

0.24.0

2 years ago

0.23.1

2 years ago

0.23.0

2 years ago

0.22.0

2 years ago

0.27.4

2 years ago

0.27.3

2 years ago

0.10.0

2 years ago

0.9.0

2 years ago

0.8.0

3 years ago

0.7.5

3 years ago

0.7.3

3 years ago

0.7.2

3 years ago