@tailorbrands/node-exporter-prometheus
v2.0.7
Published
Exposes prometheus metrics
Downloads
256
Readme
Prometheus Exporter for Express Applications
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.
Installing
npm install @tailorbrands/node-exporter-prometheus
Setup
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(...)
Options
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)
Additional Metrics
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:
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)
})
Testing
npm test