mirror of
https://github.com/janderedev/automod.git
synced 2024-12-22 10:45:27 +00:00
count bridged messages in db for metrics
This commit is contained in:
parent
eab2064518
commit
fefd29fbc1
|
@ -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' ]);
|
||||
|
|
|
@ -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 }
|
||||
|
|
Loading…
Reference in a new issue