Implement global label values

This commit is contained in:
RuscalWorld 2021-12-11 15:16:59 +03:00
parent a3f39f5e65
commit 3791510ac0
No known key found for this signature in database
GPG key ID: 6FF416979DF2B416
11 changed files with 49 additions and 18 deletions

View file

@ -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<Metric> getMetrics() {
return metrics;
}

View file

@ -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();
}

View file

@ -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;
}
}

View file

@ -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<String, Integer> 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);
}
}
}

View file

@ -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());
}
}
}

View file

@ -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()));
}
}

View file

@ -11,7 +11,7 @@ public class MillisPerTick extends SparkMetric {
}
@Override
public void update(FabricExporter exporter) {
public void onShouldUpdate(FabricExporter exporter) {
GenericStatistic<DoubleAverageInfo, StatisticWindow.MillisPerTick> 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);
}
}

View file

@ -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());
}
}
}

View file

@ -10,9 +10,9 @@ public class TicksPerSecond extends SparkMetric {
}
@Override
public void update(FabricExporter exporter) {
public void onShouldUpdate(FabricExporter exporter) {
DoubleStatistic<StatisticWindow.TicksPerSecond> 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));
}
}

View file

@ -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());
}
}
}

View file

@ -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