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 MinecraftServer server;
|
||||||
private MainConfig config;
|
private MainConfig config;
|
||||||
private HTTPServer httpServer;
|
private HTTPServer httpServer;
|
||||||
|
private MetricRegistry metricRegistry;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onInitialize() {
|
public void onInitialize() {
|
||||||
|
@ -28,18 +29,9 @@ public class FabricExporter implements ModInitializer {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
MetricUpdater metricUpdater = new MetricUpdater(this);
|
MetricRegistry metricRegistry = new MetricRegistry(this);
|
||||||
metricUpdater.registerMetric(new OnlinePlayers());
|
metricRegistry.registerDefault();
|
||||||
metricUpdater.registerMetric(new Entities());
|
this.setMetricRegistry(metricRegistry);
|
||||||
metricUpdater.registerMetric(new LoadedChunks());
|
|
||||||
metricUpdater.registerMetric(new TotalLoadedChunks());
|
|
||||||
|
|
||||||
if (this.getConfig().shouldUseSpark()) {
|
|
||||||
metricUpdater.registerMetric(new TicksPerSecond());
|
|
||||||
metricUpdater.registerMetric(new MillisPerTick());
|
|
||||||
}
|
|
||||||
|
|
||||||
Timer timer = new Timer();
|
|
||||||
|
|
||||||
ServerLifecycleEvents.SERVER_STARTING.register(this::setServer);
|
ServerLifecycleEvents.SERVER_STARTING.register(this::setServer);
|
||||||
ServerLifecycleEvents.SERVER_STARTED.register(server -> {
|
ServerLifecycleEvents.SERVER_STARTED.register(server -> {
|
||||||
|
@ -48,7 +40,7 @@ public class FabricExporter implements ModInitializer {
|
||||||
this.setHttpServer(new HTTPServer(port));
|
this.setHttpServer(new HTTPServer(port));
|
||||||
FabricExporter.getLogger().info("Prometheus exporter server is now listening on port " + 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) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -56,7 +48,7 @@ public class FabricExporter implements ModInitializer {
|
||||||
|
|
||||||
ServerLifecycleEvents.SERVER_STOPPING.register(server -> {
|
ServerLifecycleEvents.SERVER_STOPPING.register(server -> {
|
||||||
this.getHttpServer().stop();
|
this.getHttpServer().stop();
|
||||||
timer.cancel();
|
this.getMetricRegistry().getTimer().cancel();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,4 +79,12 @@ public class FabricExporter implements ModInitializer {
|
||||||
public void setHttpServer(HTTPServer httpServer) {
|
public void setHttpServer(HTTPServer httpServer) {
|
||||||
this.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;
|
package ru.ruscalworld.fabricexporter.config;
|
||||||
|
|
||||||
import net.fabricmc.loader.api.FabricLoader;
|
import net.fabricmc.loader.api.FabricLoader;
|
||||||
import org.apache.logging.log4j.LogManager;
|
|
||||||
import ru.ruscalworld.fabricexporter.FabricExporter;
|
import ru.ruscalworld.fabricexporter.FabricExporter;
|
||||||
import ru.ruscalworld.fabricexporter.util.FileUtil;
|
import ru.ruscalworld.fabricexporter.util.FileUtil;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.net.URL;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -16,6 +13,7 @@ import java.util.Properties;
|
||||||
|
|
||||||
public abstract class Config {
|
public abstract class Config {
|
||||||
private final String fileName;
|
private final String fileName;
|
||||||
|
private Properties properties;
|
||||||
|
|
||||||
public Config(String fileName) {
|
public Config(String fileName) {
|
||||||
this.fileName = fileName;
|
this.fileName = fileName;
|
||||||
|
@ -36,12 +34,21 @@ public abstract class Config {
|
||||||
|
|
||||||
Properties properties = new Properties();
|
Properties properties = new Properties();
|
||||||
properties.load(Files.newInputStream(path));
|
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() {
|
public String getFileName() {
|
||||||
return fileName;
|
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
|
@Override
|
||||||
public void parse(Properties properties) {
|
public void onLoad() {
|
||||||
|
Properties properties = this.getProperties();
|
||||||
|
|
||||||
String portString = properties.getProperty("server-port", "25585");
|
String portString = properties.getProperty("server-port", "25585");
|
||||||
this.setPort(ConvertUtil.intToStringOrDefault(portString, 25585));
|
this.setPort(ConvertUtil.intToStringOrDefault(portString, 25585));
|
||||||
|
|
||||||
|
|
|
@ -5,13 +5,15 @@ import ru.ruscalworld.fabricexporter.FabricExporter;
|
||||||
|
|
||||||
public abstract class Metric {
|
public abstract class Metric {
|
||||||
private final Gauge gauge;
|
private final Gauge gauge;
|
||||||
|
private final String name;
|
||||||
|
|
||||||
public Metric(String name, String help, String... labels) {
|
public Metric(String name, String help, String... labels) {
|
||||||
|
this.name = name;
|
||||||
this.gauge = new Gauge.Builder()
|
this.gauge = new Gauge.Builder()
|
||||||
.name("minecraft_" + name)
|
.name("minecraft_" + name)
|
||||||
.help(help)
|
.help(help)
|
||||||
.labelNames(labels)
|
.labelNames(labels)
|
||||||
.create().register();
|
.create();
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract void update(FabricExporter exporter);
|
public abstract void update(FabricExporter exporter);
|
||||||
|
@ -19,4 +21,8 @@ public abstract class Metric {
|
||||||
public Gauge getGauge() {
|
public Gauge getGauge() {
|
||||||
return gauge;
|
return gauge;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue