@digikare/nestjs-prom

中文文档
1.0.0发布于 2020/10/24MITTypeScriptCommonJS近 30 天下载 7337

A promotheus module for nestjs

原始使用文档
文档目录12 节
  1. Description
  2. Installation
  3. How to use
  4. Setup metric
  5. Metric class instances
  6. Metric method calls
  7. Metric endpoint
  8. API
  9. PromModule.forRoot() options
  10. Auth/security
  11. TODO
  12. License

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/nestjs-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/nestjs-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 http_requests_total 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

版本与发布记录

@digikare/nestjs-prom 共发布 12 个版本,首次发布于 2018/10/02;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.2.52020/10/246 年前
1.0.0latest2020/10/246 年前
0.2.42020/07/296 年前
0.2.32020/07/276 年前
0.2.22019/12/186 年前
0.2.12019/08/267 年前

关于 @digikare/nestjs-prom 的常见问题

@digikare/nestjs-prom 是什么?

A promotheus module for nestjs

如何安装 @digikare/nestjs-prom?

在项目目录运行 npm install @digikare/nestjs-prom,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@nestjs/common(^6.0.0 || ^7.0.0)、@nestjs/core(^6.0.0 || ^7.0.0)、reflect-metadata(^0.1.12)、rxjs(^6.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@digikare/nestjs-prom 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@digikare/nestjs-prom 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@digikare/nestjs-prom 可以商用吗?

@digikare/nestjs-prom 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@digikare/nestjs-prom 的下载量有多少?

根据 npm 官方下载统计,@digikare/nestjs-prom 在 2026/10/12 – 2026/11/10 这 30 天内被下载 7,337 次,日均约 245 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@digikare/nestjs-prom 还在维护吗?

最新版本 1.0.0 发布于 2020/10/24(6 年前);至今共发布 12 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/10/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。