{"id":21035261,"url":"https://github.com/skellla/fastify-metrics","last_synced_at":"2026-04-04T23:04:16.153Z","repository":{"id":33047644,"uuid":"150535701","full_name":"SkeLLLa/fastify-metrics","owner":"SkeLLLa","description":"Prometheus metrics exporter for Fastify","archived":false,"fork":false,"pushed_at":"2024-09-20T15:55:55.000Z","size":1740,"stargazers_count":105,"open_issues_count":13,"forks_count":33,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-02T04:02:55.201Z","etag":null,"topics":["fastify","fastify-plugin","metrics","prometheus"],"latest_commit_sha":null,"homepage":"https://savelife.in.ua/en/donate-en/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SkeLLLa.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-09-27T05:55:28.000Z","updated_at":"2025-03-28T04:59:31.000Z","dependencies_parsed_at":"2023-02-18T12:45:33.356Z","dependency_job_id":"57ace632-a1a0-4bf5-bdbf-116b29018a95","html_url":"https://github.com/SkeLLLa/fastify-metrics","commit_stats":{"total_commits":280,"total_committers":28,"mean_commits":10.0,"dds":0.7142857142857143,"last_synced_commit":"35efa3593483be4b15da01238e41104bdf999f4b"},"previous_names":[],"tags_count":79,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SkeLLLa%2Ffastify-metrics","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SkeLLLa%2Ffastify-metrics/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SkeLLLa%2Ffastify-metrics/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SkeLLLa%2Ffastify-metrics/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SkeLLLa","download_url":"https://codeload.github.com/SkeLLLa/fastify-metrics/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247980837,"owners_count":21027808,"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":["fastify","fastify-plugin","metrics","prometheus"],"created_at":"2024-11-19T13:14:22.119Z","updated_at":"2026-04-04T23:04:16.133Z","avatar_url":"https://github.com/SkeLLLa.png","language":"TypeScript","readme":"# fastify-metrics\n\n\u003cdiv align=\"center\"\u003e\n  \u003cimg src=\"https://github.com/SkeLLLa/fastify-metrics/raw/master/logo.png\" alt=\"fastify-metrics logo\"/\u003e\n\u003c/div\u003e\n\n[![NPM Version](https://img.shields.io/npm/v/fastify-metrics.svg)](https://www.npmjs.com/package/fastify-metrics)\n[![Downloads Count](https://img.shields.io/npm/dm/fastify-metrics.svg)](https://www.npmjs.com/package/fastify-metrics)\n[![Build Status](https://github.com/SkeLLLa/fastify-metrics/workflows/release/badge.svg)](https://github.com/SkeLLLa/fastify-metrics/actions)\n[![License](https://img.shields.io/npm/l/fastify-metrics.svg)](https://gitlab.com/m03geek/fastify-metrics/blob/master/LICENSE)\n[![Codecov](https://img.shields.io/codecov/c/gh/SkeLLLa/fastify-metrics.svg)](https://codecov.io/gh/SkeLLLa/fastify-metrics)\n[![Socket Badge](https://socket.dev/api/badge/npm/package/fastify-metrics)](https://socket.dev/npm/package/fastify-metrics)\n\n[Prometheus](https://prometheus.io/) metrics exporter for Fastify.\n\nThis plugin uses [prom-client](https://github.com/siimon/prom-client) under the hood with optional support for [@platformatic/prom-client](https://github.com/platformatic/prom-client) as a high-performance drop-in replacement.\n\nThis plugin also adds two http metrics for your routes:\n\n- Requests duration histogram\n- Requests duration summary\n\n## ToC\n\n- [fastify-metrics](#fastify-metrics)\n  - [ToC](#toc)\n  - [Fastify support](#fastify-support)\n  - [Notable changes](#notable-changes)\n    - [v12.x.x](#v12xx)\n    - [v11.x.x](#v11xx)\n    - [v10.x.x](#v10xx)\n    - [v9.x.x](#v9xx)\n    - [v6.x.x](#v6xx)\n  - [Installation](#installation)\n  - [Using @platformatic/prom-client](#using-platformaticprom-client)\n  - [Features and requirements](#features-and-requirements)\n  - [Usage](#usage)\n    - [Registry clear](#registry-clear)\n    - [Plugin options](#plugin-options)\n      - [Route metrics](#route-metrics)\n        - [Route metrics enabled](#route-metrics-enabled)\n        - [Route metrics overrides](#route-metrics-overrides)\n          - [Labels](#labels)\n          - [Request durations summary](#request-durations-summary)\n          - [Request durations histogram](#request-durations-histogram)\n    - [HTTP routes metrics in Prometheus](#http-routes-metrics-in-prometheus)\n  - [API Docs](#api-docs)\n  - [Changelog](#changelog)\n  - [See also](#see-also)\n  - [License](#license)\n\n## Fastify support\n\n- **v3.x.x** - supports `fastify-1.x`\n- **v4.x.x** - supports `fastify-2.x` `prom-client-11.x`\n- **v5.x.x** - supports `fastify-2.x` `prom-client-12.x`\n- **v6.x.x** - supports `fastify-3.x`\n- **v9.x.x** - supports `fastify-4.x` `prom-client-14.x`\n- **v11.x.x** - supports `fastify-4.x` `prom-client-15.x`\n- **v12.x.x** - supports `fastify-5.x` `prom-client-15.x`\n\n## Notable changes\n\n### v12.x.x\n\n- Fastify v5 support.\n- Drop node.js 18 support.\n\n### v11.x.x\n\n- Drop node.js 16 support.\n- Upgrade to prom-client 15.1.\n\n### v10.x.x\n\n- Replace route `context.config` with `routeConfig` due to deprecation in fastify v4 and removal in fastify v5. If you had `disableMetrics` option in you route `config`, update fastify to latest version.\n- Prefer `request.routeOptions.method` over deprecated `request.routerMethod`.\n\n### v9.x.x\n\n- Fastify v4 support.\n- Complete config rewrite, default behaviour changed.\n- Support disabling metrics in route config.\n- Now collects metrics only for registered routes by default.\n- Unknown routes metrics collection disabled by default.\n- Removed `metrics` from `request`. Now it uses `WeakMap` and not exposed.\n- Add balcklisting possibility for request methods.\n- Registry overrides moved to metric configuration.\n- Support overriding all Summary and Histogram options for default route metrics.\n\n### v6.x.x\n\n- Fastify v3 support.\n- Drop node.js 8 support.\n- `enableDefaultMetrics` - now enables only default `prom-client` metrics. Set to `true` by default.\n- `enableRouteMetrics` - additional flag that enables route metrics. Set to `true` by default.\n\n## Installation\n\n```sh\nnpm i fastify-metrics --save\npnpm i fastify-metrics --save\n```\n\n\u003csub\u003e[Back to top](#toc)\u003c/sub\u003e\n\n## Using @platformatic/prom-client\n\nThis plugin supports [@platformatic/prom-client](https://github.com/platformatic/prom-client) as an optional drop-in replacement for `prom-client`. It is a performance-focused fork by [Platformatic](https://platformatic.dev/) that provides the same API with lower overhead — optimized internal data structures, reduced memory allocations, and faster metric serialization.\n\n**If `@platformatic/prom-client` is installed, the plugin will use it automatically.** No configuration changes needed. If it's not installed, the plugin falls back to standard `prom-client`.\n\n### Why use it\n\n| | `prom-client` | `@platformatic/prom-client` |\n|---|---|---|\n| **API** | Standard | Same (drop-in compatible) |\n| **Internal storage** | `hashMap` | Optimized `LabelMap` |\n| **Memory allocations** | Standard | Reduced |\n| **Metric serialization** | Standard | Faster |\n| **Node.js support** | \u003e=16 | ^20 \\|\\| ^22 \\|\\| \u003e=24 |\n\n### Installation\n\nJust install it alongside `fastify-metrics`:\n\n```sh\nnpm i @platformatic/prom-client\npnpm i @platformatic/prom-client\n```\n\nThe plugin auto-detects it on startup. No code changes required.\n\n### Explicit client override\n\nYou can also pass the client explicitly via the `promClient` option. This takes precedence over auto-detection:\n\n```js\nimport fastify from 'fastify';\nimport metricsPlugin from 'fastify-metrics';\nimport client from '@platformatic/prom-client';\n\nconst app = fastify();\nawait app.register(metricsPlugin, {\n  endpoint: '/metrics',\n  promClient: client,\n});\n```\n\n### Verifying which client is active\n\nAfter registration, `fastify.metrics.client` holds the resolved prom-client instance. You can check which one is being used:\n\n```js\nawait app.ready();\nconsole.log(app.metrics.client); // the active prom-client module\n```\n\n\u003csub\u003e[Back to top](#toc)\u003c/sub\u003e\n\n## Features and requirements\n\n- Collects default server metrics (see [prom-client](https://github.com/siimon/prom-client/tree/master/lib/metrics));\n- Collects route response timings\n- Adds `metrics` to fastify instance for your custom metrics.\n\n---\n\n- Requires fastify `\u003e=4.0.0`.\n- Node.js `\u003e=20.0.0`.\n\n\u003csub\u003e[Back to top](#toc)\u003c/sub\u003e\n\n## Usage\n\nAdd it to your project like regular fastify plugin. Use `register` method and pass options to it.\n\n```js\nconst fastify = require('fastify');\nconst app = fastify();\n\nconst metricsPlugin = require('fastify-metrics');\nawait app.register(metricsPlugin, { endpoint: '/metrics' });\n```\n\nIt also exports client to fastify instance `fastify.metrics.client` which you may use it in your routes.\n\nYou may create your metrics when app starts and store it in `fastify.metrics` object and reuse them in multiple routes.\n\n### Registry clear\n\nAfter calling `registry.clear()` all metrics are removed from registry. In order to add them again to the registry, call `fastify.metrics.initMetricsInRegistry`.\n\n\u003csub\u003e[Back to top](#toc)\u003c/sub\u003e\n\n### Plugin options\n\nSee for details [docs](docs/api/fastify-metrics.imetricspluginoptions.md)\n\n| Property                                                                                              | Type                                                                                                                | Default Value       |\n| ----------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------- | ------------------- |\n| [clearRegisterOnInit?](./docs/api/fastify-metrics.imetricspluginoptions.clearregisteroninit.md)       | boolean                                                                                                             | `false`             |\n| [defaultMetrics?](./docs/api/fastify-metrics.imetricspluginoptions.defaultmetrics.md)                 | [IDefaultMetricsConfig](./docs/api/fastify-metrics.idefaultmetricsconfig.md)                                        | `{ enabled: true }` |\n| [endpoint?](./docs/api/fastify-metrics.imetricspluginoptions.endpoint.md)                             | string \\| null \\| [`Fastify.RouteOptions`](https://www.fastify.io/docs/api/latest/Reference/Routes/#routes-options) | `'/metrics'`        |\n| [name?](./docs/api/fastify-metrics.imetricspluginoptions.name.md)                                     | string                                                                                                              | `'metrics'`         |\n| [promClient?](./docs/api/fastify-metrics.imetricspluginoptions.promclient.md)                         | `prom-client` instance \\| null                                                                                      | `null`              |\n| [routeMetrics?](./docs/api/fastify-metrics.imetricspluginoptions.routemetrics.md)                     | [IRouteMetricsConfig](./docs/api/fastify-metrics.iroutemetricsconfig.md)                                            | `{ enabled: true }` |\n\n#### Route metrics\n\n| Property                                                                                           | Type                                                                                          | Default Value                           |\n| -------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------- | --------------------------------------- |\n| [enabled?](./docs/api/fastify-metrics.iroutemetricsconfig.enabled.md)                              | boolean \\| { histogram: boolean, summary: boolean }                                           | `true`                                  |\n| [groupStatusCodes?](./docs/api/fastify-metrics.iroutemetricsconfig.groupstatuscodes.md)            | boolean                                                                                       | `false`                                 |\n| [invalidRouteGroup?](./docs/api/fastify-metrics.iroutemetricsconfig.invalidroutegroup.md)          | string                                                                                        | `'__unknown__'`                         |\n| [methodBlacklist?](./docs/api/fastify-metrics.iroutemetricsconfig.methodblacklist.md)              | readonly string\\[\\]                                                                           | `['HEAD','OPTIONS','TRACE','CONNECT']`  |\n| [customLabels?](./docs/api/fastify-metrics.iroutemetricsconfig.customlabels.md)                    | Record\u0026lt;string, string \\| ((request: FastifyRequest, reply: FastifyReply) =\u0026gt; string)\u0026gt; | `undefined`                             |\n| [overrides?](./docs/api/fastify-metrics.iroutemetricsconfig.overrides.md)                          | [IRouteMetricsOverrides](./docs/api/fastify-metrics.iroutemetricsoverrides.md)                |                                         |\n| [registeredRoutesOnly?](./docs/api/fastify-metrics.iroutemetricsconfig.registeredroutesonly.md)    | boolean                                                                                       | `true`                                  |\n| [routeBlacklist?](./docs/api/fastify-metrics.iroutemetricsconfig.routeblacklist.md)                | readonly (string \\| RegExp)\\[\\]                                                               | `undefined`                             |\n\n##### Route metrics enabled\n\nThe `enabled` configuration option can be either a boolean which enables/disables generation of both histograms and summaries, or it can be set to an object that allows you to pick individually whether you want histograms or summaries to be generated, for example:\n\n```\n{\n  ...\n  routeMetrics: {\n    enabled: {\n      histogram: true,\n      summary: false\n    }\n  }\n}\n```\n\nwould result in the library only generating histograms.\n\n##### Route metrics overrides\n\nYou may override default metrics settings. You may provide overrides for two metrics tracking http request durations: `histogram` and `summary`.\n\n```js\nconst fastify = require('fastify');\nconst app = fastify();\nconst metricsPlugin = require('fastify-metrics');\n\nawait app.register(metricsPlugin, {\n  endpoint: '/metrics',\n  routeMetrics: {\n    overrides: {\n      histogram: {\n        name: 'my_custom_http_request_duration_seconds',\n        buckets: [0.1, 0.5, 1, 3, 5],\n      },\n      summary: {\n        help: 'custom request duration in seconds summary help',\n        labelNames: ['status_code', 'method', 'route'],\n        percentiles: [0.5, 0.75, 0.9, 0.95, 0.99],\n      },\n    },\n  },\n});\n```\n\n###### Labels\n\n| Property                                                                              | Type                                   | Default value   |\n| ------------------------------------------------------------------------------------- | -------------------------------------- | --------------- |\n| [getRouteLabel?](./docs/api/fastify-metrics.iroutelabelsoverrides.getroutelabel.md)  | (request: FastifyRequest) =\u0026gt; string | `undefined`     |\n| [method?](./docs/api/fastify-metrics.iroutelabelsoverrides.method.md)                | string                                 | `'method'`      |\n| [route?](./docs/api/fastify-metrics.iroutelabelsoverrides.route.md)                  | string                                 | `'route'`       |\n| [status?](./docs/api/fastify-metrics.iroutelabelsoverrides.status.md)                | string                                 | `'status_code'` |\n\n###### Request durations summary\n\n| Property                                                                | Type       | Default value                           |\n| ----------------------------------------------------------------------- | ---------- | --------------------------------------- |\n| [name?](./docs/fastify-metrics.isummaryoverrides.name.md)               | string     | `'http_request_summary_seconds'`        |\n| [help?](./docs/fastify-metrics.isummaryoverrides.help.md)               | string     | `'request duration in seconds summary'` |\n| [percentiles?](./docs/fastify-metrics.isummaryoverrides.percentiles.md) | number\\[\\] | `[0.5, 0.9, 0.95, 0.99]`                |\n\n###### Request durations histogram\n\n| Property                                                              | Type       | Default value                     |\n| --------------------------------------------------------------------- | ---------- | --------------------------------- |\n| [name?](./docs/api/fastify-metrics.ihistogramoverrides.name.md)       | string     | `'http_request_duration_seconds'` |\n| [help?](./docs/api/fastify-metrics.ihistogramoverrides.help.md)       | string     | `'request duration in seconds'`   |\n| [buckets?](./docs/api/fastify-metrics.ihistogramoverrides.buckets.md) | number\\[\\] | `[0.05, 0.1, 0.5, 1, 3, 5, 10]`   |\n\n\u003csub\u003e[Back to top](#toc)\u003c/sub\u003e\n\n### HTTP routes metrics in Prometheus\n\nThe following table shows what metrics will be available in Prometheus (subject to the `enabled` configuration option). Note suffixes like `_bucket`, `_sum`, `_count` are added automatically.\n\n| metric                                 | labels                           | description                   |\n| -------------------------------------- | -------------------------------- | ----------------------------- |\n| `http_request_duration_seconds_count`  | `method`, `route`, `status_code` | Requests total count          |\n| `http_request_duration_seconds_bucket` | `method`, `route`, `status_code` | Requests durations by bucket  |\n| `http_request_summary_seconds`         | `method`, `route`, `status_code` | Requests duration percentiles |\n| `http_request_summary_seconds_count`   | `method`, `route`, `status_code` | Requests total count          |\n\n\u003csub\u003e[Back to top](#toc)\u003c/sub\u003e\n\n## API Docs\n\nSee [docs](docs/api/index.md).\n\n\u003csub\u003e[Back to top](#toc)\u003c/sub\u003e\n\n## Changelog\n\nSee [changelog](docs/CHANGELOG.md).\n\n\u003csub\u003e[Back to top](#toc)\u003c/sub\u003e\n\n## See also\n\n- [fastify-prom-client](https://github.com/ExcitableAardvark/fastify-prom-client) - just simple client that adds aggregated http requests metric.\n\n\u003csub\u003e[Back to top](#toc)\u003c/sub\u003e\n\n## Support Ukraine\n\nIf you find this project useful, please consider [supporting Ukraine's defense](./SPONSOR.md).\n\n## License\n\nLicensed under [MIT](./LICENSE).\n\n\u003csub\u003e[Back to top](#toc)\u003c/sub\u003e\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskellla%2Ffastify-metrics","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fskellla%2Ffastify-metrics","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskellla%2Ffastify-metrics/lists"}