Refactor metric updating, implement entity metrics

This commit is contained in:
RuscalWorld 2021-05-25 00:20:28 +03:00
parent 19951d3f36
commit a4c4b144f8
No known key found for this signature in database
GPG key ID: 4F53776031D128ED
7 changed files with 53 additions and 12 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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