From 7a5b42e9d7834e12140f62295e383e17459292a0 Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 18 Apr 2024 17:38:14 +0100 Subject: [PATCH] maybe fix metrics randomly not writing to influx --- .../backend/service/MetricService.java | 30 ++++++++++++------- .../backend/service/PlayerService.java | 5 +++- .../backend/service/ServerService.java | 3 +- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/main/java/xyz/mcutils/backend/service/MetricService.java b/src/main/java/xyz/mcutils/backend/service/MetricService.java index 3ff1d24..923ae16 100644 --- a/src/main/java/xyz/mcutils/backend/service/MetricService.java +++ b/src/main/java/xyz/mcutils/backend/service/MetricService.java @@ -125,24 +125,32 @@ public class MetricService { * @param metric the metric to save */ private void saveMetric(Metric metric) { - metricsRepository.save(metric); // Save the metric to the repository + try { + metricsRepository.save(metric); // Save the metric to the repository + } catch (Exception e) { + log.error("Failed to save metric to MongoDB", e); + } } /** * Push all metrics to InfluxDB. */ private void writeToInflux() { - List points = new ArrayList<>(); - for (Metric metric : metrics.values()) { - if (metric.isCollector()) { - metric.collect(); - } - Point point = metric.toPoint(); - if (point != null) { - points.add(point); + try { + List points = new ArrayList<>(); + for (Metric metric : metrics.values()) { + if (metric.isCollector()) { + metric.collect(); + } + Point point = metric.toPoint(); + if (point != null) { + points.add(point); + } } + influxWriteApi.writePoints(points); + log.info("Wrote {} metrics to Influx", metrics.size()); + } catch (Exception e) { + log.error("Failed to write metrics to Influx", e); } - influxWriteApi.writePoints(points); - log.info("Wrote {} metrics to Influx", metrics.size()); } } diff --git a/src/main/java/xyz/mcutils/backend/service/PlayerService.java b/src/main/java/xyz/mcutils/backend/service/PlayerService.java index 19d5f6a..a6a8fca 100644 --- a/src/main/java/xyz/mcutils/backend/service/PlayerService.java +++ b/src/main/java/xyz/mcutils/backend/service/PlayerService.java @@ -83,7 +83,10 @@ public class PlayerService { mojangProfile.getProperties() // Raw properties ) ); - ((UniquePlayerLookupsMetric) metricService.getMetric(UniquePlayerLookupsMetric.class)).addLookup(uuid); // Add the lookup to the unique player lookups + + // Add the lookup to the unique player lookups metric + ((UniquePlayerLookupsMetric) metricService.getMetric(UniquePlayerLookupsMetric.class)) + .addLookup(uuid); playerCacheRepository.save(player); player.getCache().setCached(false); diff --git a/src/main/java/xyz/mcutils/backend/service/ServerService.java b/src/main/java/xyz/mcutils/backend/service/ServerService.java index 968053c..4dbe593 100644 --- a/src/main/java/xyz/mcutils/backend/service/ServerService.java +++ b/src/main/java/xyz/mcutils/backend/service/ServerService.java @@ -100,8 +100,9 @@ public class ServerService { ((JavaMinecraftServer) server.getServer()).setMojangBlocked(mojangService.isServerBlocked(hostname)); } + // Add the server lookup to the unique server lookups metric ((UniqueServerLookupsMetric) metricService.getMetric(UniqueServerLookupsMetric.class)) - .addLookup("%s-%s:%s".formatted(platformName, hostname, port)); // Add the server lookup to the unique server lookups + .addLookup("%s-%s:%s".formatted(platformName, hostname, port)); log.info("Found server: {}:{}", hostname, port); serverCacheRepository.save(server);