https://github.com/platformatic/fastify-http-metrics
https://github.com/platformatic/fastify-http-metrics
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/platformatic/fastify-http-metrics
- Owner: platformatic
- License: apache-2.0
- Created: 2024-08-26T17:22:44.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-04-12T20:50:05.000Z (3 months ago)
- Last Synced: 2025-04-13T11:12:28.049Z (3 months ago)
- Language: JavaScript
- Size: 395 KB
- Stars: 2
- Watchers: 4
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @platformatic/fastify-http-metrics
The `fastify-http-metrics` package provides a simple way to collect prometheus metrics for your Fastify application.
## Installation
```bash
npm install @platformatic/fastify-http-metrics
```## Usage
```javascript
const { Registry } = require('prom-client')
const fastify = require('fastify')
const httpMetrics = require('@platformatic/fastify-http-metrics')const app = fastify()
const registry = new Registry()
app.register(httpMetrics, { registry })app.get('/metrics', async () => {
const metrics = await registry.metrics()
return metrics
})app.get('/', async () => {
return 'Hello World'
})app.listen({ port: 0 }, (err, address) => {
if (err) {
console.error(err)
process.exit(1)
}
console.log(`Server listening on ${address}`)
})
```## API
#### httpMetrics plugin options
- __`options`__ `` Options for configuring the metrics collection.
- __`registry`__ `` The prom-client registry to use for collecting metrics.
- __`customLabels`__ `` A list of custom labels names to add to the metrics.
- __`getCustomLabels(req, res, server)`__ `` A function that returns an object of custom labels to add to the metrics. The function receives the request object as a first argument and a response object as a second argument.
- __`ignoreMethods`__ `` A list of HTTP methods to ignore when collecting metrics. Default: `['OPTIONS', 'HEAD', 'CONNECT', 'TRACE']`.
- __`ignoreRoutes`__ `` A list of fastify routes to ignore when collecting metrics. Default: `[]`.
- __`ignore(req, res, server)`__ `` A function that returns a boolean indicating whether to ignore the request when collecting metrics. The function receives the request object as a first argument and a response object as a second argument.
- __`histogram`__ `` prom-client [histogram options](https://github.com/siimon/prom-client?tab=readme-ov-file#histogram). Use it if you want to customize the histogram.
- __`summary`__ `` prom-client [summary options](https://github.com/siimon/prom-client?tab=readme-ov-file#summary). Use it if you want to customize the summary.
- __`zeroFill`__ `` Whether to zero-fill the histogram and summary buckets. Default: `false`.## License
Apache-2.0