From a4c4b144f8fdd0387f9b2121827038e72025bbd7 Mon Sep 17 00:00:00 2001 From: RuscalWorld Date: Tue, 25 May 2021 00:20:28 +0300 Subject: [PATCH] Refactor metric updating, implement entity metrics --- .../fabricexporter/FabricExporter.java | 2 ++ .../fabricexporter/MetricUpdater.java | 2 +- .../fabricexporter/metrics/Entities.java | 35 +++++++++++++++++++ .../fabricexporter/metrics/Metric.java | 10 ++++-- .../fabricexporter/metrics/MillisPerTick.java | 6 ++-- .../fabricexporter/metrics/OnlinePlayers.java | 4 +-- .../metrics/TicksPerSecond.java | 6 ++-- 7 files changed, 53 insertions(+), 12 deletions(-) create mode 100644 src/main/java/ru/ruscalworld/fabricexporter/metrics/Entities.java diff --git a/src/main/java/ru/ruscalworld/fabricexporter/FabricExporter.java b/src/main/java/ru/ruscalworld/fabricexporter/FabricExporter.java index 4fdc909..78f615a 100644 --- a/src/main/java/ru/ruscalworld/fabricexporter/FabricExporter.java +++ b/src/main/java/ru/ruscalworld/fabricexporter/FabricExporter.java @@ -7,6 +7,7 @@ import net.minecraft.server.MinecraftServer; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import ru.ruscalworld.fabricexporter.config.MainConfig; +import ru.ruscalworld.fabricexporter.metrics.Entities; import ru.ruscalworld.fabricexporter.metrics.OnlinePlayers; import ru.ruscalworld.fabricexporter.metrics.TicksPerSecond; import ru.ruscalworld.fabricexporter.metrics.MillisPerTick; @@ -33,6 +34,7 @@ public class FabricExporter implements ModInitializer { metricUpdater.registerMetric(new OnlinePlayers()); metricUpdater.registerMetric(new TicksPerSecond()); metricUpdater.registerMetric(new MillisPerTick()); + metricUpdater.registerMetric(new Entities()); ServerLifecycleEvents.SERVER_STARTING.register(this::setServer); ServerLifecycleEvents.SERVER_STARTED.register(server -> { diff --git a/src/main/java/ru/ruscalworld/fabricexporter/MetricUpdater.java b/src/main/java/ru/ruscalworld/fabricexporter/MetricUpdater.java index b46d6ec..b14a9bc 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.getGauge().set(metric.getCurrentValue(this.getExporter())); + metric.update(this.getExporter()); } catch (Exception exception) { exception.printStackTrace(); } diff --git a/src/main/java/ru/ruscalworld/fabricexporter/metrics/Entities.java b/src/main/java/ru/ruscalworld/fabricexporter/metrics/Entities.java new file mode 100644 index 0000000..88050ac --- /dev/null +++ b/src/main/java/ru/ruscalworld/fabricexporter/metrics/Entities.java @@ -0,0 +1,35 @@ +package ru.ruscalworld.fabricexporter.metrics; + +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityType; +import net.minecraft.server.world.ServerWorld; +import net.minecraft.util.Identifier; +import net.minecraft.util.registry.Registry; +import ru.ruscalworld.fabricexporter.FabricExporter; + +import java.util.HashMap; + +public class Entities extends Metric { + public Entities() { + super("entities", "Amount of entities in the world", "world", "group", "type"); + } + + @Override + public void update(FabricExporter exporter) { + for (ServerWorld world : exporter.getServer().getWorlds()) { + HashMap currentWorldEntities = new HashMap<>(); + + for (Entity entity : world.getEntitiesByType(null, entity -> true)) { + String name = Registry.ENTITY_TYPE.getId(entity.getType()).getPath(); + Integer typeCount = currentWorldEntities.getOrDefault(name, 0); + currentWorldEntities.put(name, typeCount + 1); + } + + for (String type : currentWorldEntities.keySet()) { + Integer count = currentWorldEntities.get(type); + EntityType entityType = Registry.ENTITY_TYPE.get(new Identifier(type)); + this.getGauge().labels(world.getRegistryKey().getValue().getPath(), entityType.getSpawnGroup().getName(), type).set(count); + } + } + } +} diff --git a/src/main/java/ru/ruscalworld/fabricexporter/metrics/Metric.java b/src/main/java/ru/ruscalworld/fabricexporter/metrics/Metric.java index 3affa7f..cec7796 100644 --- a/src/main/java/ru/ruscalworld/fabricexporter/metrics/Metric.java +++ b/src/main/java/ru/ruscalworld/fabricexporter/metrics/Metric.java @@ -6,11 +6,15 @@ import ru.ruscalworld.fabricexporter.FabricExporter; public abstract class Metric { private final Gauge gauge; - public Metric(String name, String help) { - this.gauge = new Gauge.Builder().name("minecraft_" + name).help(help).create().register(); + public Metric(String name, String help, String... labels) { + this.gauge = new Gauge.Builder() + .name("minecraft_" + name) + .help(help) + .labelNames(labels) + .create().register(); } - public abstract double getCurrentValue(FabricExporter exporter); + public abstract void update(FabricExporter exporter); public Gauge getGauge() { return gauge; diff --git a/src/main/java/ru/ruscalworld/fabricexporter/metrics/MillisPerTick.java b/src/main/java/ru/ruscalworld/fabricexporter/metrics/MillisPerTick.java index d7f93fd..6697437 100644 --- a/src/main/java/ru/ruscalworld/fabricexporter/metrics/MillisPerTick.java +++ b/src/main/java/ru/ruscalworld/fabricexporter/metrics/MillisPerTick.java @@ -11,9 +11,9 @@ public class MillisPerTick extends SparkMetric { } @Override - public double getCurrentValue(FabricExporter exporter) { + public void update(FabricExporter exporter) { GenericStatistic mspt = this.getSpark().mspt(); - if (mspt == null) return 0; - return mspt.poll(StatisticWindow.MillisPerTick.MINUTES_1).mean(); + if (mspt == null) this.getGauge().set(0); + else this.getGauge().set(mspt.poll(StatisticWindow.MillisPerTick.MINUTES_1).mean()); } } diff --git a/src/main/java/ru/ruscalworld/fabricexporter/metrics/OnlinePlayers.java b/src/main/java/ru/ruscalworld/fabricexporter/metrics/OnlinePlayers.java index 0f2abb8..1f2f799 100644 --- a/src/main/java/ru/ruscalworld/fabricexporter/metrics/OnlinePlayers.java +++ b/src/main/java/ru/ruscalworld/fabricexporter/metrics/OnlinePlayers.java @@ -8,7 +8,7 @@ public class OnlinePlayers extends Metric { } @Override - public double getCurrentValue(FabricExporter exporter) { - return exporter.getServer().getCurrentPlayerCount(); + public void update(FabricExporter exporter) { + this.getGauge().set(exporter.getServer().getCurrentPlayerCount()); } } diff --git a/src/main/java/ru/ruscalworld/fabricexporter/metrics/TicksPerSecond.java b/src/main/java/ru/ruscalworld/fabricexporter/metrics/TicksPerSecond.java index 792875c..3f85ff6 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 double getCurrentValue(FabricExporter exporter) { + public void update(FabricExporter exporter) { DoubleStatistic tps = this.getSpark().tps(); - if (tps == null) return 20; - return tps.poll(StatisticWindow.TicksPerSecond.MINUTES_1); + if (tps == null) this.getGauge().set(20); + else this.getGauge().set(tps.poll(StatisticWindow.TicksPerSecond.MINUTES_1)); } }