@springtree/springboard-metrics
v1.2.0
Published
Enable Prometheus metrics on a Node App
Downloads
14
Readme
springboard-metrics
Expose metrics in Prometheus format on a Node.JS API with custom Express middleware.
Features
- Integration of RED (Requests, Errors, Duration) metrics
- Standard Prometheus Metrics included (https://prometheus.io/docs/instrumenting/writing_clientlibs/#standard-and-runtime-collectors)
- Node.JS specific metrics included (https://github.com/siimon/prom-client/tree/master/lib/metrics)
- User configuration by specifying user options on initialization of the middleware
- Extensibility
Dependencies
- prom-client (https://github.com/siimon/prom-client, a Prometheus client)
- response-time (https://www.npmjs.com/package/response-time, used to measure request duration)
Installation
Install the package using NPM
npm i @springtree/springboard-metrics
Using the package
Import the package
import { prometheusMiddleware } from '@springtree/springboard-metrics';
Initialize Express API
this.app = express();
Make the Express API use the middleware
this.app.use(prometheusMiddleware('API_name', 'API_version_0.1', {defaultMetricsPath: '/metrics'}));
Expose the metrics
Import prom-client
const prometheus = require('prom-client')
Make an endpoint to expose the metrics
app.get('/metrics', (async (request, response) => { response.send(await prometheus.register.metrics()); }));
Navigate to '/metrics' to see the exposed metrics in Prometheus format.
Ignoring the /metrics path
Make sure the endpoint path and the 'defaultMetricsPath' in the options are the same.
If you don't do this, the Prometheus instance that scrapes '/metrics' will count as a request.
A Prometheus instance scrapes at a set interval, this means that the metrics will increase over time without user action.
Prometheus target
The '/metrics' endpoint is also the target for a Prometheus Instance to scrape.