forked from EDOHWARES/EventHorizon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexporter.js
More file actions
89 lines (77 loc) · 2.56 KB
/
exporter.js
File metadata and controls
89 lines (77 loc) · 2.56 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
const http = require('http');
const client = require('prom-client');
// Create a Registry to register the metrics
const register = new client.Registry();
// Add a default label to all metrics
register.setDefaultLabels({
app: 'eventhorizon-backend'
});
// Enable the collection of default metrics (CPU/Memory usage, etc.)
client.collectDefaultMetrics({ register });
// Track success/failure rates of actions
const actionStatusCounter = new client.Counter({
name: 'eventhorizon_action_status_total',
help: 'Total number of actions executed, labeled by status (success/failure) and action_type',
labelNames: ['status', 'action_type'],
});
register.registerMetric(actionStatusCounter);
// Monitor queue latency
const queueLatencyHistogram = new client.Histogram({
name: 'eventhorizon_queue_latency_seconds',
help: 'Latency of actions in the queue in seconds',
buckets: [0.1, 0.5, 1, 2, 5, 10] // Buckets for latency observation
});
register.registerMetric(queueLatencyHistogram);
/**
* Record a successful action
* @param {string} actionType - The type of action performed
*/
function recordActionSuccess(actionType) {
actionStatusCounter.labels('success', actionType).inc();
}
/**
* Record a failed action
* @param {string} actionType - The type of action performed
*/
function recordActionFailure(actionType) {
actionStatusCounter.labels('failure', actionType).inc();
}
/**
* Record the time an action spent in the queue
* @param {number} latencyInSeconds - The queue latency
*/
function recordQueueLatency(latencyInSeconds) {
queueLatencyHistogram.observe(latencyInSeconds);
}
/**
* Start the HTTP server to expose the /metrics endpoint
* @param {number} port - Port to listen on (default 9090)
*/
function startMetricsServer(port = 9090) {
const server = http.createServer(async (req, res) => {
if (req.url === '/metrics' && req.method === 'GET') {
res.setHeader('Content-Type', register.contentType);
try {
const metrics = await register.metrics();
res.end(metrics);
} catch (err) {
res.statusCode = 500;
res.end(err.message);
}
} else {
res.statusCode = 404;
res.end('Not Found');
}
});
server.listen(port, () => {
console.log(`Prometheus Exporter listening on http://0.0.0.0:${port}/metrics`);
});
return server;
}
module.exports = {
startMetricsServer,
recordActionSuccess,
recordActionFailure,
recordQueueLatency,
register // Exported for testing purposes
};