-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmetrics.js
38 lines (33 loc) · 1.04 KB
/
metrics.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
const {Metrics, IntervalFlusher} = require("metrics-node");
const Sentry = require('@sentry/node');
let config = require("./config");
const metrics = new Metrics(config.metrics);
const flusher = new IntervalFlusher(metrics, 10000);
metrics.setFlusher(flusher);
const API_REQUESTS_METRIC = metrics.metric('spiget', 'api_requests');
const apiRequestsMiddleware = (req, res, next) => {
res.on("finish", () => {
try {
const route = req.route;
if (route) {
const path = route["path"];
if (path) {
API_REQUESTS_METRIC
.tag("server", config.server.name)
.tag("method", req.method)
.tag("path", path)
.tag("status", `${res.statusCode}`)
.inc();
}
}
} catch (e) {
Sentry.captureException(e);
}
})
next();
}
module.exports = {
metrics,
API_REQUESTS_METRIC,
apiRequestsMiddleware
}