fix: snapshot entity metrics on main thread
Build / build (push) Successful in 3m0s

This commit is contained in:
dmg
2026-01-22 23:47:55 -05:00
parent 7f48256f7d
commit 2648897197
4 changed files with 56 additions and 26 deletions
@@ -17,7 +17,9 @@ import java.net.InetSocketAddress;
import java.util.Locale;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask;
public final class PrometheusSpigotPlugin extends JavaPlugin {
private HttpServer httpServer;
@@ -25,6 +27,7 @@ public final class PrometheusSpigotPlugin extends JavaPlugin {
private MetricsRegistry metricsRegistry;
private String bearerToken;
private boolean countBlockMovementOnly;
private BukkitTask snapshotTask;
@Override
public void onEnable() {
@@ -43,6 +46,8 @@ public final class PrometheusSpigotPlugin extends JavaPlugin {
metricsRegistry = new MetricsRegistry(getServer(), enableJvm, enableProcess);
scheduleSnapshotTask();
registerListeners();
startHttpServer();
}
@@ -57,6 +62,10 @@ public final class PrometheusSpigotPlugin extends JavaPlugin {
httpExecutor.shutdownNow();
httpExecutor = null;
}
if (snapshotTask != null) {
snapshotTask.cancel();
snapshotTask = null;
}
}
private void registerListeners() {
@@ -95,4 +104,12 @@ public final class PrometheusSpigotPlugin extends JavaPlugin {
httpServer.start();
getLogger().info(String.format(Locale.ROOT, "Prometheus endpoint listening on http://%s:%d%s", bindAddress, port, path));
}
private void scheduleSnapshotTask() {
int intervalSeconds = getConfig().getInt("metrics.entity_snapshot_interval_seconds", 15);
long intervalTicks = Math.max(20L, intervalSeconds * 20L);
metricsRegistry.refreshEntitySnapshots();
snapshotTask = Bukkit.getScheduler().runTaskTimer(this, metricsRegistry::refreshEntitySnapshots, intervalTicks, intervalTicks);
}
}