diff --git a/src/main/java/ru/ruscalworld/fabricexporter/FabricExporter.java b/src/main/java/ru/ruscalworld/fabricexporter/FabricExporter.java index be27955..4fdc909 100644 --- a/src/main/java/ru/ruscalworld/fabricexporter/FabricExporter.java +++ b/src/main/java/ru/ruscalworld/fabricexporter/FabricExporter.java @@ -8,6 +8,8 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import ru.ruscalworld.fabricexporter.config.MainConfig; import ru.ruscalworld.fabricexporter.metrics.OnlinePlayers; +import ru.ruscalworld.fabricexporter.metrics.TicksPerSecond; +import ru.ruscalworld.fabricexporter.metrics.MillisPerTick; import java.io.IOException; import java.util.Timer; @@ -29,6 +31,8 @@ public class FabricExporter implements ModInitializer { MetricUpdater metricUpdater = new MetricUpdater(this); metricUpdater.registerMetric(new OnlinePlayers()); + metricUpdater.registerMetric(new TicksPerSecond()); + metricUpdater.registerMetric(new MillisPerTick()); 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 2c5ca0e..b46d6ec 100644 --- a/src/main/java/ru/ruscalworld/fabricexporter/MetricUpdater.java +++ b/src/main/java/ru/ruscalworld/fabricexporter/MetricUpdater.java @@ -1,5 +1,7 @@ package ru.ruscalworld.fabricexporter; +import ru.ruscalworld.fabricexporter.metrics.Metric; + import java.util.ArrayList; import java.util.List; import java.util.TimerTask; diff --git a/src/main/java/ru/ruscalworld/fabricexporter/Metric.java b/src/main/java/ru/ruscalworld/fabricexporter/metrics/Metric.java similarity index 79% rename from src/main/java/ru/ruscalworld/fabricexporter/Metric.java rename to src/main/java/ru/ruscalworld/fabricexporter/metrics/Metric.java index 4c49d57..3affa7f 100644 --- a/src/main/java/ru/ruscalworld/fabricexporter/Metric.java +++ b/src/main/java/ru/ruscalworld/fabricexporter/metrics/Metric.java @@ -1,6 +1,7 @@ -package ru.ruscalworld.fabricexporter; +package ru.ruscalworld.fabricexporter.metrics; import io.prometheus.client.Gauge; +import ru.ruscalworld.fabricexporter.FabricExporter; public abstract class Metric { private final Gauge gauge; diff --git a/src/main/java/ru/ruscalworld/fabricexporter/metrics/MillisPerTick.java b/src/main/java/ru/ruscalworld/fabricexporter/metrics/MillisPerTick.java new file mode 100644 index 0000000..d7f93fd --- /dev/null +++ b/src/main/java/ru/ruscalworld/fabricexporter/metrics/MillisPerTick.java @@ -0,0 +1,19 @@ +package ru.ruscalworld.fabricexporter.metrics; + +import me.lucko.spark.api.statistic.StatisticWindow; +import me.lucko.spark.api.statistic.misc.DoubleAverageInfo; +import me.lucko.spark.api.statistic.types.GenericStatistic; +import ru.ruscalworld.fabricexporter.FabricExporter; + +public class MillisPerTick extends SparkMetric { + public MillisPerTick() { + super("mspt", "Milliseconds per tick (MSPT)"); + } + + @Override + public double getCurrentValue(FabricExporter exporter) { + GenericStatistic mspt = this.getSpark().mspt(); + if (mspt == null) return 0; + return 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 c5c6d18..0f2abb8 100644 --- a/src/main/java/ru/ruscalworld/fabricexporter/metrics/OnlinePlayers.java +++ b/src/main/java/ru/ruscalworld/fabricexporter/metrics/OnlinePlayers.java @@ -1,7 +1,6 @@ package ru.ruscalworld.fabricexporter.metrics; import ru.ruscalworld.fabricexporter.FabricExporter; -import ru.ruscalworld.fabricexporter.Metric; public class OnlinePlayers extends Metric { public OnlinePlayers() { diff --git a/src/main/java/ru/ruscalworld/fabricexporter/metrics/SparkMetric.java b/src/main/java/ru/ruscalworld/fabricexporter/metrics/SparkMetric.java new file mode 100644 index 0000000..292ad0f --- /dev/null +++ b/src/main/java/ru/ruscalworld/fabricexporter/metrics/SparkMetric.java @@ -0,0 +1,14 @@ +package ru.ruscalworld.fabricexporter.metrics; + +import me.lucko.spark.api.Spark; +import me.lucko.spark.api.SparkProvider; + +public abstract class SparkMetric extends Metric { + public SparkMetric(String name, String help) { + super(name, help); + } + + public Spark getSpark() { + return SparkProvider.get(); + } +} diff --git a/src/main/java/ru/ruscalworld/fabricexporter/metrics/TicksPerSecond.java b/src/main/java/ru/ruscalworld/fabricexporter/metrics/TicksPerSecond.java new file mode 100644 index 0000000..792875c --- /dev/null +++ b/src/main/java/ru/ruscalworld/fabricexporter/metrics/TicksPerSecond.java @@ -0,0 +1,18 @@ +package ru.ruscalworld.fabricexporter.metrics; + +import me.lucko.spark.api.statistic.StatisticWindow; +import me.lucko.spark.api.statistic.types.DoubleStatistic; +import ru.ruscalworld.fabricexporter.FabricExporter; + +public class TicksPerSecond extends SparkMetric { + public TicksPerSecond() { + super("tps", "Current TPS on server"); + } + + @Override + public double getCurrentValue(FabricExporter exporter) { + DoubleStatistic tps = this.getSpark().tps(); + if (tps == null) return 20; + return tps.poll(StatisticWindow.TicksPerSecond.MINUTES_1); + } +}