diff --git a/src/main/java/ru/ruscalworld/fabricexporter/MetricRegistry.java b/src/main/java/ru/ruscalworld/fabricexporter/MetricRegistry.java index 0ed7dc6..b13ffc3 100644 --- a/src/main/java/ru/ruscalworld/fabricexporter/MetricRegistry.java +++ b/src/main/java/ru/ruscalworld/fabricexporter/MetricRegistry.java @@ -71,6 +71,16 @@ public class MetricRegistry { return "minecraft_" + name; } + public static String[] getGlobalLabelNames() { + return new String[] { "instance" }; + } + + public String[] getGlobalLabelValues() { + return new String[] { + this.getExporter().getConfig().getInstanceName(), // server + }; + } + public List getMetrics() { return metrics; } diff --git a/src/main/java/ru/ruscalworld/fabricexporter/MetricUpdater.java b/src/main/java/ru/ruscalworld/fabricexporter/MetricUpdater.java index b14a9bc..71e3143 100644 --- a/src/main/java/ru/ruscalworld/fabricexporter/MetricUpdater.java +++ b/src/main/java/ru/ruscalworld/fabricexporter/MetricUpdater.java @@ -18,7 +18,7 @@ public class MetricUpdater extends TimerTask { public void run() { for (Metric metric : this.getMetrics()) { try { - metric.update(this.getExporter()); + metric.onShouldUpdate(this.getExporter()); } catch (Exception exception) { exception.printStackTrace(); } diff --git a/src/main/java/ru/ruscalworld/fabricexporter/config/MainConfig.java b/src/main/java/ru/ruscalworld/fabricexporter/config/MainConfig.java index 7f215cb..3b57e8b 100644 --- a/src/main/java/ru/ruscalworld/fabricexporter/config/MainConfig.java +++ b/src/main/java/ru/ruscalworld/fabricexporter/config/MainConfig.java @@ -8,6 +8,7 @@ public class MainConfig extends Config { private int port; private int updateInterval; private boolean useSpark; + private String instanceName; public MainConfig(String name) { super(name); @@ -24,6 +25,7 @@ public class MainConfig extends Config { this.setUpdateInterval(ConvertUtil.intToStringOrDefault(updateIntervalString, 1000)); this.setShouldUseSpark(properties.getProperty("use-spark", "true").equalsIgnoreCase("true")); + this.setInstanceName(properties.getProperty("instance-name", "default")); } public int getPort() { @@ -49,4 +51,12 @@ public class MainConfig extends Config { public void setShouldUseSpark(boolean useSpark) { this.useSpark = useSpark; } + + public String getInstanceName() { + return instanceName; + } + + public void setInstanceName(String instanceName) { + this.instanceName = instanceName; + } } diff --git a/src/main/java/ru/ruscalworld/fabricexporter/metrics/Entities.java b/src/main/java/ru/ruscalworld/fabricexporter/metrics/Entities.java index 085da3a..fb02f75 100644 --- a/src/main/java/ru/ruscalworld/fabricexporter/metrics/Entities.java +++ b/src/main/java/ru/ruscalworld/fabricexporter/metrics/Entities.java @@ -15,7 +15,7 @@ public class Entities extends Metric { } @Override - public void update(FabricExporter exporter) { + public void onShouldUpdate(FabricExporter exporter) { for (ServerWorld world : exporter.getServer().getWorlds()) { HashMap currentWorldEntities = new HashMap<>(); @@ -28,7 +28,7 @@ public class Entities extends Metric { for (String type : currentWorldEntities.keySet()) { Integer count = currentWorldEntities.get(type); EntityType entityType = Registry.ENTITY_TYPE.get(new Identifier(type)); - this.getGauge().labels(TextUtil.getWorldName(world), entityType.getSpawnGroup().getName(), type).set(count); + this.update(TextUtil.getWorldName(world), entityType.getSpawnGroup().getName(), type).set(count); } } } diff --git a/src/main/java/ru/ruscalworld/fabricexporter/metrics/LoadedChunks.java b/src/main/java/ru/ruscalworld/fabricexporter/metrics/LoadedChunks.java index 2b01553..31c2b67 100644 --- a/src/main/java/ru/ruscalworld/fabricexporter/metrics/LoadedChunks.java +++ b/src/main/java/ru/ruscalworld/fabricexporter/metrics/LoadedChunks.java @@ -10,9 +10,9 @@ public class LoadedChunks extends Metric { } @Override - public void update(FabricExporter exporter) { + public void onShouldUpdate(FabricExporter exporter) { for (ServerWorld world : exporter.getServer().getWorlds()) { - this.getGauge().labels(TextUtil.getWorldName(world)).set(world.getChunkManager().getLoadedChunkCount()); + this.update(TextUtil.getWorldName(world)).set(world.getChunkManager().getLoadedChunkCount()); } } } diff --git a/src/main/java/ru/ruscalworld/fabricexporter/metrics/Metric.java b/src/main/java/ru/ruscalworld/fabricexporter/metrics/Metric.java index 442e8f5..57123a2 100644 --- a/src/main/java/ru/ruscalworld/fabricexporter/metrics/Metric.java +++ b/src/main/java/ru/ruscalworld/fabricexporter/metrics/Metric.java @@ -1,6 +1,7 @@ package ru.ruscalworld.fabricexporter.metrics; import io.prometheus.client.Gauge; +import org.apache.commons.lang3.ArrayUtils; import ru.ruscalworld.fabricexporter.FabricExporter; import ru.ruscalworld.fabricexporter.MetricRegistry; @@ -13,11 +14,11 @@ public abstract class Metric { this.gauge = new Gauge.Builder() .name(MetricRegistry.getMetricName(name)) .help(help) - .labelNames(labels) + .labelNames(ArrayUtils.addAll(labels, MetricRegistry.getGlobalLabelNames())) .create(); } - public abstract void update(FabricExporter exporter); + public abstract void onShouldUpdate(FabricExporter exporter); public Gauge getGauge() { return gauge; @@ -26,4 +27,9 @@ public abstract class Metric { public String getName() { return name; } + + public Gauge.Child update(String... labelValues) { + MetricRegistry metricRegistry = FabricExporter.getInstance().getMetricRegistry(); + return this.getGauge().labels(ArrayUtils.addAll(labelValues, metricRegistry.getGlobalLabelValues())); + } } diff --git a/src/main/java/ru/ruscalworld/fabricexporter/metrics/MillisPerTick.java b/src/main/java/ru/ruscalworld/fabricexporter/metrics/MillisPerTick.java index 67bd397..ede7a0a 100644 --- a/src/main/java/ru/ruscalworld/fabricexporter/metrics/MillisPerTick.java +++ b/src/main/java/ru/ruscalworld/fabricexporter/metrics/MillisPerTick.java @@ -11,7 +11,7 @@ public class MillisPerTick extends SparkMetric { } @Override - public void update(FabricExporter exporter) { + public void onShouldUpdate(FabricExporter exporter) { GenericStatistic mspt = this.getSpark().mspt(); if (mspt == null) this.setValue(0, 0, 0); else this.setValue( @@ -22,8 +22,8 @@ public class MillisPerTick extends SparkMetric { } private void setValue(double min, double mean, double max) { - this.getGauge().labels("min").set(min); - this.getGauge().labels("mean").set(mean); - this.getGauge().labels("max").set(max); + this.update("min").set(min); + this.update("mean").set(mean); + this.update("max").set(max); } } diff --git a/src/main/java/ru/ruscalworld/fabricexporter/metrics/OnlinePlayers.java b/src/main/java/ru/ruscalworld/fabricexporter/metrics/OnlinePlayers.java index a4fb1e8..37a3fac 100644 --- a/src/main/java/ru/ruscalworld/fabricexporter/metrics/OnlinePlayers.java +++ b/src/main/java/ru/ruscalworld/fabricexporter/metrics/OnlinePlayers.java @@ -10,9 +10,9 @@ public class OnlinePlayers extends Metric { } @Override - public void update(FabricExporter exporter) { + public void onShouldUpdate(FabricExporter exporter) { for (ServerWorld world : exporter.getServer().getWorlds()) { - this.getGauge().labels(TextUtil.getWorldName(world)).set(world.getPlayers().size()); + this.update(TextUtil.getWorldName(world)).set(world.getPlayers().size()); } } } diff --git a/src/main/java/ru/ruscalworld/fabricexporter/metrics/TicksPerSecond.java b/src/main/java/ru/ruscalworld/fabricexporter/metrics/TicksPerSecond.java index 3f85ff6..4dc8344 100644 --- a/src/main/java/ru/ruscalworld/fabricexporter/metrics/TicksPerSecond.java +++ b/src/main/java/ru/ruscalworld/fabricexporter/metrics/TicksPerSecond.java @@ -10,9 +10,9 @@ public class TicksPerSecond extends SparkMetric { } @Override - public void update(FabricExporter exporter) { + public void onShouldUpdate(FabricExporter exporter) { DoubleStatistic tps = this.getSpark().tps(); - if (tps == null) this.getGauge().set(20); - else this.getGauge().set(tps.poll(StatisticWindow.TicksPerSecond.MINUTES_1)); + if (tps == null) this.update().set(20); + else this.update().set(tps.poll(StatisticWindow.TicksPerSecond.MINUTES_1)); } } diff --git a/src/main/java/ru/ruscalworld/fabricexporter/metrics/TotalLoadedChunks.java b/src/main/java/ru/ruscalworld/fabricexporter/metrics/TotalLoadedChunks.java index 74fc854..8f61a97 100644 --- a/src/main/java/ru/ruscalworld/fabricexporter/metrics/TotalLoadedChunks.java +++ b/src/main/java/ru/ruscalworld/fabricexporter/metrics/TotalLoadedChunks.java @@ -10,9 +10,9 @@ public class TotalLoadedChunks extends Metric { } @Override - public void update(FabricExporter exporter) { + public void onShouldUpdate(FabricExporter exporter) { for (ServerWorld world : exporter.getServer().getWorlds()) { - this.getGauge().labels(TextUtil.getWorldName(world)).set(world.getChunkManager().getTotalChunksLoadedCount()); + this.update(TextUtil.getWorldName(world)).set(world.getChunkManager().getTotalChunksLoadedCount()); } } } diff --git a/src/main/resources/config/exporter.properties b/src/main/resources/config/exporter.properties index bed1a7c..fae39e2 100644 --- a/src/main/resources/config/exporter.properties +++ b/src/main/resources/config/exporter.properties @@ -2,6 +2,11 @@ # Default: 25585 server-port=25585 +# Name of this instance used to identify it when running multiple servers +# Must be different on all your servers that use FabricExporter +# Default: default +instance-name=default + # FabricExporter updates gauge metrics every N milliseconds # You can change this value if you want metrics to be updated more or less frequently # Value must be provided in milliseconds, 1 second = 1000 milliseconds