2022-04-28 05:31:02 +00:00
|
|
|
import prom from 'prom-client';
|
|
|
|
import http from 'http';
|
2022-05-01 12:40:40 +00:00
|
|
|
import { BRIDGE_CONFIG, logger } from '.';
|
2022-04-28 05:31:02 +00:00
|
|
|
|
|
|
|
const PORT = Number(process.env.BRIDGE_METRICS_PORT);
|
|
|
|
|
|
|
|
prom.collectDefaultMetrics({ prefix: 'automod_bridge_' });
|
|
|
|
|
|
|
|
const metrics = {
|
|
|
|
messages: new prom.Counter({ name: 'messages', help: 'Bridged message events', labelNames: [ 'source', 'type' ] }),
|
2022-05-01 12:40:40 +00:00
|
|
|
bridged_channels: new prom.Gauge({ name: 'bridged_channels', help: 'How many channels are bridged' }),
|
2022-04-28 05:31:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isNaN(PORT)) {
|
|
|
|
logger.info(`Enabling Prometheus metrics on :${PORT}`);
|
|
|
|
|
|
|
|
const server = new http.Server();
|
|
|
|
|
|
|
|
server.on('request', async (req, res) => {
|
|
|
|
if (req.url == '/metrics') {
|
|
|
|
res.write(await prom.register.metrics());
|
|
|
|
res.end();
|
|
|
|
} else {
|
|
|
|
res.statusCode = 404;
|
|
|
|
res.write('404 not found');
|
|
|
|
res.end();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
server.listen(PORT, () => logger.done(`Prometheus metrics ready`));
|
2022-05-01 12:40:40 +00:00
|
|
|
|
|
|
|
async function updateMetrics() {
|
|
|
|
metrics.bridged_channels.set(await BRIDGE_CONFIG.count({ }));
|
|
|
|
}
|
|
|
|
|
|
|
|
updateMetrics();
|
|
|
|
setInterval(updateMetrics, 1000 * 10);
|
2022-04-28 05:31:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export { metrics }
|