Skip to content

Commit

Permalink
prometheus metrics export
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Jul 30, 2021
1 parent 8417858 commit 59f7f47
Show file tree
Hide file tree
Showing 16 changed files with 170 additions and 6 deletions.
13 changes: 13 additions & 0 deletions .idea/libraries/Maven__io_prometheus_simpleclient_0_11_0.xml

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

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

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

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

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

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

6 changes: 6 additions & 0 deletions MinecraftTransport-Server.iml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,11 @@
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.15" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-all:4.1.66.Final" level="project" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.7" level="project" />
<orderEntry type="library" name="Maven: io.prometheus:simpleclient:0.11.0" level="project" />
<orderEntry type="library" name="Maven: io.prometheus:simpleclient_tracer_otel:0.11.0" level="project" />
<orderEntry type="library" name="Maven: io.prometheus:simpleclient_tracer_common:0.11.0" level="project" />
<orderEntry type="library" name="Maven: io.prometheus:simpleclient_tracer_otel_agent:0.11.0" level="project" />
<orderEntry type="library" name="Maven: io.prometheus:simpleclient_httpserver:0.11.0" level="project" />
<orderEntry type="library" name="Maven: io.prometheus:simpleclient_common:0.11.0" level="project" />
</component>
</module>
12 changes: 11 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.rezxis</groupId>
<artifactId>MinecraftTransport-Server</artifactId>
<version>0.0.3-SNAPSHOT</version>
<version>0.0.4-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
Expand Down Expand Up @@ -58,5 +58,15 @@
<artifactId>gson</artifactId>
<version>2.8.7</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient</artifactId>
<version>0.11.0</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_httpserver</artifactId>
<version>0.11.0</version>
</dependency>
</dependencies>
</project>
8 changes: 7 additions & 1 deletion src/main/java/net/rezxis/mctp/server/ChannelMirror.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,27 @@
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import net.rezxis.mctp.server.prometheus.MCTPPrometheus;

