From c46443425e2c6cc6da27ec85b87ca32027cd9d0a Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 18 Apr 2024 02:26:02 +0100 Subject: [PATCH] add total player and server lookups --- .../xyz/mcutils/backend/service/MetricService.java | 4 ++++ .../xyz/mcutils/backend/service/PlayerService.java | 8 +++++++- .../xyz/mcutils/backend/service/ServerService.java | 8 +++++++- .../metric/metrics/TotalPlayerLookupsMetric.java | 10 ++++++++++ .../metric/metrics/TotalServerLookupsMetric.java | 10 ++++++++++ .../backend/websocket/MetricsWebSocketHandler.java | 8 ++++++-- 6 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 src/main/java/xyz/mcutils/backend/service/metric/metrics/TotalPlayerLookupsMetric.java create mode 100644 src/main/java/xyz/mcutils/backend/service/metric/metrics/TotalServerLookupsMetric.java diff --git a/src/main/java/xyz/mcutils/backend/service/MetricService.java b/src/main/java/xyz/mcutils/backend/service/MetricService.java index 9efdf33..c344672 100644 --- a/src/main/java/xyz/mcutils/backend/service/MetricService.java +++ b/src/main/java/xyz/mcutils/backend/service/MetricService.java @@ -10,7 +10,9 @@ import xyz.mcutils.backend.common.Timer; import xyz.mcutils.backend.repository.MetricsRepository; import xyz.mcutils.backend.service.metric.Metric; import xyz.mcutils.backend.service.metric.metrics.RequestsPerRouteMetric; +import xyz.mcutils.backend.service.metric.metrics.TotalPlayerLookupsMetric; import xyz.mcutils.backend.service.metric.metrics.TotalRequestsMetric; +import xyz.mcutils.backend.service.metric.metrics.TotalServerLookupsMetric; import xyz.mcutils.backend.service.metric.metrics.process.CpuUsageMetric; import xyz.mcutils.backend.service.metric.metrics.process.MemoryMetric; @@ -45,6 +47,8 @@ public class MetricService { registerMetric(new RequestsPerRouteMetric()); registerMetric(new MemoryMetric()); registerMetric(new CpuUsageMetric()); + registerMetric(new TotalPlayerLookupsMetric()); + registerMetric(new TotalServerLookupsMetric()); // Load the metrics from Redis loadMetrics(); diff --git a/src/main/java/xyz/mcutils/backend/service/PlayerService.java b/src/main/java/xyz/mcutils/backend/service/PlayerService.java index 7328c9f..f12304a 100644 --- a/src/main/java/xyz/mcutils/backend/service/PlayerService.java +++ b/src/main/java/xyz/mcutils/backend/service/PlayerService.java @@ -24,6 +24,8 @@ import xyz.mcutils.backend.model.token.MojangUsernameToUuidToken; import xyz.mcutils.backend.repository.PlayerCacheRepository; import xyz.mcutils.backend.repository.PlayerNameCacheRepository; import xyz.mcutils.backend.repository.PlayerSkinPartCacheRepository; +import xyz.mcutils.backend.service.metric.metrics.TotalPlayerLookupsMetric; +import xyz.mcutils.backend.service.metric.metrics.TotalServerLookupsMetric; import java.awt.image.BufferedImage; import java.util.Optional; @@ -33,14 +35,16 @@ import java.util.UUID; public class PlayerService { private final MojangService mojangAPIService; + private final MetricService metricService; private final PlayerCacheRepository playerCacheRepository; private final PlayerNameCacheRepository playerNameCacheRepository; private final PlayerSkinPartCacheRepository playerSkinPartCacheRepository; @Autowired - public PlayerService(MojangService mojangAPIService, PlayerCacheRepository playerCacheRepository, + public PlayerService(MojangService mojangAPIService, MetricService metricService, PlayerCacheRepository playerCacheRepository, PlayerNameCacheRepository playerNameCacheRepository, PlayerSkinPartCacheRepository playerSkinPartCacheRepository) { this.mojangAPIService = mojangAPIService; + this.metricService = metricService; this.playerCacheRepository = playerCacheRepository; this.playerNameCacheRepository = playerNameCacheRepository; this.playerSkinPartCacheRepository = playerSkinPartCacheRepository; @@ -61,6 +65,8 @@ public class PlayerService { uuid = usernameToUuid(id).getUniqueId(); } + ((TotalPlayerLookupsMetric) metricService.getMetric(TotalPlayerLookupsMetric.class)).increment(); // Increment the total player lookups + Optional cachedPlayer = playerCacheRepository.findById(uuid); if (cachedPlayer.isPresent() && Config.INSTANCE.isProduction()) { // Return the cached player if it exists log.info("Player {} is cached", id); diff --git a/src/main/java/xyz/mcutils/backend/service/ServerService.java b/src/main/java/xyz/mcutils/backend/service/ServerService.java index 8890dfb..768ae0d 100644 --- a/src/main/java/xyz/mcutils/backend/service/ServerService.java +++ b/src/main/java/xyz/mcutils/backend/service/ServerService.java @@ -15,6 +15,8 @@ import xyz.mcutils.backend.model.dns.impl.SRVRecord; import xyz.mcutils.backend.model.server.JavaMinecraftServer; import xyz.mcutils.backend.model.server.MinecraftServer; import xyz.mcutils.backend.repository.MinecraftServerCacheRepository; +import xyz.mcutils.backend.service.metric.Metric; +import xyz.mcutils.backend.service.metric.metrics.TotalServerLookupsMetric; import java.net.InetSocketAddress; import java.util.ArrayList; @@ -27,11 +29,13 @@ public class ServerService { private static final String DEFAULT_SERVER_ICON = "iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAASFBMVEWwsLBBQUE9PT1JSUlFRUUuLi5MTEyzs7M0NDQ5OTlVVVVQUFAmJia5ubl+fn5zc3PFxcVdXV3AwMCJiYmUlJRmZmbQ0NCjo6OL5p+6AAAFVklEQVRYw+1W67K0KAzkJnIZdRAZ3/9NtzvgXM45dX7st1VbW7XBUVDSdEISRqn/5R+T82/+nsr/XZn/SHm/3x9/ArA/IP8qwPK433d44VubZ/XT6/cJy0L792VZfnDrcRznr86d748u92X5vtaxOe228zcCy+MSMpg/5SwRopsYMv8oigCwngbQhE/rzhwAYMpxnvMvHhgy/8AgByJolzb5pPqEbvtgMBBmtvkbgxKmaaIZ5TyPum6Viue6te241N+s+W6nOlucgjEx6Nay9zZta1XVxejW+Q5ZhhkDS31lgOTegjUBor33CQilbC2GYGy9y9bN8ytevjE4a2stajHDAgAcUkoYwzO6zQi8ZflC+XO0+exiuNa3OQtIJOCk13neUjv7VO7Asu/3LwDFeg37sQtQhy4lAQH6IR9ztca0E3oI5PtDAlJ1tHGplrJ12jjrrXPWYvXsU042Bl/qUr3B9qzPSKaovpvjgglYL2F1x+Zs7gIvpLYuq46wr3H5/RJxyvM6sXOY762oU4YZ3mAz1lpc9O3Y30VJUM/iWhBIib63II/LA4COEMxcSmrH4ddl/wTYe3RIO0vK2VI9wQy6AxRsJpb3AAALvXb6TxvUCYSdOQo5Mh0GySkJc7r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JfA7GpN6RPkx4voshXgu91p7CGHeA+IY8dUUVXwT7PYw12Xsj0Lfh9X4ac9XgKW86cj8bPh8XmyDOD88FLoB+YPXp4YtyB3gBPXu98xeRI2zploVCBQAAAABJRU5ErkJggg=="; private final MojangService mojangService; + private final MetricService metricService; private final MinecraftServerCacheRepository serverCacheRepository; @Autowired - public ServerService(MojangService mojangService, MinecraftServerCacheRepository serverCacheRepository) { + public ServerService(MojangService mojangService, MetricService metricService, MinecraftServerCacheRepository serverCacheRepository) { this.mojangService = mojangService; + this.metricService = metricService; this.serverCacheRepository = serverCacheRepository; } @@ -62,6 +66,8 @@ public class ServerService { String key = "%s-%s:%s".formatted(platformName, hostname, port); log.info("Getting server: {}:{}", hostname, port); + ((TotalServerLookupsMetric) metricService.getMetric(TotalServerLookupsMetric.class)).increment(); // Increment the total server lookups + // Check if the server is cached Optional cached = serverCacheRepository.findById(key); if (cached.isPresent() && Config.INSTANCE.isProduction()) { diff --git a/src/main/java/xyz/mcutils/backend/service/metric/metrics/TotalPlayerLookupsMetric.java b/src/main/java/xyz/mcutils/backend/service/metric/metrics/TotalPlayerLookupsMetric.java new file mode 100644 index 0000000..592a191 --- /dev/null +++ b/src/main/java/xyz/mcutils/backend/service/metric/metrics/TotalPlayerLookupsMetric.java @@ -0,0 +1,10 @@ +package xyz.mcutils.backend.service.metric.metrics; + +import xyz.mcutils.backend.service.metric.impl.IntegerMetric; + +public class TotalPlayerLookupsMetric extends IntegerMetric { + + public TotalPlayerLookupsMetric() { + super("total_player_lookups"); + } +} diff --git a/src/main/java/xyz/mcutils/backend/service/metric/metrics/TotalServerLookupsMetric.java b/src/main/java/xyz/mcutils/backend/service/metric/metrics/TotalServerLookupsMetric.java new file mode 100644 index 0000000..7b6b0a7 --- /dev/null +++ b/src/main/java/xyz/mcutils/backend/service/metric/metrics/TotalServerLookupsMetric.java @@ -0,0 +1,10 @@ +package xyz.mcutils.backend.service.metric.metrics; + +import xyz.mcutils.backend.service.metric.impl.IntegerMetric; + +public class TotalServerLookupsMetric extends IntegerMetric { + + public TotalServerLookupsMetric() { + super("total_server_lookups"); + } +} diff --git a/src/main/java/xyz/mcutils/backend/websocket/MetricsWebSocketHandler.java b/src/main/java/xyz/mcutils/backend/websocket/MetricsWebSocketHandler.java index 4aa4e12..b92b31b 100644 --- a/src/main/java/xyz/mcutils/backend/websocket/MetricsWebSocketHandler.java +++ b/src/main/java/xyz/mcutils/backend/websocket/MetricsWebSocketHandler.java @@ -10,7 +10,9 @@ import xyz.mcutils.backend.Main; import xyz.mcutils.backend.common.Timer; import xyz.mcutils.backend.model.metric.WebsocketMetrics; import xyz.mcutils.backend.service.MetricService; +import xyz.mcutils.backend.service.metric.metrics.TotalPlayerLookupsMetric; import xyz.mcutils.backend.service.metric.metrics.TotalRequestsMetric; +import xyz.mcutils.backend.service.metric.metrics.TotalServerLookupsMetric; import java.util.ArrayList; import java.util.List; @@ -41,8 +43,10 @@ public class MetricsWebSocketHandler extends TextWebSocketHandler { private void sendMetrics(WebSocketSession session) { try { WebsocketMetrics metrics = new WebsocketMetrics(Map.of( - "totalRequests", metricService.getMetric(TotalRequestsMetric.class).getValue()) - ); + "totalRequests", metricService.getMetric(TotalRequestsMetric.class).getValue(), + "totalServerLookups", metricService.getMetric(TotalServerLookupsMetric.class).getValue(), + "totalPlayerLookups", metricService.getMetric(TotalPlayerLookupsMetric.class).getValue() + )); session.sendMessage(new TextMessage(Main.GSON.toJson(metrics))); } catch (Exception e) {