-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetrics.js
61 lines (49 loc) · 1.94 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const express = require("express");
const client = require("prom-client");
const app = express();
const requestDurationHistogram = new client.Histogram({
name: "score_rank_api_request_duration_histogram",
help: "Histogram of HTTP request durations in seconds",
labelNames: ["method", "route", "status_code", "origin", "mode"],
});
const dbQueryDurationHistogram = new client.Histogram({
name: "score_rank_api_db_query_duration_histogram",
help: "Histogram of database query durations in seconds",
labelNames: ["query"],
});
const osuApiRequestDurationHistogram = new client.Histogram({
name: "score_rankings_fetcher_osu_api_request_duration_histogram",
help: "Histogram of osu api request durations in seconds",
labelNames: ["mode", "status_code"],
});
function observeRequestDuration(duration, method, route, statusCode, origin, mode) {
requestDurationHistogram.labels(method, route, statusCode, origin, mode).observe(duration);
}
function observeDbQueryDuration(duration, query) {
dbQueryDurationHistogram.labels(query).observe(duration);
}
function observeOsuApiRequestDuration(duration, mode, statusCode) {
osuApiRequestDurationHistogram.labels(mode, statusCode).observe(duration);
}
const collectDefaultMetrics = client.collectDefaultMetrics;
const Registry = client.Registry;
const register = new Registry();
collectDefaultMetrics({ register });
register.registerMetric(requestDurationHistogram);
register.registerMetric(dbQueryDurationHistogram);
register.registerMetric(osuApiRequestDurationHistogram);
function metricsServer(port) {
app.get("/metrics", async (req, res) => {
res.set("Content-Type", register.contentType);
res.send(await register.metrics());
});
app.listen(port, () => {
console.log(`metrics server started on port ${port}`);
});
}
module.exports = {
metricsServer,
observeDbQueryDuration,
observeRequestDuration,
observeOsuApiRequestDuration,
};