Skip to content

Commit

Permalink
add metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Ampa1R committed Nov 12, 2023
1 parent 6db9eff commit b7cc48b
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 6 deletions.
4 changes: 2 additions & 2 deletions config.json.example
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"TOKEN": "",
"MAX_PLAYLIST_SIZE": 10,
"PREFIX": "/",
"PRUNING": false,
"LOCALE": "en",
"STAY_TIME": 30,
"DEFAULT_VOLUME": 100
"DEFAULT_VOLUME": 100,
"METRICS_PORT": 3000
}
3 changes: 3 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Client, GatewayIntentBits } from "discord.js";
import startMetricsServer from "./metrics";
import { Bot } from "./structs/Bot";

startMetricsServer();

export const bot = new Bot(
new Client({
intents: [
Expand Down
2 changes: 1 addition & 1 deletion interfaces/Config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export interface Config {
TOKEN: string;
PREFIX: string;
MAX_PLAYLIST_SIZE: number;
PRUNING: boolean;
STAY_TIME: number;
DEFAULT_VOLUME: number;
LOCALE: string;
METRICS_PORT: number;
}
34 changes: 34 additions & 0 deletions metrics/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import http from 'http';
import promClient from 'prom-client';
import { config } from '../utils/config';
import { Logger } from '../utils/logger';

const PORT = config.METRICS_PORT;

export default function startMetricsServer() {
promClient.collectDefaultMetrics();

const server = http.createServer(async function (req, res) {
try {
if (req.url === '/metrics') {
res.setHeader('Content-Type', promClient.register.contentType);
res.write(await promClient.register.metrics());
res.end();
return;
}
res.statusCode = 404;
res.write('Not found');
res.end();
} catch (err) {
Logger.error(`Metrics server response error`);
Logger.error(err);
res.statusCode = 500;
res.write('Internal server error');
res.end();
}
})

server.listen(PORT, function () {
console.log(`Metrics server started at port ${PORT}`);
});
}
34 changes: 34 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"i18n": "^0.15.1",
"lyrics-finder": "^21.0.5",
"play-dl": "^1.9.7",
"prom-client": "^15.0.0",
"soundcloud-downloader": "^0.2.3",
"string-progressbar": "^1.0.4",
"typescript": "^5.2.2",
Expand Down
2 changes: 1 addition & 1 deletion structs/Bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { Song } from "./Song";

export class Bot {
private readonly logger = new Logger(this.constructor.name);
public readonly prefix = config.PREFIX;
public readonly prefix = `/`;
public commands = new Collection<string, Command>();
public slashCommands = new Array<ApplicationCommandDataResolvable>();
public slashCommandsMap = new Collection<string, Command>();
Expand Down
4 changes: 2 additions & 2 deletions utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ try {
} catch (error) {
config = {
TOKEN: process.env.TOKEN || "",
PREFIX: process.env.PREFIX || "!",
MAX_PLAYLIST_SIZE: 500, // parseInt(process.env.MAX_PLAYLIST_SIZE!) || 10,
PRUNING: process.env.PRUNING === "true" ? true : false,
STAY_TIME: parseInt(process.env.STAY_TIME!) || 30,
DEFAULT_VOLUME: parseInt(process.env.DEFAULT_VOLUME!) || 100,
LOCALE: process.env.LOCALE || "en"
LOCALE: process.env.LOCALE || "en",
METRICS_PORT: parseInt(process.env.METRICS_PORT!) || 3000,
};
}

Expand Down

0 comments on commit b7cc48b

Please sign in to comment.