Skip to content

Commit

Permalink
Add prometheus metrics, currently only meteor_discord_users_total
Browse files Browse the repository at this point in the history
  • Loading branch information
MineGame159 committed May 19, 2024
1 parent c2e81c7 commit a7674e0
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/org/meteordev/meteorbot/MeteorBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static void main(String[] args) {
JDABuilder.createDefault(token)
.enableIntents(GatewayIntent.GUILD_MEMBERS, GatewayIntent.MESSAGE_CONTENT)
.enableCache(CacheFlag.EMOJI)
.addEventListeners(new MeteorBot(), new Commands(), new Uptime(), new InfoChannels())
.addEventListeners(new MeteorBot(), new Commands(), new Uptime(), new InfoChannels(), new Metrics())
.build();
}

Expand Down
78 changes: 78 additions & 0 deletions src/main/java/org/meteordev/meteorbot/Metrics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.meteordev.meteorbot;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpServer;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.events.session.ReadyEvent;
import net.dv8tion.jda.api.events.session.ShutdownEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;

public class Metrics extends ListenerAdapter {
private Guild guild;
private HttpServer server;

@Override
public void onReady(ReadyEvent event) {
JDA bot = event.getJDA();

guild = bot.getGuildById(Env.GUILD_ID.value);
if (guild == null) {
MeteorBot.LOG.error("Couldn't find the specified server.");
System.exit(1);
}

try {
server = HttpServer.create(new InetSocketAddress(9400), 0);
server.createContext("/metrics", this::onRequest);
server.start();
} catch (IOException e) {
throw new RuntimeException(e);
}

MeteorBot.LOG.info("Providing metrics on :9400/metrics");
}

@Override
public void onShutdown(ShutdownEvent event) {
server.stop(1000);
}

private void onRequest(HttpExchange exchange) {
byte[] response = String.format("""
# HELP meteor_discord_users_total Total number of Discord users in our server
# TYPE meteor_discord_users_total gauge
meteor_discord_users_total %d
""", guild.getMemberCount()).getBytes(StandardCharsets.UTF_8);

try {
exchange.getResponseHeaders().set("Content-Type", "text/plain");
exchange.sendResponseHeaders(200, response.length);
} catch (IOException e) {
throw new RuntimeException(e);
}

OutputStream out = exchange.getResponseBody();
copy(response, out);
}

@SuppressWarnings("ThrowFromFinallyBlock")
private static void copy(byte[] in, OutputStream out) {
try {
out.write(in);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
out.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}

0 comments on commit a7674e0

Please sign in to comment.