@6river/nestjs-prom 中文文档教程

发布于 4年前 浏览 22 项目主页 更新于 3年前

TEMPORARY FORK TO UPDATE prom-client to 12.0.0

Will be removed once change is accepted upstream

Description

Nest 的普罗米修斯模块。

Installation

$ npm install --save @digikare/nestjs-prom prom-client

How to use

PromModule 导入根 ApplicationModule

import { Module } from '@nestjs/common';
import { PromModule } from '@digikare/nestjs-prom';

@Module({
  imports: [
    PromModule.forRoot({
      defaultLabels: {
        app: 'my_app',
      }
    }),
  ]
})
export class ApplicationModule {}

Setup metric

在您的模块中,使用 forMetrics() 方法定义所需的指标。

import { Module } from '@nestjs/common';
import { PromModule, MetricType } from '@digikare/nest-prom';

@Module({
  imports: [
    PromModule.forMetrics([
      {
        type: MetricType.Counter,
        configuration: {
          name: 'my_counter',
          help: 'my_counter a simple counter',
        }
      },
      {
        type: MetricType.Gauge,
        configuration: {
          name: 'my_gauge',
          help: 'my_gauge a simple gauge',
        }
      },
      {
        type: MetricType.Histogram,
        configuration: {
          name: 'my_histogram',
          help: 'my_histogram a simple histogram',
        }
      },
      {
        type: MetricType.Summary,
        configuration: {
          name: 'my_summary',
          help: 'my_summary a simple summary',
        }
      }
    ]),
  ]
})
export class MyModule

您可以使用 @InjectCounterMetric() 装饰器获取指标

import { Injectable } from '@nestjs/common';
import {
  InjectCounterMetric,
  InjectGaugeMetric,
  InjectHistogramMetric,
  InjectSummaryMetric,
  CounterMetric,
  GaugeMetric,
  HistogramMetric,
  SummaryMetric,
} from '@digikare/nest-prom';

@Injectable()
export class MyService {
  constructor(
    @InjectCounterMetric('my_counter') private readonly _counterMetric: CounterMetric,
    @InjectGaugeMetric('my_gauge') private readonly _gaugeMetric: GaugeMetric,
    @InjectHistogramMetric('my_histogram') private readonly _histogramMetric: HistogramMetric,
    @InjectSummaryMetric('my_summary') private readonly _summaryMetric: SummaryMetric,
  ) {}

  doStuff() {
    this._counterMetric.inc();
  }

  resetCounter() {
    this._counterMetric.reset();
  }
}

Metric class instances

@PromInstanceCounter
export class MyClass {

}

将生成一个名为:app_MyClass_instances_total

Metric method calls

@Injectable()
export class MyService {
  @PromMethodCounter
  doMyStuff() {

  }
}

的计数器 将生成一个名为:app_MyService_doMyStuff_calls_total 的计数器

Metric endpoint

默认值metrics 端点是 /metrics 这可以用 customUrl 选项改变

@Module({
  imports: [
    PromModule.forRoot({
      defaultLabels: {
        app: 'my_app',
      },
      customUrl: 'custom/uri',
    }),
  ],
})
export class MyModule

现在你的指标可以在 /custom/uri 找到。

PS:如果你有全局前缀,路径将为 {globalPrefix}/metrics for 此时此刻。

API

PromModule.forRoot() options

  • withDefaultsMetrics: boolean (default true) enable defaultMetrics provided by prom-client
  • withDefaultController: boolean (default true) add internal controller to expose /metrics endpoints
  • useHttpCounterMiddleware: boolean (default false) register httprequeststotal counter

Auth/security

我不为 /metrics 端点提供任何身份验证/安全性。 这不是本模块的目的,但根据身份验证策略,您可以 在 /metrics 上应用中间件来保护它。

TODO

  • Update readme
  • Gauge
  • Histogram
  • Summary
  • Manage registries
  • Tests
  • Give possibility to custom metric endpoint
  • Adding example on how to secure /metrics endpoint
  • secret
  • jwt

