Refactor metric registration, add ability to disable metrics in config
This commit is contained in:
parent
2506639051
commit
bad6896a5e
5 changed files with 103 additions and 21 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<Metric> 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<Metric> getMetrics() {
|
||||
return metrics;
|
||||
}
|
||||
|
||||
public FabricExporter getExporter() {
|
||||
return exporter;
|
||||
}
|
||||
|
||||
public Timer getTimer() {
|
||||
return timer;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue