forked from rudderlabs/rudder-transformer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
middleware.js
41 lines (33 loc) · 1.11 KB
/
middleware.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const prometheusClient = require("prom-client");
// const gcStats = require("prometheus-gc-stats");
const prometheusRegistry = new prometheusClient.Registry();
prometheusClient.collectDefaultMetrics({ register: prometheusRegistry });
// const startGcStats = gcStats(prometheusRegistry); // gcStats() would have the same effect in this case
// startGcStats();
function durationMiddleware() {
const httpRequestDurationSummary = new prometheusClient.Summary({
name: "http_request_duration_summary_seconds",
help: "Summary of HTTP requests duration in seconds",
labelNames: ["method", "route", "code"],
percentiles: [0.01, 0.1, 0.9, 0.99]
});
prometheusRegistry.registerMetric(httpRequestDurationSummary);
return async (ctx, next) => {
const end = httpRequestDurationSummary.startTimer();
await next();
const labels = {
method: ctx.method,
code: ctx.status,
route: ctx._matchedRoute
};
end(labels);
};
}
function addPrometheusMiddleware(app) {
app.use(durationMiddleware());
}
module.exports = {
addPrometheusMiddleware,
durationMiddleware,
prometheusRegistry
};