This module allows application to expose Prometheus compatible metrics. The metrics include request duration and statuses and optionally those that are
exported by default in prom-client
. Example output showing all metrics.
By default, the /metrics
and /healthz
endpoints are ignored.
npm install @tailorbrands/node-exporter-prometheus
Since the duration of requests is being measured, it is important that the exporter be activated before the first application route or middleware.
app = express();
// Node prometheus exporter setup
const options = {appName}; // `appName` is the name of your service/application
const prometheusExporter = require('@tailorbrands/node-exporter-prometheus')
const promExporter = PromExporter(options);
app.use(promExporter.middleware);
app.get('/metrics', promExporter.metrics);
// Application routes and middleware starts here
app.use(...)
app.get(...)
app.post(...)
appName
- Name that will be used in the label for every metricignoredRoutes
(optional) - An array of routes to be exuded when calculating metrics. Default value:['/metrics', '/healthz']
collectDefaultMetrics
(optional) - A boolean indicating whether or not to collect default nodejs metrics (default: false)
Once you have the default metrics accessible, you might want to add some of your own custom metrics. The module exposes the underlying prom-client
as a property called client
. This allows you to add any other prom-client
metrics.
Here is an example using a Gauge metric. It will add something similar to following in the output of the metrics endpoint:
# HELP simple_counter One route increases another one decreases # TYPE simple_counter gauge simple_counter{app_name="test-app"} 2
const {promMiddleware, promMetrics, promClient} = require('node-prometheus-exporter')({ appName });
const simpleCounter = new promClient.Gauge({
name: 'simple_counter',
help: 'One route increases another one decreases'
});
app.get('/increase', (req, res) => {
simpleCounter.inc()
res.send(200)
})
app.get('/decrease', (req, res) => {
simpleCounter.dec()
res.send(200)
})
npm test