{"id":18488402,"url":"https://github.com/willsoto/nestjs-prometheus","last_synced_at":"2025-05-15T15:05:10.619Z","repository":{"id":36453674,"uuid":"223280251","full_name":"willsoto/nestjs-prometheus","owner":"willsoto","description":"NestJS module for Prometheus","archived":false,"fork":false,"pushed_at":"2024-05-02T01:50:54.000Z","size":6522,"stargazers_count":437,"open_issues_count":5,"forks_count":24,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-05-02T05:31:25.232Z","etag":null,"topics":["metrics","metrics-gathering","nestjs","prometheus","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/willsoto.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["willsoto"],"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2019-11-21T22:55:47.000Z","updated_at":"2024-05-03T23:28:03.124Z","dependencies_parsed_at":"2023-09-24T19:27:16.174Z","dependency_job_id":"972cec53-01f7-4f89-99b0-ca1e608c1247","html_url":"https://github.com/willsoto/nestjs-prometheus","commit_stats":{"total_commits":2089,"total_committers":14,"mean_commits":"149.21428571428572","dds":0.5844901866921972,"last_synced_commit":"634653b7fa9af8621d1b3c44a0ad90432784cf09"},"previous_names":[],"tags_count":36,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willsoto%2Fnestjs-prometheus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willsoto%2Fnestjs-prometheus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willsoto%2Fnestjs-prometheus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willsoto%2Fnestjs-prometheus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/willsoto","download_url":"https://codeload.github.com/willsoto/nestjs-prometheus/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248168953,"owners_count":21058931,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["metrics","metrics-gathering","nestjs","prometheus","typescript"],"created_at":"2024-11-06T12:51:41.748Z","updated_at":"2025-04-10T06:33:46.885Z","avatar_url":"https://github.com/willsoto.png","language":"TypeScript","funding_links":["https://github.com/sponsors/willsoto"],"categories":["TypeScript"],"sub_categories":[],"readme":"# NestJS Prometheus\n\n![](https://github.com/willsoto/nestjs-prometheus/workflows/tests/badge.svg)\n\n\u003c!-- toc --\u003e\n\n- [Installation](#installation)\n- [Usage](#usage)\n  - [Changing the metrics http endpoint](#changing-the-metrics-http-endpoint)\n  - [Disabling default metrics collection](#disabling-default-metrics-collection)\n  - [Configuring the default metrics](#configuring-the-default-metrics)\n- [Injecting individual metrics](#injecting-individual-metrics)\n- [Setting default labels](#setting-default-labels)\n- [Prefixing custom metrics](#prefixing-custom-metrics)\n  - [Option 1 (recommended)](#option-1-recommended)\n  - [Option 2 (not recommended)](#option-2-not-recommended)\n- [Available metrics](#available-metrics)\n  - [Counter](#counter)\n  - [Gauge](#gauge)\n  - [Histogram](#histogram)\n  - [Summary](#summary)\n- [Providing a custom controller](#providing-a-custom-controller)\n- [Pushgateway](#pushgateway)\n\n\u003c!-- tocstop --\u003e\n\n## Installation\n\n```bash\nyarn add @willsoto/nestjs-prometheus prom-client\n```\n\n```bash\nnpm install @willsoto/nestjs-prometheus prom-client\n```\n\n## Usage\n\n```typescript\nimport { Module } from \"@nestjs/common\";\nimport { PrometheusModule } from \"@willsoto/nestjs-prometheus\";\n\n@Module({\n  imports: [PrometheusModule.register()],\n})\nexport class AppModule {}\n```\n\nBy default, this will register a `/metrics` endpoint that will return the [default metrics](https://github.com/siimon/prom-client#default-metrics).\n\n### Changing the metrics http endpoint\n\n```typescript\nimport { Module } from \"@nestjs/common\";\nimport { PrometheusModule } from \"@willsoto/nestjs-prometheus\";\n\n@Module({\n  imports: [\n    PrometheusModule.register({\n      path: \"/mymetrics\",\n    }),\n  ],\n})\nexport class AppModule {}\n```\n\n### Disabling default metrics collection\n\n```typescript\nimport { Module } from \"@nestjs/common\";\nimport { PrometheusModule } from \"@willsoto/nestjs-prometheus\";\n\n@Module({\n  imports: [\n    PrometheusModule.register({\n      defaultMetrics: {\n        enabled: false,\n      },\n    }),\n  ],\n})\nexport class AppModule {}\n```\n\n### Configuring the default metrics\n\n```typescript\nimport { Module } from \"@nestjs/common\";\nimport { PrometheusModule } from \"@willsoto/nestjs-prometheus\";\n\n@Module({\n  imports: [\n    PrometheusModule.register({\n      defaultMetrics: {\n        // See https://github.com/siimon/prom-client#configuration\n        config: {},\n      },\n    }),\n  ],\n})\nexport class AppModule {}\n```\n\n## Injecting individual metrics\n\n```typescript\n// module.ts\nimport { Module } from \"@nestjs/common\";\nimport {\n  PrometheusModule,\n  makeCounterProvider,\n} from \"@willsoto/nestjs-prometheus\";\nimport { Service } from \"./service\";\n\n@Module({\n  imports: [PrometheusModule.register()],\n  providers: [\n    Service,\n    makeCounterProvider({\n      name: \"metric_name\",\n      help: \"metric_help\",\n    }),\n  ],\n})\nexport class AppModule {}\n```\n\n```typescript\n// service.ts\nimport { Injectable } from \"@nestjs/common\";\nimport { InjectMetric } from \"@willsoto/nestjs-prometheus\";\nimport { Counter } from \"prom-client\";\n\n@Injectable()\nexport class Service {\n  constructor(@InjectMetric(\"metric_name\") public counter: Counter\u003cstring\u003e) {}\n}\n```\n\n## Setting default labels\n\n```typescript\nimport { Module } from \"@nestjs/common\";\nimport { PrometheusModule } from \"@willsoto/nestjs-prometheus\";\n\n@Module({\n  imports: [\n    PrometheusModule.register({\n      defaultLabels: {\n        app: \"My app\",\n      },\n    }),\n  ],\n})\nexport class AppModule {}\n```\n\nSee the [docs](https://github.com/siimon/prom-client#default-labels-segmented-by-registry) for more information.\n\n## Prefixing custom metrics\n\nYou can add a custom prefix to all custom metrics by providing the `customMetricPrefix` option to the module configuration.\n\nSome caveats:\n\nIn order to have the custom metrics registered in different modules from where the `PrometheusModule` was registered, you must do one of a few things:\n\n### Option 1 (recommended)\n\n1. Add the `PrometheusModule` to the `exports` of the registering `Module`. It may be useful to create a `CommonModule` that registers and exports the `PrometheusModule`.\n2. Import that module into whatever module you are creating the custom metrics.\n\n### Option 2 (not recommended)\n\n1. Mark the `PrometheusModule` as `global`\n\n## Available metrics\n\n\u003c!-- Prettier will delete these imports as they are unused. So we ignore these blocks. --\u003e\n\u003c!-- prettier-ignore-start --\u003e\n#### [Counter](https://github.com/siimon/prom-client#counter)\n\n```typescript\nimport { makeCounterProvider } from \"@willsoto/nestjs-prometheus\";\n```\n\n#### [Gauge](https://github.com/siimon/prom-client#gauge)\n\n```typescript\nimport { makeGaugeProvider } from \"@willsoto/nestjs-prometheus\";\n```\n\n#### [Histogram](https://github.com/siimon/prom-client#histogram)\n\n```typescript\nimport { makeHistogramProvider } from \"@willsoto/nestjs-prometheus\";\n```\n\n#### [Summary](https://github.com/siimon/prom-client#summary)\n\n```typescript\nimport { makeSummaryProvider } from \"@willsoto/nestjs-prometheus\";\n```\n\u003c!-- prettier-ignore-end --\u003e\n\n## Providing a custom controller\n\nIf you need to implement any special logic or have access to the controller (e.g., to customize [Swagger](https://docs.nestjs.com/openapi/introduction)),\nyou can provide your own controller (or subclass) of the default controller.\n\nHere is a basic example which should be enough to extend or customize in any way you might need.\n\n```typescript\n// my-custom-controller.ts\nimport { Controller, Get, Res } from \"@nestjs/common\";\nimport { PrometheusController } from \"@willsoto/nestjs-prometheus\";\nimport { Response } from \"express\";\n\n@Controller()\nclass MyCustomController extends PrometheusController {\n  @Get()\n  async index(@Res({ passthrough: true }) response: Response) {\n    return super.index(response);\n  }\n}\n```\n\n```typescript\nimport { Module } from \"@nestjs/common\";\nimport { PrometheusModule } from \"@willsoto/nestjs-prometheus\";\nimport { MyCustomController } from \"./my-custom-controller\";\n\n@Module({\n  imports: [\n    PrometheusModule.register({\n      controller: MyCustomController,\n    }),\n  ],\n})\nexport class AppModule {}\n```\n\n## Pushgateway\n\nIn order to enable Pushgateway for injection, provide the configuration under the `pushgateway` key.\n\n```typescript\nimport { Module } from \"@nestjs/common\";\nimport { PrometheusModule } from \"@willsoto/nestjs-prometheus\";\n\n@Module({\n  imports: [\n    PrometheusModule.register({\n      pushgateway: {\n        url: \"http://127.0.0.1:9091\",\n      },\n    }),\n  ],\n})\nexport class AppModule {}\n```\n\n```typescript\nimport { Injectable } from \"@nestjs/common\";\nimport * as client from \"prom-client\";\n\n@Injectable()\nexport class Service {\n  constructor(private readonly pushgateway: client.Pushgateway) {}\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwillsoto%2Fnestjs-prometheus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwillsoto%2Fnestjs-prometheus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwillsoto%2Fnestjs-prometheus/lists"}