forked from open-telemetry/opentelemetry-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitoring.js
38 lines (31 loc) · 880 Bytes
/
monitoring.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
"use strict";
const { Meter } = require("@opentelemetry/metrics");
const { PrometheusExporter } = require("@opentelemetry/exporter-prometheus");
const meter = new Meter();
meter.addExporter(
new PrometheusExporter(
{
startServer: true
},
() => {
console.log("prometheus scrape endpoint: http://localhost:9464/metrics");
}
)
);
const requestCount = meter.createCounter("requests", {
monotonic: true,
labelKeys: ["route"],
description: "Count all incoming requests"
});
const instruments = new Map();
module.exports.countAllRequests = () => {
return (req, res, next) => {
if (!instruments.has(req.path)) {
const labelSet = meter.labels({ route: req.path });
const boundCounter = requestCount.bind(labelSet);
instruments.set(req.path, boundCounter);
}
instruments.get(req.path).add(1);
next();
};
};