# fastify-metrics-js-response-timing

> A Fastify plugin using @metrics/client (https://www.npmjs.com/package/@metrics/client) to produce metric streams reporting metrics about route response times

Latest version **3.0.0** (published 2023-02-14) · ISC license · 0 weekly downloads

## Install

```sh
npm install fastify-metrics-js-response-timing
pnpm add fastify-metrics-js-response-timing
yarn add fastify-metrics-js-response-timing
bun add fastify-metrics-js-response-timing
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2023-02-14 |
| First published | 2020-02-04 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 11 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | digitalsadhu, trygve |

## Links

- npm: https://www.npmjs.com/package/fastify-metrics-js-response-timing
- Repository: https://github.com/finn-no/fastify-metrics-js-response-timing
- Homepage: https://github.com/finn-no/fastify-metrics-js-response-timing#readme
- Issues: https://github.com/finn-no/fastify-metrics-js-response-timing/issues
- npm.io page: https://npm.io/package/fastify-metrics-js-response-timing

## Dependencies (2)

- [fastify-plugin](https://npm.io/package/fastify-plugin.md) ^4.5.0
- [@metrics/client](https://npm.io/package/@metrics/client.md) ^2.5.0

## Recent versions

- 3.0.0 (latest) — 2023-02-14
- 2.0.0 — 2020-08-19
- 1.0.0 — 2020-02-04

## README

# 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

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

Step 2. Pipe metrics into a consumer. See [Metrics JS](https://www.npmjs.com/package/@metrics/client) for more on this.

```js
responseTiming.metrics.pipe(consumer);
```

Step 3. Create a fastify app and include the plugin

```js
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

```js
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

```js
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

```js
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

```js
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

```js
app.register(pluginMetrics, { timeAllRoutes, groupStatusCodes });
```

| name             | description                                            | type      | default | required |
| ---------------- | ------------------------------------------------------ | --------- | ------- | -------- |
| groupStatusCodes | Whether to group status codes eg. 400, 401, 403 => 4xx | `boolean` | `false` | `no`     |
| timeAllRoutes    | Whether to time all routes or just those configured    | `boolean` | `true`  | `no`     |

---
_Source: https://npm.io/package/fastify-metrics-js-response-timing · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
