forked from Deen-Bridge/dnb-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics.js
More file actions
97 lines (84 loc) · 2.63 KB
/
Copy pathmetrics.js
File metadata and controls
97 lines (84 loc) · 2.63 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import promClient from "prom-client";
import logger from "./logger.js";
const registry = new promClient.Registry();
promClient.collectDefaultMetrics({ register: registry });
const httpRequestDuration = new promClient.Histogram({
name: "http_request_duration_seconds",
help: "HTTP request duration in seconds",
labelNames: ["method", "route", "status_code"],
buckets: [0.01, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10],
registers: [registry],
});
const horizonRequestDuration = new promClient.Histogram({
name: "horizon_request_duration_seconds",
help: "Stellar Horizon API call duration in seconds",
labelNames: ["operation", "outcome"],
buckets: [0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10],
registers: [registry],
});
const paymentsInitialized = new promClient.Counter({
name: "payments_initialized_total",
help: "Total number of payments initialized",
labelNames: ["type"],
registers: [registry],
});
const paymentsSubmitted = new promClient.Counter({
name: "payments_submitted_total",
help: "Total number of payment transactions submitted",
labelNames: ["type"],
registers: [registry],
});
const paymentsConfirmed = new promClient.Counter({
name: "payments_confirmed_total",
help: "Total number of confirmed payments",
labelNames: ["type"],
registers: [registry],
});
const paymentsFailed = new promClient.Counter({
name: "payments_failed_total",
help: "Total number of failed payments",
labelNames: ["type", "reason"],
registers: [registry],
});
function observeHttpDuration(method, route, statusCode, durationMs) {
httpRequestDuration.observe(
{ method, route: route || "unknown", status_code: String(statusCode) },
durationMs / 1000
);
}
function observeHorizonDuration(operation, outcome, durationMs) {
horizonRequestDuration.observe(
{ operation, outcome },
durationMs / 1000
);
}
async function metricsMiddleware(req, res, next) {
const token = process.env.METRICS_TOKEN;
if (token) {
const auth = req.headers.authorization || "";
const expected = "Bearer " + token;
if (auth !== expected) {
return res.status(401).json({ success: false, message: "Unauthorized" });
}
}
try {
res.setHeader("Content-Type", registry.contentType);
const metrics = await registry.metrics();
res.end(metrics);
} catch (err) {
logger.error(err, "Failed to generate metrics");
res.status(500).json({ success: false, message: "Metrics error" });
}
}
export {
registry,
httpRequestDuration,
horizonRequestDuration,
paymentsInitialized,
paymentsSubmitted,
paymentsConfirmed,
paymentsFailed,
observeHttpDuration,
observeHorizonDuration,
metricsMiddleware,
};