3.0.0 • Published 1 year ago

fastify-metrics-js-response-timing v3.0.0

Weekly downloads
1
License
ISC
Repository
github
Last release
1 year ago

Fastify Metrics-js Response Timing

A Fastify plugin for generating @metrics/client (https://www.npmjs.com/package/@metrics/client) route timing metrics.

Usage

Step 1. Include and setup dependency

import ResponseTiming from 'fastify-metrics-js-response-timing';
const reponseTiming = new ResponseTiming();

Step 2. Pipe metrics into a consumer. See Metrics JS for more on this.

responseTiming.metrics.pipe(consumer);

Step 3. Create a fastify app and include the plugin

import fastify from 'fastify';
const app = fastify();
app.register(responseTiming.plugin());

Step 4. Opt out of any routes as needed by passing config to the route

app.get('/', { config: { timing: false } }, (request, reply) => {
    reply.send('ok');
});

By default timing metrics will be gathered for all defined routes in the app unless you opt out. However, it is possible to set the plugin up to be opt in as shown below.

Timing only desired routes

const reponseTiming = new ResponseTiming({ timeAllRoutes: false });
app.register(responseTiming.plugin());

// opt in by setting timing config to true
app.get('/', { config: { timing: true } }, (request, reply) => {
    reply.send('ok');
});

Timing all routes

const reponseTiming = new ResponseTiming({ timeAllRoutes: true });
app.register(responseTiming.plugin());

// opt out by setting timing config to false
app.get('/', { config: { timing: false } }, (request, reply) => {
    reply.send('ok');
});

Grouping status codes

const reponseTiming = new ResponseTiming({ groupStatusCodes: true });
app.register(responseTiming.plugin());

All status codes in the range 200-299 will be captured as 2xx, 300-399 as 3xx, 400-499 as 4xx, 500-599 as 5xx.

Plugin options

app.register(pluginMetrics, { timeAllRoutes, groupStatusCodes });
namedescriptiontypedefaultrequired
groupStatusCodesWhether to group status codes eg. 400, 401, 403 => 4xxbooleanfalseno
timeAllRoutesWhether to time all routes or just those configuredbooleantrueno