Import the package and add it to the Express application.
...
const promMid = require('express-prometheus-middleware');
...
app.use(promMid({
metricsPath: '/metrics',
collectDefaultMetrics: true,
requestDurationBuckets: [0.1, 0.5, 1, 3, 5, 10],
/**
* Uncomenting the `authenticate` callback will make the `metricsPath` route
* require authentication. This authentication callback can make a simple
* basic auth test, or even query a remote server to validate access.
* To access /metrics you could do:
* curl -X GET user:password@localhost:9091/metrics
*/
// authenticate: req => req.headers.authorization === 'Basic dXNlcjpwYXNzd29yZA==',
/**
* Uncommenting the `extraMasks` config will use the list of regexes to
* reformat URL path names and replace the values found with a placeholder value
*/
// extraMasks: [/..:..:..:..:..:../],
/**
* The prefix option will cause all metrics to have the given prefix.
* E.g.: `app_prefix_http_requests_total`
*/
// prefix: 'app_prefix_',
/**
* Can add custom labels with customLabels and transformLabels options
*/
// customLabels: ['contentType'],
// transformLabels(labels, req) {
// // eslint-disable-next-line no-param-reassign
// labels.contentType = req.headers['content-type'];
// },
}));
If the request URLs include IDs, for example, GET /order/1234567, capture the URLs as /order/{id} to avoid metric explosion.
In the following example, the regexp [0-9]+, which captures numbers 1234567, can be specified in the extraMasks list of regular expressions.