From ce3067ee0efe6ed9c4f35dd6c6bb678eeaaefa6f Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 18 Apr 2024 13:34:43 +0100 Subject: [PATCH] switch to mongo for metric storage --- .../xyz/mcutils/backend/service/MetricService.java | 11 +++++++++++ .../xyz/mcutils/backend/service/metric/Metric.java | 5 +++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/main/java/xyz/mcutils/backend/service/MetricService.java b/src/main/java/xyz/mcutils/backend/service/MetricService.java index b247436..c18cd51 100644 --- a/src/main/java/xyz/mcutils/backend/service/MetricService.java +++ b/src/main/java/xyz/mcutils/backend/service/MetricService.java @@ -39,6 +39,8 @@ public class MetricService { this.influxWriteApi = influxAutoConfiguration.influxDBClient().getWriteApiBlocking(); this.metricsRepository = metricsRepository; + Map, Boolean> collectorEnabled = new HashMap<>(); + // Register the metrics registerMetric(new TotalRequestsMetric()); registerMetric(new RequestsPerRouteMetric()); @@ -48,9 +50,18 @@ public class MetricService { registerMetric(new UniquePlayerLookupsMetric()); registerMetric(new UniqueServerLookupsMetric()); + // please god forgive my sins; this is the worst code I've ever written + for (Metric metric : metrics.values()) { + collectorEnabled.put(metric, metric.isCollector()); + } + // Load the metrics from Redis loadMetrics(); + for (Map.Entry, Boolean> entry : collectorEnabled.entrySet()) { + entry.getKey().setCollector(entry.getValue()); + } + Timer.scheduleRepeating(() -> { saveMetrics(); writeToInflux(); diff --git a/src/main/java/xyz/mcutils/backend/service/metric/Metric.java b/src/main/java/xyz/mcutils/backend/service/metric/Metric.java index 1b92943..1b4129f 100644 --- a/src/main/java/xyz/mcutils/backend/service/metric/Metric.java +++ b/src/main/java/xyz/mcutils/backend/service/metric/Metric.java @@ -7,6 +7,7 @@ import lombok.Getter; import lombok.Setter; import lombok.ToString; import org.springframework.data.annotation.Id; +import org.springframework.data.annotation.Transient; import org.springframework.data.mongodb.core.mapping.Document; import xyz.mcutils.backend.service.MetricService; @@ -28,8 +29,8 @@ public abstract class Metric { * Should this metric be collected * before pushing to Influx? */ - @JsonIgnore - private transient boolean collector; + @Transient @JsonIgnore + private boolean collector; /** * Collects the metric.