From fefd29fbc1e7877cef392850aeff0fd78d611c0f Mon Sep 17 00:00:00 2001 From: Jan Date: Sat, 20 Aug 2022 13:05:21 +0200 Subject: [PATCH] count bridged messages in db for metrics --- bot/src/bot/db.ts | 7 ++++++- bridge/src/metrics.ts | 44 +++++++++++++++++++++++++++++++++---------- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/bot/src/bot/db.ts b/bot/src/bot/db.ts index 73ecb32..723b983 100644 --- a/bot/src/bot/db.ts +++ b/bot/src/bot/db.ts @@ -46,7 +46,12 @@ async function databaseMigrations() { await setIndexes(dbs.BRIDGE_CONFIG, [ 'discord', '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.PENDING_LOGINS, [ 'code', 'user' ]); await setIndexes(dbs.SERVERS, [ 'id' ]); diff --git a/bridge/src/metrics.ts b/bridge/src/metrics.ts index c9a342d..87486c5 100644 --- a/bridge/src/metrics.ts +++ b/bridge/src/metrics.ts @@ -1,28 +1,40 @@ import prom from 'prom-client'; import http from 'http'; -import { BRIDGE_CONFIG, logger } from '.'; +import { BRIDGED_MESSAGES, BRIDGE_CONFIG, logger } from "."; const PORT = Number(process.env.BRIDGE_METRICS_PORT); -prom.collectDefaultMetrics({ prefix: 'automod_bridge_' }); +prom.collectDefaultMetrics({ prefix: "automod_bridge_" }); const metrics = { - messages: new prom.Counter({ name: 'messages', help: 'Bridged message events', labelNames: [ 'source', 'type' ] }), - bridged_channels: new prom.Gauge({ name: 'bridged_channels', help: 'How many channels are bridged' }), -} + messages: new prom.Counter({ + 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)) { logger.info(`Enabling Prometheus metrics on :${PORT}`); const server = new http.Server(); - server.on('request', async (req, res) => { - if (req.url == '/metrics') { + 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.write("404 not found"); res.end(); } }); @@ -30,11 +42,23 @@ if (!isNaN(PORT)) { server.listen(PORT, () => logger.done(`Prometheus metrics ready`)); 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(); - setInterval(updateMetrics, 1000 * 10); + setInterval(updateMetrics, 1000 * 60); } export { metrics }