@ChannelHandler.Sharable
public class ChannelMirror extends ChannelInboundHandlerAdapter {

private final Channel dest;
private final boolean notice;

public ChannelMirror(Channel dest) {
public ChannelMirror(Channel dest, boolean notice) {
this.dest = dest;
this.notice = notice;
}

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
if (!dest.isActive())
return;
dest.close();
if (notice) {
MCTPPrometheus.instance.decreaseConnectedSessions();
}
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/rezxis/mctp/server/Console.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static void error(String message) {

public static void exception(Throwable ex) {
ex.printStackTrace();
pw.print(ex.getMessage());
ex.printStackTrace(pw);
pw.flush();
}

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/net/rezxis/mctp/server/MCTPConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public class MCTPConfig {
public String host;
public int port_start;
public int port_range;
public boolean enable_prometheus_metrics;
public String prometheus_metrics_host;
public int prometheus_metrics_port;

public static void load(File file) throws Exception {
if (!file.exists()) {
Expand All @@ -31,6 +34,9 @@ public static void generate(File file) throws Exception {
instance.listen_port = 9998;
instance.port_start = 40000;
instance.port_range = 10000;
instance.enable_prometheus_metrics = false;
instance.prometheus_metrics_host = "0.0.0.0";
instance.prometheus_metrics_port = 9997;
instance.host = getMyIp();
file.createNewFile();
PrintWriter pw = new PrintWriter(file);
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/net/rezxis/mctp/server/MCTPConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import net.rezxis.mctp.server.prometheus.MCTPPrometheus;
import net.rezxis.mctp.server.proxied.ProxiedServer;
import net.rezxis.mctp.server.util.PacketDecoder;
import net.rezxis.mctp.server.util.PacketEncoder;
Expand Down Expand Up @@ -30,6 +31,7 @@ public void init() {
secret = new Random().nextLong();
children = new HashMap<>();
proxy = new ProxiedServer(this);
MCTPPrometheus.instance.increaseConnectedMCTPServers();
new Thread(proxy).start();
}

Expand Down Expand Up @@ -67,8 +69,9 @@ public void upgradeConnection(ChannelHandlerContext ctx1, long id) {
for (ByteBuf buf : waiting.attr(MCTPVars.PACKET_STACK).get()) {
ctx1.writeAndFlush(buf);
}
waiting.pipeline().addLast(new ChannelMirror(ctx1.channel()));
ctx1.pipeline().addLast(new ChannelMirror(waiting.channel()));
waiting.pipeline().addLast(new ChannelMirror(ctx1.channel(), false));
ctx1.pipeline().addLast(new ChannelMirror(waiting.channel(), true));
MCTPPrometheus.instance.increaseConnectedSessions();
}

public void close() {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/net/rezxis/mctp/server/MCTPServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import net.rezxis.mctp.server.prometheus.MCTPPrometheus;
import net.rezxis.mctp.server.util.PacketDecoder;
import net.rezxis.mctp.server.util.PacketEncoder;

Expand Down Expand Up @@ -73,12 +74,13 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception {
MCTPConnection connection = ctx.attr(MCTPVars.CONNECTION_KEY).get();
connection.close();
ServerManager.removeConnection(connection);
MCTPPrometheus.instance.decreaseConnectedMCTPServers();
Console.info("disconnected mctp connection from " + connection.getIp());
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
Console.info("occurred exception in mctp connnection from "+((InetSocketAddress)ctx.channel().remoteAddress()).getHostName());
Console.info("occurred exception in mctp connnection from "+((InetSocketAddress)ctx.channel().remoteAddress()).getHostString());
Console.exception(cause);
if (ctx.channel().isOpen()) {
ctx.close();
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/net/rezxis/mctp/server/MinecraftTPMain.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.rezxis.mctp.server;

import net.rezxis.mctp.server.prometheus.MCTPPrometheus;

import java.io.File;

public class MinecraftTPMain {
Expand All @@ -14,6 +16,7 @@ public static void main(String[] args) {
System.exit(-1);
return;
}
new Thread(new MCTPPrometheus()).start();
Console.info("Loaded configuration file.");
Console.info("Starting MCTP-Server!");
Console.info("Listen : "+MCTPConfig.instance.listen_host+":"+MCTPConfig.instance.listen_port);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package net.rezxis.mctp.server.prometheus;

import io.prometheus.client.Gauge;
import io.prometheus.client.exporter.HTTPServer;
import net.rezxis.mctp.server.Console;
import net.rezxis.mctp.server.MCTPConfig;

import java.io.IOException;

public class MCTPPrometheus implements Runnable {

public static MCTPPrometheus instance;
private Gauge connectedMCTPServers;
private Gauge connectedSessions;

@Override
public void run() {
instance = this;
if (!MCTPConfig.instance.enable_prometheus_metrics)
return;
this.connectedMCTPServers = Gauge.build().name("connected_mctp").help("connected mctp servers").register();
this.connectedSessions = Gauge.build().name("connected_sessions").help("connected sessions").register();
try {
HTTPServer server = new HTTPServer(MCTPConfig.instance.prometheus_metrics_host, MCTPConfig.instance.prometheus_metrics_port);
} catch (IOException e) {
Console.exception(e);
}
}

public void increaseConnectedMCTPServers() {
if (MCTPConfig.instance.enable_prometheus_metrics)
this.connectedMCTPServers.inc();
}

public void decreaseConnectedMCTPServers() {
if (MCTPConfig.instance.enable_prometheus_metrics)
this.connectedMCTPServers.dec();
}

public void increaseConnectedSessions() {
if (MCTPConfig.instance.enable_prometheus_metrics)
this.connectedSessions.inc();
}

public void decreaseConnectedSessions() {
if (MCTPConfig.instance.enable_prometheus_metrics)
this.connectedSessions.dec();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import net.rezxis.mctp.server.*;
import net.rezxis.mctp.server.prometheus.MCTPPrometheus;

import java.util.ArrayList;

Expand Down

0 comments on commit 59f7f47

Please sign in to comment.