From bad6896a5e2558d51cded3a3f1ac88ad9803fa96 Mon Sep 17 00:00:00 2001 From: RuscalWorld Date: Tue, 25 May 2021 21:44:14 +0300 Subject: [PATCH] Refactor metric registration, add ability to disable metrics in config --- .../fabricexporter/FabricExporter.java | 28 ++++---- .../fabricexporter/MetricRegistry.java | 67 +++++++++++++++++++ .../fabricexporter/config/Config.java | 17 +++-- .../fabricexporter/config/MainConfig.java | 4 +- .../fabricexporter/metrics/Metric.java | 8 ++- 5 files changed, 103 insertions(+), 21 deletions(-) create mode 100644 src/main/java/ru/ruscalworld/fabricexporter/MetricRegistry.java diff --git a/src/main/java/ru/ruscalworld/fabricexporter/FabricExporter.java b/src/main/java/ru/ruscalworld/fabricexporter/FabricExporter.java index 09ac4ee..774d124 100644 --- a/src/main/java/ru/ruscalworld/fabricexporter/FabricExporter.java +++ b/src/main/java/ru/ruscalworld/fabricexporter/FabricExporter.java @@ -16,6 +16,7 @@ public class FabricExporter implements ModInitializer { private MinecraftServer server; private MainConfig config; private HTTPServer httpServer; + private MetricRegistry metricRegistry; @Override public void onInitialize() { @@ -28,18 +29,9 @@ public class FabricExporter implements ModInitializer { e.printStackTrace(); } - MetricUpdater metricUpdater = new MetricUpdater(this); - metricUpdater.registerMetric(new OnlinePlayers()); - metricUpdater.registerMetric(new Entities()); - metricUpdater.registerMetric(new LoadedChunks()); - metricUpdater.registerMetric(new TotalLoadedChunks()); - - if (this.getConfig().shouldUseSpark()) { - metricUpdater.registerMetric(new TicksPerSecond()); - metricUpdater.registerMetric(new MillisPerTick()); - } - - Timer timer = new Timer(); + MetricRegistry metricRegistry = new MetricRegistry(this); + metricRegistry.registerDefault(); + this.setMetricRegistry(metricRegistry); ServerLifecycleEvents.SERVER_STARTING.register(this::setServer); ServerLifecycleEvents.SERVER_STARTED.register(server -> { @@ -48,7 +40,7 @@ public class FabricExporter implements ModInitializer { this.setHttpServer(new HTTPServer(port)); FabricExporter.getLogger().info("Prometheus exporter server is now listening on port " + port); - timer.schedule(metricUpdater, 1000, this.getConfig().getUpdateInterval()); + this.getMetricRegistry().runUpdater(); } catch (IOException e) { e.printStackTrace(); } @@ -56,7 +48,7 @@ public class FabricExporter implements ModInitializer { ServerLifecycleEvents.SERVER_STOPPING.register(server -> { this.getHttpServer().stop(); - timer.cancel(); + this.getMetricRegistry().getTimer().cancel(); }); } @@ -87,4 +79,12 @@ public class FabricExporter implements ModInitializer { public void setHttpServer(HTTPServer httpServer) { this.httpServer = httpServer; } + + public MetricRegistry getMetricRegistry() { + return metricRegistry; + } + + private void setMetricRegistry(MetricRegistry metricRegistry) { + this.metricRegistry = metricRegistry; + } } diff --git a/src/main/java/ru/ruscalworld/fabricexporter/MetricRegistry.java b/src/main/java/ru/ruscalworld/fabricexporter/MetricRegistry.java new file mode 100644 index 0000000..bd433dc --- /dev/null +++ b/src/main/java/ru/ruscalworld/fabricexporter/MetricRegistry.java @@ -0,0 +1,67 @@ +package ru.ruscalworld.fabricexporter; + +import io.prometheus.client.Collector; +import ru.ruscalworld.fabricexporter.config.MainConfig; +import ru.ruscalworld.fabricexporter.metrics.*; + +import java.util.ArrayList; +import java.util.List; +import java.util.Timer; + +public class MetricRegistry { + private final FabricExporter exporter; + private final List metrics = new ArrayList<>(); + private final Timer timer; + + public MetricRegistry(FabricExporter exporter) { + this.exporter = exporter; + timer = new Timer(); + } + + public void runUpdater() { + MainConfig config = this.getExporter().getConfig(); + MetricUpdater metricUpdater = new MetricUpdater(this.getExporter()); + this.getMetrics().forEach(metricUpdater::registerMetric); + this.getTimer().schedule(metricUpdater, 1000, config.getUpdateInterval()); + } + + public void registerDefault() { + this.registerMetric(new OnlinePlayers()); + this.registerMetric(new Entities()); + this.registerMetric(new LoadedChunks()); + this.registerMetric(new TotalLoadedChunks()); + this.registerMetric(new TicksPerSecond()); + this.registerMetric(new MillisPerTick()); + } + + public void registerMetric(Metric metric) { + MainConfig config = this.getExporter().getConfig(); + if (metric instanceof SparkMetric && !config.shouldUseSpark()) return; + if (!this.isEnabled(metric.getName())) return; + + metric.getGauge().register(); + this.metrics.add(metric); + } + + public void registerCustomMetric(String name, Collector collector) { + if (this.isEnabled(name)) collector.register(); + } + + public boolean isEnabled(String name) { + MainConfig config = this.getExporter().getConfig(); + String property = "enable-" + name.replace("_", "-"); + return config.getProperties().getProperty(property, "true").equals("true"); + } + + public List getMetrics() { + return metrics; + } + + public FabricExporter getExporter() { + return exporter; + } + + public Timer getTimer() { + return timer; + } +} diff --git a/src/main/java/ru/ruscalworld/fabricexporter/config/Config.java b/src/main/java/ru/ruscalworld/fabricexporter/config/Config.java index 6865da3..eacc0c4 100644 --- a/src/main/java/ru/ruscalworld/fabricexporter/config/Config.java +++ b/src/main/java/ru/ruscalworld/fabricexporter/config/Config.java @@ -1,14 +1,11 @@ package ru.ruscalworld.fabricexporter.config; import net.fabricmc.loader.api.FabricLoader; -import org.apache.logging.log4j.LogManager; import ru.ruscalworld.fabricexporter.FabricExporter; import ru.ruscalworld.fabricexporter.util.FileUtil; -import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; import java.util.List; @@ -16,6 +13,7 @@ import java.util.Properties; public abstract class Config { private final String fileName; + private Properties properties; public Config(String fileName) { this.fileName = fileName; @@ -36,12 +34,21 @@ public abstract class Config { Properties properties = new Properties(); properties.load(Files.newInputStream(path)); - this.parse(properties); + this.setProperties(properties); + this.onLoad(); } - public abstract void parse(Properties properties); + public abstract void onLoad(); public String getFileName() { return fileName; } + + public Properties getProperties() { + return properties; + } + + public void setProperties(Properties properties) { + this.properties = properties; + } } diff --git a/src/main/java/ru/ruscalworld/fabricexporter/config/MainConfig.java b/src/main/java/ru/ruscalworld/fabricexporter/config/MainConfig.java index 8f54a33..7f215cb 100644 --- a/src/main/java/ru/ruscalworld/fabricexporter/config/MainConfig.java +++ b/src/main/java/ru/ruscalworld/fabricexporter/config/MainConfig.java @@ -14,7 +14,9 @@ public class MainConfig extends Config { } @Override - public void parse(Properties properties) { + public void onLoad() { + Properties properties = this.getProperties(); + String portString = properties.getProperty("server-port", "25585"); this.setPort(ConvertUtil.intToStringOrDefault(portString, 25585)); diff --git a/src/main/java/ru/ruscalworld/fabricexporter/metrics/Metric.java b/src/main/java/ru/ruscalworld/fabricexporter/metrics/Metric.java index cec7796..154f9b8 100644 --- a/src/main/java/ru/ruscalworld/fabricexporter/metrics/Metric.java +++ b/src/main/java/ru/ruscalworld/fabricexporter/metrics/Metric.java @@ -5,13 +5,15 @@ import ru.ruscalworld.fabricexporter.FabricExporter; public abstract class Metric { private final Gauge gauge; + private final String name; public Metric(String name, String help, String... labels) { + this.name = name; this.gauge = new Gauge.Builder() .name("minecraft_" + name) .help(help) .labelNames(labels) - .create().register(); + .create(); } public abstract void update(FabricExporter exporter); @@ -19,4 +21,8 @@ public abstract class Metric { public Gauge getGauge() { return gauge; } + + public String getName() { + return name; + } }