Add TPS and MSPT metrics

This commit is contained in:
RuscalWorld 2021-05-24 20:10:45 +03:00
parent 66a1cd5741
commit 2687db425a
No known key found for this signature in database
GPG key ID: 4F53776031D128ED
7 changed files with 59 additions and 2 deletions

View file

@ -8,6 +8,8 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import ru.ruscalworld.fabricexporter.config.MainConfig; import ru.ruscalworld.fabricexporter.config.MainConfig;
import ru.ruscalworld.fabricexporter.metrics.OnlinePlayers; import ru.ruscalworld.fabricexporter.metrics.OnlinePlayers;
import ru.ruscalworld.fabricexporter.metrics.TicksPerSecond;
import ru.ruscalworld.fabricexporter.metrics.MillisPerTick;
import java.io.IOException; import java.io.IOException;
import java.util.Timer; import java.util.Timer;
@ -29,6 +31,8 @@ public class FabricExporter implements ModInitializer {
MetricUpdater metricUpdater = new MetricUpdater(this); MetricUpdater metricUpdater = new MetricUpdater(this);
metricUpdater.registerMetric(new OnlinePlayers()); metricUpdater.registerMetric(new OnlinePlayers());
metricUpdater.registerMetric(new TicksPerSecond());
metricUpdater.registerMetric(new MillisPerTick());
ServerLifecycleEvents.SERVER_STARTING.register(this::setServer); ServerLifecycleEvents.SERVER_STARTING.register(this::setServer);
ServerLifecycleEvents.SERVER_STARTED.register(server -> { ServerLifecycleEvents.SERVER_STARTED.register(server -> {

View file

@ -1,5 +1,7 @@
package ru.ruscalworld.fabricexporter; package ru.ruscalworld.fabricexporter;
import ru.ruscalworld.fabricexporter.metrics.Metric;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.TimerTask; import java.util.TimerTask;

View file

@ -1,6 +1,7 @@
package ru.ruscalworld.fabricexporter; package ru.ruscalworld.fabricexporter.metrics;
import io.prometheus.client.Gauge; import io.prometheus.client.Gauge;
import ru.ruscalworld.fabricexporter.FabricExporter;
public abstract class Metric { public abstract class Metric {
private final Gauge gauge; private final Gauge gauge;

View file

@ -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<DoubleAverageInfo, StatisticWindow.MillisPerTick> mspt = this.getSpark().mspt();
if (mspt == null) return 0;
return mspt.poll(StatisticWindow.MillisPerTick.MINUTES_1).mean();
}
}

View file

@ -1,7 +1,6 @@
package ru.ruscalworld.fabricexporter.metrics; package ru.ruscalworld.fabricexporter.metrics;
import ru.ruscalworld.fabricexporter.FabricExporter; import ru.ruscalworld.fabricexporter.FabricExporter;
import ru.ruscalworld.fabricexporter.Metric;
public class OnlinePlayers extends Metric { public class OnlinePlayers extends Metric {
public OnlinePlayers() { public OnlinePlayers() {

View file

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

View file

@ -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<StatisticWindow.TicksPerSecond> tps = this.getSpark().tps();
if (tps == null) return 20;
return tps.poll(StatisticWindow.TicksPerSecond.MINUTES_1);
}
}