add total player and server lookups

This commit is contained in:
Lee 2024-04-18 02:26:02 +01:00
parent 9fd84f2e5b
commit c46443425e
6 changed files with 44 additions and 4 deletions

@ -10,7 +10,9 @@ import xyz.mcutils.backend.common.Timer;
import xyz.mcutils.backend.repository.MetricsRepository; import xyz.mcutils.backend.repository.MetricsRepository;
import xyz.mcutils.backend.service.metric.Metric; import xyz.mcutils.backend.service.metric.Metric;
import xyz.mcutils.backend.service.metric.metrics.RequestsPerRouteMetric; 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.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.CpuUsageMetric;
import xyz.mcutils.backend.service.metric.metrics.process.MemoryMetric; import xyz.mcutils.backend.service.metric.metrics.process.MemoryMetric;
@ -45,6 +47,8 @@ public class MetricService {
registerMetric(new RequestsPerRouteMetric()); registerMetric(new RequestsPerRouteMetric());
registerMetric(new MemoryMetric()); registerMetric(new MemoryMetric());
registerMetric(new CpuUsageMetric()); registerMetric(new CpuUsageMetric());
registerMetric(new TotalPlayerLookupsMetric());
registerMetric(new TotalServerLookupsMetric());
// Load the metrics from Redis // Load the metrics from Redis
loadMetrics(); loadMetrics();

@ -24,6 +24,8 @@ import xyz.mcutils.backend.model.token.MojangUsernameToUuidToken;
import xyz.mcutils.backend.repository.PlayerCacheRepository; import xyz.mcutils.backend.repository.PlayerCacheRepository;
import xyz.mcutils.backend.repository.PlayerNameCacheRepository; import xyz.mcutils.backend.repository.PlayerNameCacheRepository;
import xyz.mcutils.backend.repository.PlayerSkinPartCacheRepository; 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.awt.image.BufferedImage;
import java.util.Optional; import java.util.Optional;
@ -33,14 +35,16 @@ import java.util.UUID;
public class PlayerService { public class PlayerService {
private final MojangService mojangAPIService; private final MojangService mojangAPIService;
private final MetricService metricService;
private final PlayerCacheRepository playerCacheRepository; private final PlayerCacheRepository playerCacheRepository;
private final PlayerNameCacheRepository playerNameCacheRepository; private final PlayerNameCacheRepository playerNameCacheRepository;
private final PlayerSkinPartCacheRepository playerSkinPartCacheRepository; private final PlayerSkinPartCacheRepository playerSkinPartCacheRepository;
@Autowired @Autowired
public PlayerService(MojangService mojangAPIService, PlayerCacheRepository playerCacheRepository, public PlayerService(MojangService mojangAPIService, MetricService metricService, PlayerCacheRepository playerCacheRepository,
PlayerNameCacheRepository playerNameCacheRepository, PlayerSkinPartCacheRepository playerSkinPartCacheRepository) { PlayerNameCacheRepository playerNameCacheRepository, PlayerSkinPartCacheRepository playerSkinPartCacheRepository) {
this.mojangAPIService = mojangAPIService; this.mojangAPIService = mojangAPIService;
this.metricService = metricService;
this.playerCacheRepository = playerCacheRepository; this.playerCacheRepository = playerCacheRepository;
this.playerNameCacheRepository = playerNameCacheRepository; this.playerNameCacheRepository = playerNameCacheRepository;
this.playerSkinPartCacheRepository = playerSkinPartCacheRepository; this.playerSkinPartCacheRepository = playerSkinPartCacheRepository;
@ -61,6 +65,8 @@ public class PlayerService {
uuid = usernameToUuid(id).getUniqueId(); uuid = usernameToUuid(id).getUniqueId();
} }
((TotalPlayerLookupsMetric) metricService.getMetric(TotalPlayerLookupsMetric.class)).increment(); // Increment the total player lookups
Optional<CachedPlayer> cachedPlayer = playerCacheRepository.findById(uuid); Optional<CachedPlayer> cachedPlayer = playerCacheRepository.findById(uuid);
if (cachedPlayer.isPresent() && Config.INSTANCE.isProduction()) { // Return the cached player if it exists if (cachedPlayer.isPresent() && Config.INSTANCE.isProduction()) { // Return the cached player if it exists
log.info("Player {} is cached", id); log.info("Player {} is cached", id);

@ -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.JavaMinecraftServer;
import xyz.mcutils.backend.model.server.MinecraftServer; import xyz.mcutils.backend.model.server.MinecraftServer;
import xyz.mcutils.backend.repository.MinecraftServerCacheRepository; 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.net.InetSocketAddress;
import java.util.ArrayList; 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+Zs7gIvpLYuq46wr3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1vPe5Uf68lIDVjV8JZpPfjxbc/QuyRKdAQJaAdIA4tCTht+kQJ1I4nbdjfHxgpTSLyI19pb/iuK7+9YJaZCxEIKj79YZ6uDU8f97878teRN1FzA7OvquSrVKUgk+S6ROpJfA7GpN6RPkx4voshXgu91p7CGHeA+IY8dUUVXwT7PYw12Xsj0Lfh9X4ac9XgKW86cj8bPh8XmyDOD88FLoB+YPXp4YtyB3gBPXu98xeRI2zploVCBQAAAABJRU5ErkJggg=="; 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/wTYe3RIO0vK2VI9wQy6AxRsJpb3AAALvXb6TxvUCYSdOQo5Mh0GySkJc7rB405GUEfzbbl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kx4voshXgu91p7CGHeA+IY8dUUVXwT7PYw12Xsj0Lfh9X4ac9XgKW86cj8bPh8XmyDOD88FLoB+YPXp4YtyB3gBPXu98xeRI2zploVCBQAAAABJRU5ErkJggg==";
private final MojangService mojangService; private final MojangService mojangService;
private final MetricService metricService;
private final MinecraftServerCacheRepository serverCacheRepository; private final MinecraftServerCacheRepository serverCacheRepository;
@Autowired @Autowired
public ServerService(MojangService mojangService, MinecraftServerCacheRepository serverCacheRepository) { public ServerService(MojangService mojangService, MetricService metricService, MinecraftServerCacheRepository serverCacheRepository) {
this.mojangService = mojangService; this.mojangService = mojangService;
this.metricService = metricService;
this.serverCacheRepository = serverCacheRepository; this.serverCacheRepository = serverCacheRepository;
} }
@ -62,6 +66,8 @@ public class ServerService {
String key = "%s-%s:%s".formatted(platformName, hostname, port); String key = "%s-%s:%s".formatted(platformName, hostname, port);
log.info("Getting server: {}:{}", 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 // Check if the server is cached
Optional<CachedMinecraftServer> cached = serverCacheRepository.findById(key); Optional<CachedMinecraftServer> cached = serverCacheRepository.findById(key);
if (cached.isPresent() && Config.INSTANCE.isProduction()) { if (cached.isPresent() && Config.INSTANCE.isProduction()) {

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

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

@ -10,7 +10,9 @@ import xyz.mcutils.backend.Main;
import xyz.mcutils.backend.common.Timer; import xyz.mcutils.backend.common.Timer;
import xyz.mcutils.backend.model.metric.WebsocketMetrics; import xyz.mcutils.backend.model.metric.WebsocketMetrics;
import xyz.mcutils.backend.service.MetricService; 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.TotalRequestsMetric;
import xyz.mcutils.backend.service.metric.metrics.TotalServerLookupsMetric;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -41,8 +43,10 @@ public class MetricsWebSocketHandler extends TextWebSocketHandler {
private void sendMetrics(WebSocketSession session) { private void sendMetrics(WebSocketSession session) {
try { try {
WebsocketMetrics metrics = new WebsocketMetrics(Map.of( 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))); session.sendMessage(new TextMessage(Main.GSON.toJson(metrics)));
} catch (Exception e) { } catch (Exception e) {