The fastify-http-metrics
package provides a simple way to collect prometheus metrics for your Fastify application.
npm install @platformatic/fastify-http-metrics
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}`)
})
options
<object>
Options for configuring the metrics collection.registry
<Registry>
The prom-client registry to use for collecting metrics.customLabels
<array>
A list of custom labels names to add to the metrics.getCustomLabels(req, res, server)
<function>
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
<array>
A list of HTTP methods to ignore when collecting metrics. Default:['OPTIONS', 'HEAD', 'CONNECT', 'TRACE']
.ignoreRoutes
<array>
A list of fastify routes to ignore when collecting metrics. Default:[]
.ignore(req, res, server)
<function>
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
<object>
prom-client histogram options. Use it if you want to customize the histogram.summary
<object>
prom-client summary options. Use it if you want to customize the summary.
Apache-2.0