Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/platformatic/fastify-http-metrics


https://github.com/platformatic/fastify-http-metrics

Last synced: about 1 month ago
JSON representation

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.

## License

Apache-2.0