2021-08-19 14:19:14 +00:00
|
|
|
import { BaseServiceWorker } from "eris-fleet";
|
|
|
|
import { createServer } from "http";
|
|
|
|
import { log } from "../logger.js";
|
|
|
|
import database from "../database.js";
|
2021-07-06 14:22:51 +00:00
|
|
|
|
|
|
|
class PrometheusWorker extends BaseServiceWorker {
|
|
|
|
constructor(setup) {
|
|
|
|
super(setup);
|
|
|
|
|
|
|
|
if (process.env.METRICS !== "" && process.env.METRICS !== undefined) {
|
2021-08-19 14:19:14 +00:00
|
|
|
this.httpServer = createServer(async (req, res) => {
|
2021-07-06 14:22:51 +00:00
|
|
|
if (req.method !== "GET") {
|
|
|
|
res.statusCode = 405;
|
|
|
|
return res.end("GET only");
|
|
|
|
}
|
2021-07-14 23:42:19 +00:00
|
|
|
res.write(`# HELP esmbot_command_count Number of times a command has been run
|
|
|
|
# TYPE esmbot_command_count counter
|
2021-08-17 04:04:11 +00:00
|
|
|
# HELP esmbot_server_count Number of servers/guilds the bot is in
|
|
|
|
# TYPE esmbot_server_count gauge
|
2021-09-20 23:32:59 +00:00
|
|
|
# HELP esmbot_shard_count Number of shards the bot has
|
|
|
|
# TYPE esmbot_shard_count gauge
|
2021-07-06 14:22:51 +00:00
|
|
|
`);
|
|
|
|
if (process.env.API === "true") {
|
|
|
|
const servers = await this.ipc.command("image", { type: "stats" }, true);
|
2021-07-14 23:42:19 +00:00
|
|
|
res.write(`# HELP esmbot_connected_workers Number of workers connected
|
|
|
|
# TYPE esmbot_connected_workers gauge
|
|
|
|
esmbot_connected_workers ${servers.length}
|
|
|
|
# HELP esmbot_running_jobs Number of running jobs on this worker
|
|
|
|
# TYPE esmbot_running_jobs gauge
|
|
|
|
# HELP esmbot_queued_jobs Number of queued jobs on this worker
|
|
|
|
# TYPE esmbot_queued_jobs gauge
|
|
|
|
# HELP esmbot_max_jobs Number of max allowed jobs on this worker
|
|
|
|
# TYPE esmbot_max_jobs gauge
|
2021-07-06 14:22:51 +00:00
|
|
|
`);
|
|
|
|
for (const [i, w] of servers.entries()) {
|
2021-07-14 23:42:19 +00:00
|
|
|
res.write(`esmbot_running_jobs{worker="${i}"} ${w.runningJobs}\n`);
|
|
|
|
res.write(`esmbot_queued_jobs{worker="${i}"} ${w.queued}\n`);
|
|
|
|
res.write(`esmbot_max_jobs{worker="${i}"} ${w.max}\n`);
|
2021-07-06 14:22:51 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-13 03:28:09 +00:00
|
|
|
const counts = await database.getCounts();
|
2021-07-06 14:22:51 +00:00
|
|
|
for (const [i, w] of Object.entries(counts)) {
|
2021-07-14 23:42:19 +00:00
|
|
|
res.write(`esmbot_command_count{command="${i}"} ${w}\n`);
|
2021-07-06 14:22:51 +00:00
|
|
|
}
|
2021-08-17 04:04:11 +00:00
|
|
|
|
|
|
|
const stats = await this.ipc.getStats();
|
|
|
|
res.write(`esmbot_server_count ${stats.guilds}\n`);
|
2021-09-20 23:32:59 +00:00
|
|
|
res.write(`esmbot_shard_count ${stats.shardCount}\n`);
|
2021-07-06 14:22:51 +00:00
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
this.httpServer.listen(process.env.METRICS, () => {
|
2021-08-19 14:19:14 +00:00
|
|
|
log("info", `Serving metrics at ${process.env.METRICS}`);
|
2021-07-06 14:22:51 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.serviceReady();
|
|
|
|
}
|
|
|
|
|
|
|
|
shutdown(done) {
|
2021-08-12 23:45:17 +00:00
|
|
|
if (this.httpServer) this.httpServer.close();
|
2021-07-06 14:22:51 +00:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-19 14:19:14 +00:00
|
|
|
export default PrometheusWorker;
|