Add serving metrics for Prometheus
Co-authored-by: TheEssem <smswessem@gmail.com>
This commit is contained in:
parent
84e20308f2
commit
a868fd0698
2 changed files with 41 additions and 4 deletions
|
@ -44,6 +44,8 @@ OUTPUT=
|
||||||
TEMPDIR=
|
TEMPDIR=
|
||||||
# Put temporary image web server domain
|
# Put temporary image web server domain
|
||||||
TMP_DOMAIN=
|
TMP_DOMAIN=
|
||||||
|
# Port for serving metrics. Metrics served are compatible with Prometheus.
|
||||||
|
METRICS=
|
||||||
|
|
||||||
# Set this to true if you're using PM2 to manage the bot
|
# Set this to true if you're using PM2 to manage the bot
|
||||||
PMTWO=false
|
PMTWO=false
|
||||||
|
|
35
shard.js
35
shard.js
|
@ -21,6 +21,7 @@ const misc = require("./utils/misc.js");
|
||||||
// generate help page
|
// generate help page
|
||||||
const helpGenerator =
|
const helpGenerator =
|
||||||
process.env.OUTPUT !== "" ? require("./utils/help.js") : null;
|
process.env.OUTPUT !== "" ? require("./utils/help.js") : null;
|
||||||
|
const http = require("http");
|
||||||
|
|
||||||
class Shard extends Base {
|
class Shard extends Base {
|
||||||
constructor(bot) {
|
constructor(bot) {
|
||||||
|
@ -68,6 +69,40 @@ class Shard extends Base {
|
||||||
logger.log("info", "The help docs have been generated.");
|
logger.log("info", "The help docs have been generated.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (process.env.METRICS !== "") {
|
||||||
|
logger.log("YES");
|
||||||
|
const httpServer = http.createServer((req, res) => {
|
||||||
|
if (req.method !== "GET") {
|
||||||
|
res.statusCode = 405;
|
||||||
|
return res.end("GET only");
|
||||||
|
}
|
||||||
|
res.write(`# HELP connected_workers Number of workers connected
|
||||||
|
# TYPE connected_workers guage
|
||||||
|
connected_workers ${image.connections.length}
|
||||||
|
# HELP running_jobs Number of running jobs on this worker
|
||||||
|
# TYPE running_jobs guage
|
||||||
|
# HELP queued_jobs Number of queued jobs on this worker
|
||||||
|
# TYPE queued_jobs guage
|
||||||
|
# HELP max_jobs Number of max allowed jobs on this worker
|
||||||
|
# TYPE max_jobs guage
|
||||||
|
`);
|
||||||
|
image.getStatus().then((servers) => {
|
||||||
|
for (const [i, w] of servers.entries()) {
|
||||||
|
res.write(`running_jobs{worker="${i}} ${w.runningJobs}\n`);
|
||||||
|
res.write(`queued_jobs{worker="${i}} ${w.queued}\n`);
|
||||||
|
res.write(`max_jobs{worker="${i}} ${w.max}\n`);
|
||||||
|
}
|
||||||
|
res.end();
|
||||||
|
}).catch(e => {
|
||||||
|
res.statusCode = 500;
|
||||||
|
return res.end(e);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
httpServer.listen(process.env.METRICS, () => {
|
||||||
|
logger.log("info", `Serving metrics at ${process.env.METRICS}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// handle process stop
|
// handle process stop
|
||||||
process.on("SIGINT", () => {
|
process.on("SIGINT", () => {
|
||||||
logger.log("warn", "SIGINT detected, shutting down...");
|
logger.log("warn", "SIGINT detected, shutting down...");
|
||||||
|
|
Loading…
Reference in a new issue