Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
fastify-metrics
Advanced tools
Prometheus metrics exporter for Fastify.
This plugin uses prom-client under the hood.
This plugin also adds two http metrics for your routes:
fastify-1.x
fastify-2.x
prom-client-11.x
fastify-2.x
prom-client-12.x
fastify-3.x
fastify-4.x
prom-client-14.x
metrics
from request
. Now it uses WeakMap
and not exposed.enableDefaultMetrics
- now enables only default prom-client
metrics. Set to true
by default.enableRouteMetrics
- additional flag that enables route metrics. Set to true
by default.npm i fastify-metrics --save
pnpm i fastify-metrics --save
metrics
to fastify instance for your custom metrics.>=4.0.0
.>=16.0.0
.Add it to your project like regular fastify plugin. Use register
method and pass options to it.
const fastify = require('fastify');
const app = fastify();
const metricsPlugin = require('fastify-metrics');
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.
After calling registry.clear()
all metrics are removed from registry. In order to add them again to the registry, call fastify.mterics.initMetricsInRegistry
.
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 } |
Property | Type | Default Value |
---|---|---|
enabled? | boolean | true |
groupStatusCodes? | boolean | false |
invalidRouteGroup? | string | '__unknown__' |
methodBlacklist? | readonly string[] | ['HEAD','OPTIONS','TRACE','CONNECT',] |
overrides? | IRouteMetricsOverrides | |
registeredRoutesOnly? | boolean | true |
routeBlacklist? | readonly string[] | [] |
You may override default metrics settings. You may provide overrides for two metrics tracking http request durations: histogram
and summary
.
const fastify = require('fastify');
const app = fastify();
const metricsPlugin = require('fastify-metrics');
app.register(metricsPlugin, {endpoint: '/metrics', {
routeMetrics: {
overrides: {
histogram: {
name: 'my_custom_http_request_duration_seconds',
buckets: [0.1, 0.5, 1, 3, 5],
},
summary: {
help: 'custom request duration in seconds summary help',
labelNames: ['status_code', 'method', 'route'],
percentiles: [0.5, 0.75, 0.9, 0.95, 0.99],
},
}
}
}});
Property | Type | Default value |
---|---|---|
method? | string | 'method' |
route? | string | 'route' |
status? | string | 'status_code' |
Property | Type | Default value |
---|---|---|
name? | string | 'http_request_summary_seconds' |
help? | string | 'rrequest duration in seconds summary' |
percentiles? | number[] | [0.5, 0.9, 0.95, 0.99] |
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] |
The following table shows what metrics will be available in Prometheus. 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 |
See docs.
See changelog.
Licensed under MIT.
FAQs
Prometheus metrics exporter for Fastify
The npm package fastify-metrics receives a total of 89,629 weekly downloads. As such, fastify-metrics popularity was classified as popular.
We found that fastify-metrics demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.