https://github.com/rhaymo/fastify-opencensus
Fastify metrics and tracing using Opencensus
https://github.com/rhaymo/fastify-opencensus
fastify metrics opencensus
Last synced: 2 months ago
JSON representation
Fastify metrics and tracing using Opencensus
- Host: GitHub
- URL: https://github.com/rhaymo/fastify-opencensus
- Owner: rhaymo
- License: mit
- Created: 2019-09-18T14:40:50.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2024-09-06T07:19:55.000Z (almost 2 years ago)
- Last Synced: 2025-09-06T04:40:00.054Z (10 months ago)
- Topics: fastify, metrics, opencensus
- Language: TypeScript
- Size: 934 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 15
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# fastify-opencensus
[]()
[]()
[](https://travis-ci.org/rhaymo/fastify-opencensus)
[](https://snyk.io//test/github/rhaymo/fastify-opencensus?targetFile=package.json)
[](https://coveralls.io/github/rhaymo/fastify-opencensus?branch=master)
[](https://github.com/rhaymo/fastify-opencensus/blob/master/LICENSE)
Metrics collector for Fastify based on [Opencensus](https://opencensus.io/) .
This module is inspired and based on the [fastify-metrics](https://gitlab.com/m03geek/fastify-metrics) plugin.
This plugin adds 3 http metrics for your routes:
- Requests duration distribution
- Requests duration summary
- Requests count
## ToC
- [fastify-opencensus](#fastify-opencensus)
- [ToC](#ToC)
- [Fastify support](#Fastify-support)
- [Installation](#Installation)
- [Features and requirements](#Features-and-requirements)
- [Usage](#Usage)
- [Plugin options](#Plugin-options)
- [Metrics details](#Metrics-details)
- [HTTP routes metrics](#HTTP-routes-metrics)
- [Docs](#Docs)
- [Changelog](#Changelog)
- [See also](#See-also)
- [License](#License)
## Fastify support
- **v0.x.x** - will support `>= fastify-2.0.0`
## Installation
```sh
npm i fastify-opencensus --save
```
[Back to top](#toc)
## Features and requirements
- Collects default server metrics (see [opencensus-default-metrics](https://github.com/rhaymo/opencensus-node-default-metrics));
- Collects route response timings
- By default, metrics info are collected into the global instance of opencensus required by this package. If you want to use another opencensus version, you have to pass in this instance using the `stats` options field.
---
- Requires fastify `>=2.0.0`.
- Node.js `>=8.9.0`.
[Back to top](#toc)
## Usage
Add it to your project like regular fastify plugin. Use `register` method and pass options to it.
```js
const fastify = require('fastify');
const app = fastify();
const metricsPlugin = require('fastify-opencensus');
app.register(metricsPlugin, { interval: 3000 });
```
It also exports opencensus stats instance to fastify instance `fastify.opencensus.client` which you may use it in your routes.
See example folder for other examples.
[Back to top](#toc)
### Plugin options
| parameter | type | description | default |
| ---------------------- | ------------------------ | ------------------------------------------------------------------------------- | ------------ |
| `enableDefaultMetrics` | Boolean | Enables collection of node default metrics. | `true` |
| `enableStats` | Boolean | Enables collection of fastify metrics. | `true` |
| `stats` | Object | Custom opencensus metrics instance. | `undefined` |
| `metricsExporter` | Array | Array of Opencensus metrics exporter | `undefined` |
| `groupStatusCodes` | Boolean | Groups status codes (e.g. 2XX) if `true` | `false` |
| `pluginName` | String | Change name which you'll use to access opencensus registry instance in fastify. | `opencensus` |
| `interval` | Number | Default metrics collection interval in ms. | `5000` |
| `blacklist` | String, RegExp, String[] | Skip metrics collection for blacklisted routes | `undefined` |
| `prefix` | String | Custom default metrics prefix. | `""` |
| `metrics` | Object | Allows override default metrics config. See section below. | `{}` |
#### Metrics details
You may override default metrics settings. You may provide overrides for three metrics tracking http request durations, count and sum (labelNames cannot be overriden).
Default values:
```js
{
distribution: {
name: 'http_request_duration_seconds',
desc: 'request duration in seconds',
labelNames: ['status_code', 'method', 'route'],
buckets: [0.05, 0.1, 0.5, 1, 3, 5, 10],
},
sum: {
name: 'http_sum_request_duration_seconds',
desc: 'Sum of durations of http requests',
labelNames: ['status_code', 'method', 'route']
},
count: {
name: 'http_request_count',
desc: 'Counter of http requests',
labelNames: ['status_code', 'method', 'route']
},
}
```
Override should look like:
```js
const fastify = require('fastify');
const app = fastify();
const metricsPlugin = require('fastify-metrics');
app.register(metricsPlugin, {metrics: {
distribution: {
name: 'my_custom_http_request_duration_seconds',
buckets: [0.1, 0.5, 1, 3, 5],
},
sum: {
desc: 'custom request duration in seconds summary help'
},
});
```
[Back to top](#toc)
### HTTP routes metrics
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_count` | `method`, `route`, `status_code` | Requests total count |
| `http_request_duration_seconds` | `method`, `route`, `status_code` | Requests durations by bucket |
| `http_sum_request_duration_seconds` | `method`, `route`, `status_code` | Requests duration sum |
[Back to top](#toc)
## Docs
See [docs](docs/README.md).
[Back to top](#toc)
## Changelog
See [changelog](CHANGELOG.md).
[Back to top](#toc)
## See also
- [opencensus-default-metrics](https://github.com/rhaymo/opencensus-node-default-metrics) - collector of node default metrics using opencensus
[Back to top](#toc)
## License
Licensed under [MIT](./LICENSE).
[Back to top](#toc)