Prometheus Express Middleware
Improving API visibility via a simple plugin and forget interface.
- request count as
requests_total
Prom Counter - request timing as
request_timing_seconds
Prom Histogram
Currently used default labels are:
path
being calledmethod
being usedstatusCode
being replied
statusCode
s are normalized by prefix, i.e. 2xx, 4xx, 5xx etcpath
s are normalized viaeroute-idrr
(https://github.com/razmat145/eroute-idrr) by reading express app routes
npm install --save prom-emw
Given an example express app
public async initialise(port: number) {
const app = express();
await PromEMW.install(app); // install the MW
app.get('/myapi/test', (req, res) => {
const randSample = _.sample([1, 3, 15, 20]);
switch (true) { // change status randomly
case randSample % 5 === 0 && randSample % 3 === 0:
res.status(400);
break;
case randSample % 5 === 0:
res.status(500);
break;
default:
res.status(200);
break;
}
const randomResponseTime = _.sample([0.5, 1, 1.5, 2]);
setTimeout(() => { // reply in random times
return res.json('Hello');
}, randomResponseTime * 1000);
});
app.listen(port);
logger.info(`Listening on port: ${port}`);
}
For more queries examples, look into PromQL https://prometheus.io/docs/prometheus/latest/querying/basics/
rate(requests_total[5m])
histogram_quantile(0.9, rate(request_timing_seconds_bucket[5m]))
sum(rate(request_timing_seconds_sum[5m])) / sum(rate(request_timing_seconds_count[5m]))
rate(requests_total{statusCode='401'}[10s])
rate(process_cpu_system_seconds_total[5m])
nodejs_heap_size_used_bytes
Given a path of '/myapi/test/:myId'
, despite of being called with myId
of [101, 102, ... 9999]
it will get normalized to it's base
interface IMWOpts {
// Prom collection endpoint path
collectionPath?: string; // defaults to /metrics
// App name to be appended to Prom metric type names
appName?: string; // defaults to ''
// Http status codes to be escaped from normalization
escapeStatusCodes?: Array<number>;
// If to collect default NodeJS metrics see https://github.com/siimon/prom-client#default-metrics
collectDefaultMetrics?: boolean;
// If to enable paremeter path normalization - recommended in order to reduce `path` label dimensions
enableParameterNormalization?: boolean; // defaults to true
}
- more config flexibility and options
- pm2 cluster support
- custom metrics
- nodejs cluster support if desired
This library is licensed under the Apache 2.0 License