Socket
Socket
Sign inDemoInstall

fastify-metrics

Package Overview
Dependencies
Maintainers
1
Versions
68
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastify-metrics

Prometheus metrics exporter for Fastify


Version published
Maintainers
1
Created
Source

fastify-metrics

NPM Version Downloads Count Vunerabilities Count Build Status License

Prometheus metrics exporter for Fastify.

This plugin uses prom-client under the hood.

This plugin also adds 2 http metrics for your routes:

  • Requests duration histogram
  • Requests duration summary

ToC

Fastify support

  • v3.x.x - supports < fastify-2.0.0
  • v4.x.x - will support >= fastify-2.0.0

Installation

npm i fastify-metrics --save

Back to top

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 >=1.9.0.
  • Node.js >=8.9.0.

Back to top

Usage

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.

Back to top

Plugin options

parametertypedescriptiondefault
enableDefaultMetricsBooleanEnables collection of default metrics.true
pluginNameStringChange name which you'll use to access prometheus client instance in fastify.metrics
intervalNumberDefault metrics collection interval in ms.5000
registerObjectCustom prom-client metrics registry (see docs).undefined
prefixStringCustom default metrics prefix.""
endpointStringIf set, fastify route will be added to expose metrics. If not set you may manually add it afterwards.undefined
metricsObjectAllows override default metrics config. See section below.{}
blacklistString, RegExp, String[]Skip metrics collection for blacklisted routesundefined
groupStatusCodesBooleanGroups status codes (e.g. 2XX) if truefalse
Metrics details

You may override default metrics settings. You may provide overrides for two metrics tracking http request durations: histogram and summary. Default values:

{
  histogram: {
    name: 'http_request_duration_seconds',
    help: 'request duration in seconds',
    labelNames: ['status_code', 'method', 'route'],
    buckets: [0.05, 0.1, 0.5, 1, 3, 5, 10],
  },
  summary: {
    name: 'http_request_summary_seconds',
    help: 'request duration in seconds summary',
    labelNames: ['status_code', 'method', 'route'],
    percentiles: [0.5, 0.9, 0.95, 0.99],
  },
}

You may also provide registers there or use it instead of prefix. Override should look like:

const fastify = require('fastify');
const app = fastify();
const metricsPlugin = require('fastify-metrics');

app.register(metricsPlugin, {endpoint: '/metrics', {
  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],
  },
}});

Back to top

HTTP routes metrics

The following table shows what metrics will be available in Prometheus. Note suffixes like _bucket, _sum, _count are added automatically.

metriclabelsdescription
http_request_duration_seconds_countmethod, route, status_codeRequests total count
http_request_duration_seconds_bucketmethod, route, status_codeRequests durations by bucket
http_request_duration_seconds_summethod, route, status_codeRequests duration summaries by quantile

Back to top

Docs

See docs.

Back to top

Changelog

See changelog.

Back to top

See also

Back to top

License

Licensed under MIT.

Back to top

Keywords

FAQs

Package last updated on 08 Mar 2020

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc