diff --git a/src/main/java/ru/ruscalworld/fabricexporter/FabricExporter.java b/src/main/java/ru/ruscalworld/fabricexporter/FabricExporter.java index 774d124..92348db 100644 --- a/src/main/java/ru/ruscalworld/fabricexporter/FabricExporter.java +++ b/src/main/java/ru/ruscalworld/fabricexporter/FabricExporter.java @@ -7,12 +7,12 @@ import net.minecraft.server.MinecraftServer; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import ru.ruscalworld.fabricexporter.config.MainConfig; -import ru.ruscalworld.fabricexporter.metrics.*; import java.io.IOException; -import java.util.Timer; public class FabricExporter implements ModInitializer { + private static FabricExporter instance; + private MinecraftServer server; private MainConfig config; private HTTPServer httpServer; @@ -50,6 +50,8 @@ public class FabricExporter implements ModInitializer { this.getHttpServer().stop(); this.getMetricRegistry().getTimer().cancel(); }); + + instance = this; } public static Logger getLogger() { @@ -87,4 +89,8 @@ public class FabricExporter implements ModInitializer { private void setMetricRegistry(MetricRegistry metricRegistry) { this.metricRegistry = metricRegistry; } + + public static FabricExporter getInstance() { + return instance; + } } diff --git a/src/main/java/ru/ruscalworld/fabricexporter/MetricRegistry.java b/src/main/java/ru/ruscalworld/fabricexporter/MetricRegistry.java index bd433dc..10eb26d 100644 --- a/src/main/java/ru/ruscalworld/fabricexporter/MetricRegistry.java +++ b/src/main/java/ru/ruscalworld/fabricexporter/MetricRegistry.java @@ -1,16 +1,20 @@ package ru.ruscalworld.fabricexporter; import io.prometheus.client.Collector; +import io.prometheus.client.Counter; +import io.prometheus.client.SimpleCollector; import ru.ruscalworld.fabricexporter.config.MainConfig; import ru.ruscalworld.fabricexporter.metrics.*; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Timer; public class MetricRegistry { private final FabricExporter exporter; private final List metrics = new ArrayList<>(); + private final HashMap customMetrics = new HashMap<>(); private final Timer timer; public MetricRegistry(FabricExporter exporter) { @@ -32,25 +36,36 @@ public class MetricRegistry { this.registerMetric(new TotalLoadedChunks()); this.registerMetric(new TicksPerSecond()); this.registerMetric(new MillisPerTick()); + + this.registerCustomMetric("handshakes", new Counter.Builder() + .name(getMetricName("handshakes")) + .help("Amount of handshake requests") + .labelNames("type") + ); } public void registerMetric(Metric metric) { MainConfig config = this.getExporter().getConfig(); if (metric instanceof SparkMetric && !config.shouldUseSpark()) return; - if (!this.isEnabled(metric.getName())) return; + if (this.isDisabled(metric.getName())) return; metric.getGauge().register(); this.metrics.add(metric); } - public void registerCustomMetric(String name, Collector collector) { - if (this.isEnabled(name)) collector.register(); + public void registerCustomMetric(String name, SimpleCollector.Builder collector) { + if (this.isDisabled(name)) return; + this.getCustomMetrics().put(name, collector.register()); } - public boolean isEnabled(String name) { + public boolean isDisabled(String name) { MainConfig config = this.getExporter().getConfig(); String property = "enable-" + name.replace("_", "-"); - return config.getProperties().getProperty(property, "true").equals("true"); + return !config.getProperties().getProperty(property, "true").equals("true"); + } + + public static String getMetricName(String name) { + return "minecraft_" + name; } public List getMetrics() { @@ -64,4 +79,8 @@ public class MetricRegistry { public Timer getTimer() { return timer; } + + public HashMap getCustomMetrics() { + return customMetrics; + } } diff --git a/src/main/java/ru/ruscalworld/fabricexporter/metrics/Metric.java b/src/main/java/ru/ruscalworld/fabricexporter/metrics/Metric.java index 154f9b8..442e8f5 100644 --- a/src/main/java/ru/ruscalworld/fabricexporter/metrics/Metric.java +++ b/src/main/java/ru/ruscalworld/fabricexporter/metrics/Metric.java @@ -2,6 +2,7 @@ package ru.ruscalworld.fabricexporter.metrics; import io.prometheus.client.Gauge; import ru.ruscalworld.fabricexporter.FabricExporter; +import ru.ruscalworld.fabricexporter.MetricRegistry; public abstract class Metric { private final Gauge gauge; @@ -10,7 +11,7 @@ public abstract class Metric { public Metric(String name, String help, String... labels) { this.name = name; this.gauge = new Gauge.Builder() - .name("minecraft_" + name) + .name(MetricRegistry.getMetricName(name)) .help(help) .labelNames(labels) .create(); diff --git a/src/main/java/ru/ruscalworld/fabricexporter/mixin/ServerHandshakeNetworkHandlerMixin.java b/src/main/java/ru/ruscalworld/fabricexporter/mixin/ServerHandshakeNetworkHandlerMixin.java new file mode 100644 index 0000000..a64fd85 --- /dev/null +++ b/src/main/java/ru/ruscalworld/fabricexporter/mixin/ServerHandshakeNetworkHandlerMixin.java @@ -0,0 +1,24 @@ +package ru.ruscalworld.fabricexporter.mixin; + +import io.prometheus.client.Collector; +import io.prometheus.client.Counter; +import net.minecraft.network.packet.c2s.handshake.HandshakeC2SPacket; +import net.minecraft.server.network.ServerHandshakeNetworkHandler; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import ru.ruscalworld.fabricexporter.FabricExporter; +import ru.ruscalworld.fabricexporter.MetricRegistry; + +@Mixin(ServerHandshakeNetworkHandler.class) +public class ServerHandshakeNetworkHandlerMixin { + @Inject(method = "onHandshake", at = @At("HEAD")) + public void onHandshake(HandshakeC2SPacket packet, CallbackInfo info) { + MetricRegistry metricRegistry = FabricExporter.getInstance().getMetricRegistry(); + Collector collector = metricRegistry.getCustomMetrics().get("handshakes"); + if (!(collector instanceof Counter)) return; + Counter counter = (Counter) collector; + counter.labels(packet.getIntendedState().name().toLowerCase()).inc(); + } +} diff --git a/src/main/resources/fabricexporter.mixins.json b/src/main/resources/fabricexporter.mixins.json index 0429ae6..418b468 100644 --- a/src/main/resources/fabricexporter.mixins.json +++ b/src/main/resources/fabricexporter.mixins.json @@ -3,9 +3,8 @@ "minVersion": "0.8", "package": "ru.ruscalworld.fabricexporter.mixin", "compatibilityLevel": "JAVA_8", - "mixins": [ - ], - "client": [ + "server": [ + "ServerHandshakeNetworkHandlerMixin" ], "injectors": { "defaultRequire": 1