Installations
npm install fastify-metrics
Score
55.6
Supply Chain
97.8
Quality
80.4
Maintenance
100
Vulnerability
99.3
License
Developer
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
Yes
Node Version
22.9.0
NPM Version
10.8.3
Statistics
101 Stars
296 Commits
33 Forks
4 Watching
1 Branches
23 Contributors
Updated on 17 Nov 2024
Bundle Size
72.39 kB
Minified
19.65 kB
Minified + Gzipped
Languages
TypeScript (87.96%)
JavaScript (12.04%)
Total Downloads
Cumulative downloads
Total Downloads
10,200,704
Last day
4.8%
14,885
Compared to previous day
Last week
13.2%
84,755
Compared to previous week
Last month
-1.6%
349,282
Compared to previous month
Last year
285%
6,982,367
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
2
Peer Dependencies
1
Dev Dependencies
41
fastify-metrics
Prometheus metrics exporter for Fastify.
This plugin uses prom-client under the hood.
This plugin also adds two http metrics for your routes:
- Requests duration histogram
- Requests duration summary
ToC
- fastify-metrics
Fastify support
- v3.x.x - supports
fastify-1.x
- v4.x.x - supports
fastify-2.x
prom-client-11.x
- v5.x.x - supports
fastify-2.x
prom-client-12.x
- v6.x.x - supports
fastify-3.x
- v9.x.x - supports
fastify-4.x
prom-client-14.x
Notable changes
v10.x.x
- Replace route
context.config
withrouteConfig
due to deprecation in fastify v4 and removal in fastify v5. If you haddisableMetrics
option in you routeconfig
, update fastify to latest version. - Prefer
request.routeOptions.method
over deprecatedrequest.routerMethod
.
v9.x.x
- Fastify v4 support.
- Complete config rewrite, default behaviour changed.
- Support disabling metrics in route config.
- Now collects metrics only for registered routes by default.
- Unknown routes metrics collection disabled by default.
- Removed
metrics
fromrequest
. Now it usesWeakMap
and not exposed. - Add balcklisting possibility for request methods.
- Registry overrides moved to metric configuration.
- Support overriding all Summary and Histogram options for default route metrics.
v6.x.x
- Fastify v3 support.
- Drop node.js 8 support.
enableDefaultMetrics
- now enables only defaultprom-client
metrics. Set totrue
by default.enableRouteMetrics
- additional flag that enables route metrics. Set totrue
by default.
Installation
1npm i fastify-metrics --save 2pnpm i fastify-metrics --save
Features and requirements
- Collects default server metrics (see prom-client);
- Collects route response timings
- Adds
metrics
to fastify instance for your custom metrics.
- Requires fastify
>=4.0.0
. - Node.js
>=18.0.0
.
Usage
Add it to your project like regular fastify plugin. Use register
method and pass options to it.
1const fastify = require('fastify'); 2const app = fastify(); 3 4const metricsPlugin = require('fastify-metrics'); 5await app.register(metricsPlugin, { endpoint: '/metrics' });
It also exports client to fastify instance fastify.metrics.client
which you may use it in your routes.
You may create your metrics when app starts and store it in fastify.metrics
object and reuse them in multiple routes.
Registry clear
After calling registry.clear()
all metrics are removed from registry. In order to add them again to the registry, call fastify.mterics.initMetricsInRegistry
.
Plugin options
See for details docs
Property | Type | Default Value |
---|---|---|
defaultMetrics? | IDefaultMetricsConfig | { enabled: true } |
endpoint? | string | null | Fastify.RouteOptions | '/metrics' |
name? | string | 'metrics' |
routeMetrics? | IRouteMetricsConfig | { enabled: true } |
promClient? | prom-client instance | null | null |
Route metrics
Property | Type | Default Value |
---|---|---|
enabled? | boolean | { histogram: boolean, summary: boolean } | true |
enableSummaries? | boolean | true |
groupStatusCodes? | boolean | false |
invalidRouteGroup? | string | '__unknown__' |
methodBlacklist? | readonly string[] | ['HEAD','OPTIONS','TRACE','CONNECT',] |
overrides? | IRouteMetricsOverrides | |
registeredRoutesOnly? | boolean | true |
customLabels? | Record<string, string | ((request: FastifyRequest, reply: FastifyReply) => string)> | undefined |
routeBlacklist? | readonly (string | RegExp)[] | [] |
Route metrics enabled
The 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:
{
...
routeMetrics: {
enabled: {
histogram: true,
summary: false
}
}
}
would result in the library only generating histograms.
Route metrics overrides
You may override default metrics settings. You may provide overrides for two metrics tracking http request durations: histogram
and summary
.
1const fastify = require('fastify'); 2const app = fastify(); 3const metricsPlugin = require('fastify-metrics'); 4 5await app.register(metricsPlugin, { 6 endpoint: '/metrics', 7 routeMetrics: { 8 overrides: { 9 histogram: { 10 name: 'my_custom_http_request_duration_seconds', 11 buckets: [0.1, 0.5, 1, 3, 5], 12 }, 13 summary: { 14 help: 'custom request duration in seconds summary help', 15 labelNames: ['status_code', 'method', 'route'], 16 percentiles: [0.5, 0.75, 0.9, 0.95, 0.99], 17 }, 18 }, 19 }, 20});
Labels
Property | Type | Default value |
---|---|---|
getRouteLabel? | (request: FastifyRequest) => string | undefined |
method? | string | 'method' |
route? | string | 'route' |
status? | string | 'status_code' |
Request durations summary
Property | Type | Default value |
---|---|---|
name? | string | 'http_request_summary_seconds' |
help? | string | 'request duration in seconds summary' |
percentiles? | number[] | [0.5, 0.9, 0.95, 0.99] |
Request durations histogram
Property | Type | Default value |
---|---|---|
name? | string | 'http_request_duration_seconds' |
help? | string | 'request duration in seconds' |
buckets? | number[] | [0.05, 0.1, 0.5, 1, 3, 5, 10] |
HTTP routes metrics in Prometheus
The 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.
metric | labels | description |
---|---|---|
http_request_duration_seconds_count | method , route , status_code | Requests total count |
http_request_duration_seconds_bucket | method , route , status_code | Requests durations by bucket |
http_request_summary_seconds | method , route , status_code | Requests duration percentiles |
http_request_summary_seconds_count | method , route , status_code | Requests total count |
API Docs
See docs.
Changelog
See changelog.
See also
- fastify-prom-client - just simple client that adds aggregated http requests metric.
License
Licensed under MIT.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
11 commit(s) and 2 issue activity found in the last 90 days -- score normalized to 10
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
SAST tool is run on all commits
Details
- Info: SAST configuration detected: CodeQL
- Info: all commits (15) are checked with a SAST tool
Reason
3 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-7q7g-4xm8-89cq
- Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
Reason
Found 7/24 approved changesets -- score normalized to 2
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Info: jobLevel 'actions' permission set to 'read': .github/workflows/codeql.yml:16
- Info: jobLevel 'contents' permission set to 'read': .github/workflows/codeql.yml:17
- Warn: jobLevel 'packages' permission set to 'write': .github/workflows/release.yml:67
- Warn: jobLevel 'checks' permission set to 'write': .github/workflows/release.yml:68
- Warn: jobLevel 'contents' permission set to 'write': .github/workflows/release.yml:63
- Warn: no topLevel permission defined: .github/workflows/codeql.yml:1
- Warn: no topLevel permission defined: .github/workflows/release.yml:1
- Warn: no topLevel permission defined: .github/workflows/verify.yml:1
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql.yml:27: update your workflow using https://app.stepsecurity.io/secureworkflow/SkeLLLa/fastify-metrics/codeql.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql.yml:30: update your workflow using https://app.stepsecurity.io/secureworkflow/SkeLLLa/fastify-metrics/codeql.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql.yml:36: update your workflow using https://app.stepsecurity.io/secureworkflow/SkeLLLa/fastify-metrics/codeql.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql.yml:39: update your workflow using https://app.stepsecurity.io/secureworkflow/SkeLLLa/fastify-metrics/codeql.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:17: update your workflow using https://app.stepsecurity.io/secureworkflow/SkeLLLa/fastify-metrics/release.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:19: update your workflow using https://app.stepsecurity.io/secureworkflow/SkeLLLa/fastify-metrics/release.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/release.yml:22: update your workflow using https://app.stepsecurity.io/secureworkflow/SkeLLLa/fastify-metrics/release.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:32: update your workflow using https://app.stepsecurity.io/secureworkflow/SkeLLLa/fastify-metrics/release.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/release.yml:44: update your workflow using https://app.stepsecurity.io/secureworkflow/SkeLLLa/fastify-metrics/release.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/release.yml:52: update your workflow using https://app.stepsecurity.io/secureworkflow/SkeLLLa/fastify-metrics/release.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:71: update your workflow using https://app.stepsecurity.io/secureworkflow/SkeLLLa/fastify-metrics/release.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:73: update your workflow using https://app.stepsecurity.io/secureworkflow/SkeLLLa/fastify-metrics/release.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/release.yml:76: update your workflow using https://app.stepsecurity.io/secureworkflow/SkeLLLa/fastify-metrics/release.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:86: update your workflow using https://app.stepsecurity.io/secureworkflow/SkeLLLa/fastify-metrics/release.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/verify.yml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/SkeLLLa/fastify-metrics/verify.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/verify.yml:18: update your workflow using https://app.stepsecurity.io/secureworkflow/SkeLLLa/fastify-metrics/verify.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/verify.yml:21: update your workflow using https://app.stepsecurity.io/secureworkflow/SkeLLLa/fastify-metrics/verify.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/verify.yml:31: update your workflow using https://app.stepsecurity.io/secureworkflow/SkeLLLa/fastify-metrics/verify.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/verify.yml:43: update your workflow using https://app.stepsecurity.io/secureworkflow/SkeLLLa/fastify-metrics/verify.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/verify.yml:51: update your workflow using https://app.stepsecurity.io/secureworkflow/SkeLLLa/fastify-metrics/verify.yml/master?enable=pin
- Info: 0 out of 13 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 7 third-party GitHubAction dependencies pinned
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Score
5.4
/10
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to fastify-metrics
@immobiliarelabs/fastify-metrics
A minimalistic and opinionated Fastify plugin that collects metrics and dispatches them to statsd
@fastify/busboy
A streaming parser for HTML form data for node.js
fastify
Fast and low overhead web framework, for Node.js
@fastify/aws-lambda
Inspired by aws-serverless-express to work with Fastify with inject functionality.