License

麻省理工学院许可

TEMPORARY FORK TO UPDATE prom-client to 12.0.0

Will be removed once change is accepted upstream

Description

A prometheus module for Nest.

Installation

$ npm install --save @digikare/nestjs-prom prom-client

How to use

Import PromModule into the root ApplicationModule

import { Module } from '@nestjs/common';
import { PromModule } from '@digikare/nestjs-prom';

@Module({
  imports: [
    PromModule.forRoot({
      defaultLabels: {
        app: 'my_app',
      }
    }),
  ]
})
export class ApplicationModule {}

Setup metric

In your module, use forMetrics() method to define the metrics needed.

import { Module } from '@nestjs/common';
import { PromModule, MetricType } from '@digikare/nest-prom';

@Module({
  imports: [
    PromModule.forMetrics([
      {
        type: MetricType.Counter,
        configuration: {
          name: 'my_counter',
          help: 'my_counter a simple counter',
        }
      },
      {
        type: MetricType.Gauge,
        configuration: {
          name: 'my_gauge',
          help: 'my_gauge a simple gauge',
        }
      },
      {
        type: MetricType.Histogram,
        configuration: {
          name: 'my_histogram',
          help: 'my_histogram a simple histogram',
        }
      },
      {
        type: MetricType.Summary,
        configuration: {
          name: 'my_summary',
          help: 'my_summary a simple summary',
        }
      }
    ]),
  ]
})
export class MyModule

And you can use @InjectCounterMetric() decorator to get the metrics

import { Injectable } from '@nestjs/common';
import {
  InjectCounterMetric,
  InjectGaugeMetric,
  InjectHistogramMetric,
  InjectSummaryMetric,
  CounterMetric,
  GaugeMetric,
  HistogramMetric,
  SummaryMetric,
} from '@digikare/nest-prom';

@Injectable()
export class MyService {
  constructor(
    @InjectCounterMetric('my_counter') private readonly _counterMetric: CounterMetric,
    @InjectGaugeMetric('my_gauge') private readonly _gaugeMetric: GaugeMetric,
    @InjectHistogramMetric('my_histogram') private readonly _histogramMetric: HistogramMetric,
    @InjectSummaryMetric('my_summary') private readonly _summaryMetric: SummaryMetric,
  ) {}

  doStuff() {
    this._counterMetric.inc();
  }

  resetCounter() {
    this._counterMetric.reset();
  }
}

Metric class instances

@PromInstanceCounter
export class MyClass {

}

Will generate a counter called: app_MyClass_instances_total

Metric method calls

@Injectable()
export class MyService {
  @PromMethodCounter
  doMyStuff() {

  }
}

Will generate a counter called: app_MyService_doMyStuff_calls_total

Metric endpoint

The default metrics endpoint is /metrics this can be changed with the customUrl option

@Module({
  imports: [
    PromModule.forRoot({
      defaultLabels: {
        app: 'my_app',
      },
      customUrl: 'custom/uri',
    }),
  ],
})
export class MyModule

Now your metrics can be found at /custom/uri.

PS: If you have a global prefix, the path will be {globalPrefix}/metrics for the moment.

API

PromModule.forRoot() options

  • withDefaultsMetrics: boolean (default true) enable defaultMetrics provided by prom-client
  • withDefaultController: boolean (default true) add internal controller to expose /metrics endpoints
  • useHttpCounterMiddleware: boolean (default false) register httprequeststotal counter

Auth/security

I do not provide any auth/security for /metrics endpoints. This is not the aim of this module, but depending of the auth strategy, you can apply a middleware on /metrics to secure it.

TODO

  • Update readme
  • Gauge
  • Histogram
  • Summary
  • Manage registries
  • Tests
  • Give possibility to custom metric endpoint
  • Adding example on how to secure /metrics endpoint
  • secret
  • jwt

License

MIT licensed

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文