count bridged messages in db for metrics

This commit is contained in:
Jan 2022-08-20 13:05:21 +02:00
parent eab2064518
commit fefd29fbc1
Signed by: Lea
GPG key ID: 5D5E18ACB990F57A
2 changed files with 40 additions and 11 deletions

View file

@ -46,7 +46,12 @@ async function databaseMigrations() {
await setIndexes(dbs.BRIDGE_CONFIG, [ 'discord', 'revolt' ]); await setIndexes(dbs.BRIDGE_CONFIG, [ 'discord', 'revolt' ]);
await setIndexes(dbs.BRIDGE_REQUESTS, [ 'id', 'revolt' ]); await setIndexes(dbs.BRIDGE_REQUESTS, [ 'id', 'revolt' ]);
await setIndexes(dbs.BRIDGED_MESSAGES, [ 'discord.messageId', 'revolt.messageId', 'revolt.nonce' ]); await setIndexes(dbs.BRIDGED_MESSAGES, [
"discord.messageId",
"revolt.messageId",
"revolt.nonce",
"origin",
]);
await setIndexes(dbs.INFRACTIONS, [ 'createdBy', 'user', 'server' ]); await setIndexes(dbs.INFRACTIONS, [ 'createdBy', 'user', 'server' ]);
await setIndexes(dbs.PENDING_LOGINS, [ 'code', 'user' ]); await setIndexes(dbs.PENDING_LOGINS, [ 'code', 'user' ]);
await setIndexes(dbs.SERVERS, [ 'id' ]); await setIndexes(dbs.SERVERS, [ 'id' ]);

View file

@ -1,28 +1,40 @@
import prom from 'prom-client'; import prom from 'prom-client';
import http from 'http'; import http from 'http';
import { BRIDGE_CONFIG, logger } from '.'; import { BRIDGED_MESSAGES, BRIDGE_CONFIG, logger } from ".";
const PORT = Number(process.env.BRIDGE_METRICS_PORT); const PORT = Number(process.env.BRIDGE_METRICS_PORT);
prom.collectDefaultMetrics({ prefix: 'automod_bridge_' }); prom.collectDefaultMetrics({ prefix: "automod_bridge_" });
const metrics = { const metrics = {
messages: new prom.Counter({ name: 'messages', help: 'Bridged message events', labelNames: [ 'source', 'type' ] }), messages: new prom.Counter({
bridged_channels: new prom.Gauge({ name: 'bridged_channels', help: 'How many channels are bridged' }), name: "messages",
} help: "Bridged message events",
labelNames: ["source", "type"],
}),
bridged_channels: new prom.Gauge({
name: "bridged_channels",
help: "How many channels are bridged",
}),
db_messages: new prom.Gauge({
name: "db_messages",
help: "Number of bridged message documents in the database",
labelNames: ["source"],
}),
};
if (!isNaN(PORT)) { if (!isNaN(PORT)) {
logger.info(`Enabling Prometheus metrics on :${PORT}`); logger.info(`Enabling Prometheus metrics on :${PORT}`);
const server = new http.Server(); const server = new http.Server();
server.on('request', async (req, res) => { server.on("request", async (req, res) => {
if (req.url == '/metrics') { if (req.url == "/metrics") {
res.write(await prom.register.metrics()); res.write(await prom.register.metrics());
res.end(); res.end();
} else { } else {
res.statusCode = 404; res.statusCode = 404;
res.write('404 not found'); res.write("404 not found");
res.end(); res.end();
} }
}); });
@ -30,11 +42,23 @@ if (!isNaN(PORT)) {
server.listen(PORT, () => logger.done(`Prometheus metrics ready`)); server.listen(PORT, () => logger.done(`Prometheus metrics ready`));
async function updateMetrics() { async function updateMetrics() {
metrics.bridged_channels.set(await BRIDGE_CONFIG.count({ })); const now = Date.now();
metrics.bridged_channels.set(await BRIDGE_CONFIG.count({}));
const [revolt, discord] = await Promise.all([
BRIDGED_MESSAGES.count({ origin: "revolt" }),
BRIDGED_MESSAGES.count({ origin: "discord" }),
]);
metrics.db_messages.set({ source: "revolt" }, revolt);
metrics.db_messages.set({ source: "discord" }, discord);
logger.debug(`Fetching database metrics took ${Date.now() - now} ms`);
} }
updateMetrics(); updateMetrics();
setInterval(updateMetrics, 1000 * 10); setInterval(updateMetrics, 1000 * 60);
} }
export { metrics } export { metrics }