switch to unique player and server lookups not the total
Some checks failed
Deploy App / docker (ubuntu-latest, 2.44.0, 17, 3.8.5) (push) Failing after 26s

This commit is contained in:
Lee 2024-04-18 13:17:41 +01:00
parent b93c7f68fb
commit 4fd66dffd3
12 changed files with 94 additions and 40 deletions

View File

@ -44,9 +44,8 @@ public class MetricService {
registerMetric(new RequestsPerRouteMetric());
registerMetric(new MemoryMetric());
registerMetric(new CpuUsageMetric());
registerMetric(new TotalPlayerLookupsMetric());
registerMetric(new TotalServerLookupsMetric());
registerMetric(new ConnectedSocketsMetric());
registerMetric(new UniquePlayerLookupsMetric());
// Load the metrics from Redis
loadMetrics();
@ -88,8 +87,8 @@ public class MetricService {
*/
public void loadMetrics() {
log.info("Loading metrics");
for (Metric<?> metric : metricsRepository.findAll()) {
metrics.put(metric.getClass(), metric);
for (Metric<?> metric : metrics.values()) {
metricsRepository.findById(metric.getId()).ifPresent(loaded -> metrics.put(loaded.getClass(), loaded));
}
log.info("Loaded {} metrics", metrics.size());
}
@ -120,7 +119,7 @@ public class MetricService {
List<Point> points = new ArrayList<>();
for (Metric<?> metric : metrics.values()) {
if (metric.isCollector()) {
metric.collect();
metric.collect(this);
}
Point point = metric.toPoint();
if (point != null) {

View File

@ -24,8 +24,7 @@ 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 xyz.mcutils.backend.service.metric.metrics.UniquePlayerLookupsMetric;
import java.awt.image.BufferedImage;
import java.util.Optional;
@ -65,7 +64,7 @@ public class PlayerService {
uuid = usernameToUuid(id).getUniqueId();
}
((TotalPlayerLookupsMetric) metricService.getMetric(TotalPlayerLookupsMetric.class)).increment(); // Increment the total player lookups
((UniquePlayerLookupsMetric) metricService.getMetric(UniquePlayerLookupsMetric.class)).addLookup(id); // Add the lookup to the unique player lookups
Optional<CachedPlayer> cachedPlayer = playerCacheRepository.findById(uuid);
if (cachedPlayer.isPresent() && Config.INSTANCE.isProduction()) { // Return the cached player if it exists

View File

@ -15,8 +15,7 @@ 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 xyz.mcutils.backend.service.metric.metrics.UniqueServerLookupsMetric;
import java.net.InetSocketAddress;
import java.util.ArrayList;
@ -66,7 +65,7 @@ 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
((UniqueServerLookupsMetric) metricService.getMetric(UniqueServerLookupsMetric.class)).addLookup(key); // Add the server lookup to the unique server lookups
// Check if the server is cached
Optional<CachedMinecraftServer> cached = serverCacheRepository.findById(key);

View File

@ -8,6 +8,7 @@ import lombok.Setter;
import lombok.ToString;
import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash;
import xyz.mcutils.backend.service.MetricService;
@AllArgsConstructor
@Getter @Setter @ToString
@ -33,7 +34,7 @@ public abstract class Metric<T> {
/**
* Collects the metric.
*/
public void collect() {}
public void collect(MetricService metricService) {}
/**
* Gets this point as a {@link Point}.

View File

@ -1,5 +1,6 @@
package xyz.mcutils.backend.service.metric.metrics;
import xyz.mcutils.backend.service.MetricService;
import xyz.mcutils.backend.service.metric.impl.IntegerMetric;
import xyz.mcutils.backend.websocket.MetricsWebSocketHandler;
@ -15,7 +16,7 @@ public class ConnectedSocketsMetric extends IntegerMetric {
}
@Override
public void collect() {
public void collect(MetricService metricService) {
setValue(MetricsWebSocketHandler.SESSIONS.size());
}
}

View File

@ -1,10 +0,0 @@
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");
}
}

View File

@ -1,10 +0,0 @@
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");
}
}

View File

@ -0,0 +1,37 @@
package xyz.mcutils.backend.service.metric.metrics;
import xyz.mcutils.backend.service.MetricService;
import xyz.mcutils.backend.service.metric.impl.IntegerMetric;
import java.util.ArrayList;
import java.util.List;
public class UniquePlayerLookupsMetric extends IntegerMetric {
private List<String> uniqueLookups = new ArrayList<>();
public UniquePlayerLookupsMetric() {
super("unique_player_lookups");
}
@Override
public boolean isCollector() {
return true;
}
/**
* Adds a lookup to the list of unique lookups.
*
* @param lookup the query that was used to look up a player
*/
public void addLookup(String lookup) {
lookup = lookup.toLowerCase();
if (!uniqueLookups.contains(lookup)) {
uniqueLookups.add(lookup);
}
}
@Override
public void collect(MetricService metricService) {
setValue(uniqueLookups.size());
}
}

View File

@ -0,0 +1,37 @@
package xyz.mcutils.backend.service.metric.metrics;
import xyz.mcutils.backend.service.MetricService;
import xyz.mcutils.backend.service.metric.impl.IntegerMetric;
import java.util.ArrayList;
import java.util.List;
public class UniqueServerLookupsMetric extends IntegerMetric {
private List<String> uniqueLookups = new ArrayList<>();
public UniqueServerLookupsMetric() {
super("unique_server_lookups");
}
@Override
public boolean isCollector() {
return true;
}
/**
* Adds a lookup to the list of unique lookups.
*
* @param lookup the query that was used to look up a player
*/
public void addLookup(String lookup) {
lookup = lookup.toLowerCase();
if (!uniqueLookups.contains(lookup)) {
uniqueLookups.add(lookup);
}
}
@Override
public void collect(MetricService metricService) {
setValue(uniqueLookups.size());
}
}

View File

@ -1,6 +1,7 @@
package xyz.mcutils.backend.service.metric.metrics.process;
import com.sun.management.OperatingSystemMXBean;
import xyz.mcutils.backend.service.MetricService;
import xyz.mcutils.backend.service.metric.impl.DoubleMetric;
import java.lang.management.ManagementFactory;
@ -21,7 +22,7 @@ public class CpuUsageMetric extends DoubleMetric {
}
@Override
public void collect() {
public void collect(MetricService metricService) {
this.setValue(OS_BEAN.getProcessCpuLoad() * 100);
}
}

View File

@ -1,5 +1,6 @@
package xyz.mcutils.backend.service.metric.metrics.process;
import xyz.mcutils.backend.service.MetricService;
import xyz.mcutils.backend.service.metric.impl.MapMetric;
public class MemoryMetric extends MapMetric<String, Long> {
@ -14,7 +15,7 @@ public class MemoryMetric extends MapMetric<String, Long> {
}
@Override
public void collect() {
public void collect(MetricService metricService) {
Runtime runtime = Runtime.getRuntime();
this.getValue().put("total", runtime.maxMemory());

View File

@ -8,11 +8,10 @@ import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
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 xyz.mcutils.backend.service.metric.metrics.UniquePlayerLookupsMetric;
import xyz.mcutils.backend.service.metric.metrics.UniqueServerLookupsMetric;
import java.util.ArrayList;
import java.util.List;
@ -44,8 +43,8 @@ public class MetricsWebSocketHandler extends TextWebSocketHandler {
try {
session.sendMessage(new TextMessage(Main.GSON.toJson(Map.of(
"totalRequests", metricService.getMetric(TotalRequestsMetric.class).getValue(),
"totalServerLookups", metricService.getMetric(TotalServerLookupsMetric.class).getValue(),
"totalPlayerLookups", metricService.getMetric(TotalPlayerLookupsMetric.class).getValue()
"uniqueServerLookups", metricService.getMetric(UniqueServerLookupsMetric.class).getValue(),
"uniquePlayerLookups", metricService.getMetric(UniquePlayerLookupsMetric.class).getValue()
))));
} catch (Exception e) {
log.error("An error occurred while sending metrics to the client", e);