-
Notifications
You must be signed in to change notification settings - Fork 3
/
prometheus.js
82 lines (67 loc) · 2.4 KB
/
prometheus.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
'use strict';
/* List all DRC metrics:
curl http://localhost:9090/api/v1/metadata | jq '.data | keys | map(select(. | contains("drc_")))'
*/
const config = require('./config');
const { PREFIX } = require('./lib/constants');
const promClient = require('prom-client');
const Redis = require(process.env.NODE_ENV === 'test' ? 'ioredis-mock' : 'ioredis');
const processClient = new Redis(config.redis.url);
const listenClient = new Redis(config.redis.url);
const app = require('fastify')({
logger: true
});
require('./logger')('prometheus');
promClient.collectDefaultMetrics();
const redisMessageCounter = new promClient.Counter({
name: 'drc_redis_message_count',
help: 'Counts of DRC Redis messages',
labelNames: ['type', 'subType']
});
const HANDLERS = {
prometheus: async () => ([await promClient.register.metrics(), promClient.register.contentType])
};
app.get('/:component/metrics', async (req, res) => {
if (!HANDLERS[req.params.component]) {
return res.send('');
}
const [body, contentType] = await HANDLERS[req.params.component]();
return res.type(contentType).send(body);
});
app.listen(config.prometheus.listenPort, (err, addr) => {
if (err) { throw err; }
console.log(`Listening on ${addr}`);
processClient.on('message', async (_chan, message) => {
try {
const { type, data } = JSON.parse(message);
const { metrics, contentType } = data;
const [p, e, processName] = type.split(':');
if (p !== 'prometheus' || e !== 'export') {
throw new Error(`bad type ${type}`);
}
if (!HANDLERS[processName]) {
console.log(`Saw new exporter for process "${processName}"`);
}
HANDLERS[processName] = async () => ([metrics, contentType]);
} catch (e) {
console.error(':prometheus:export bad message', message, e);
}
});
listenClient.on('pmessage', async (_pattern, channel, message) => {
try {
const { type } = JSON.parse(message);
const [mainType, ...subTypePath] = type.split(':');
redisMessageCounter.inc({ type: mainType, subType: subTypePath.join(':') });
} catch (e) {
console.error('Unknown redis message shape!', channel, message);
}
});
processClient.subscribe(`${PREFIX}:prometheus:export`);
listenClient.psubscribe(`${PREFIX}*`);
process.on('SIGINT', () => {
console.log('Exiting...');
processClient.disconnect();
listenClient.disconnect();
app.close();
});
});