npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fastify-metrics

v12.1.0

Published

Prometheus metrics exporter for Fastify

Downloads

384,294

Readme

fastify-metrics

NPM Version Downloads Count Vunerabilities Count Build Status License Codecov

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 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 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.
  • Prefer request.routeOptions.method over deprecated request.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 from request. Now it uses WeakMap 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 default prom-client metrics. Set to true by default.
  • enableRouteMetrics - additional flag that enables route metrics. Set to true by default.

Installation

npm i fastify-metrics --save
pnpm 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 >=4.0.0.
  • Node.js >=18.0.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');
await 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.

Back to top

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.

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

await 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],
      },
    },
  },
});
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] |

Back to top

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 |

Back to top

API Docs

See docs.

Back to top

Changelog

See changelog.

Back to top

See also

Back to top

License

Licensed under MIT.

Back to top