generated from nhcarrigan/template
feat: add prometheus metrics
This commit is contained in:
@ -3,6 +3,7 @@ import { scheduleJob } from "node-schedule";
|
||||
import { ExtendedClient } from "../../interfaces/ExtendedClient";
|
||||
import { maintainSecurity } from "../../modules/maintainSecurity";
|
||||
import { postBirthdays } from "../../modules/postBirthdays";
|
||||
import { Prometheus } from "../../modules/prometheus";
|
||||
import { registerCommands } from "../../utils/registerCommands";
|
||||
import { sendDebugMessage } from "../../utils/sendDebugMessage";
|
||||
|
||||
@ -14,6 +15,7 @@ import { sendDebugMessage } from "../../utils/sendDebugMessage";
|
||||
export const onReady = async (bot: ExtendedClient) => {
|
||||
await sendDebugMessage(bot, `Logged in as ${bot.user?.tag}`);
|
||||
await registerCommands(bot);
|
||||
bot.analytics = new Prometheus(bot);
|
||||
|
||||
// Daily at 9am PST
|
||||
scheduleJob("birthdays", "0 9 * * *", async () => await postBirthdays(bot));
|
||||
|
@ -15,4 +15,5 @@ export const onGuildCreate = async function (
|
||||
await bot.env.debugHook.send({
|
||||
content: `JOINED GUILD: ${guild.name} (${guild.id}) - owned by ${owner?.displayName} (${owner.id})`
|
||||
});
|
||||
bot.analytics.updateGuilds(bot);
|
||||
};
|
||||
|
@ -36,6 +36,7 @@ export const onGuildDelete = async function (
|
||||
await bot.db.security
|
||||
.deleteMany({ where: { serverId: guild.id } })
|
||||
.catch(() => null);
|
||||
bot.analytics.updateGuilds(bot);
|
||||
} catch (err) {
|
||||
await errorHandler(bot, "on guild delete", err);
|
||||
}
|
||||
|
@ -35,6 +35,7 @@ export const onInteraction = async (
|
||||
);
|
||||
return;
|
||||
}
|
||||
bot.analytics.commandUsed();
|
||||
if (interaction.isChatInputCommand()) {
|
||||
handleChatInputCommand(bot, interaction);
|
||||
}
|
||||
|
@ -4,6 +4,8 @@ import { Client, WebhookClient } from "discord.js";
|
||||
import { Command } from "./Command";
|
||||
import { Context } from "./Context";
|
||||
|
||||
import type { Prometheus } from "../modules/prometheus.js";
|
||||
|
||||
export interface ExtendedClient extends Client {
|
||||
env: {
|
||||
token: string;
|
||||
@ -12,6 +14,7 @@ export interface ExtendedClient extends Client {
|
||||
devMode: boolean;
|
||||
};
|
||||
db: PrismaClient;
|
||||
analytics: Prometheus;
|
||||
commands: Command[];
|
||||
contexts: Context[];
|
||||
configs: { [serverId: string]: Omit<configs, "id"> };
|
||||
|
29
src/modules/prometheus.ts
Normal file
29
src/modules/prometheus.ts
Normal file
@ -0,0 +1,29 @@
|
||||
import client, { Counter, Gauge } from "prom-client";
|
||||
import type { ExtendedClient } from "../interfaces/ExtendedClient.js";
|
||||
|
||||
export class Prometheus {
|
||||
private client = client;
|
||||
private guilds: Gauge;
|
||||
private commands: Counter;
|
||||
|
||||
constructor(bot: ExtendedClient) {
|
||||
this.guilds = new Gauge({
|
||||
name: "guilds",
|
||||
help: "The number of guilds the bot is in."
|
||||
});
|
||||
this.guilds.set(bot.guilds.cache.size);
|
||||
this.commands = new Counter({
|
||||
name: "commands",
|
||||
help: "The number of commands that have been used since last boot."
|
||||
});
|
||||
this.client.collectDefaultMetrics();
|
||||
}
|
||||
|
||||
public commandUsed() {
|
||||
this.commands.inc();
|
||||
}
|
||||
|
||||
public updateGuilds(bot: ExtendedClient) {
|
||||
this.guilds.set(bot.guilds.cache.size);
|
||||
}
|
||||
}
|
@ -6,6 +6,7 @@ import https from "https";
|
||||
import { Octokit } from "@octokit/rest";
|
||||
import { GuildTextBasedChannel } from "discord.js";
|
||||
import express from "express";
|
||||
import { register } from "prom-client";
|
||||
|
||||
import { IgnoredActors, ThankYou } from "../config/Github";
|
||||
import { ExtendedClient } from "../interfaces/ExtendedClient";
|
||||
@ -47,6 +48,15 @@ export const serve = async (bot: ExtendedClient) => {
|
||||
res.send("bot online!");
|
||||
});
|
||||
|
||||
app.get("/metrics", async (_req, res) => {
|
||||
try {
|
||||
res.set("Content-Type", register.contentType);
|
||||
res.end(await register.metrics());
|
||||
} catch (err) {
|
||||
res.status(500).end(err);
|
||||
}
|
||||
});
|
||||
|
||||
app.post("/kofi", async (req, res) => {
|
||||
const payload = JSON.parse(req.body.data);
|
||||
const {
|
||||
|
Reference in New Issue
Block a user