From 7a50dccabd23871630e34369aead217a5187fbd3 Mon Sep 17 00:00:00 2001 From: Liam Date: Wed, 17 Apr 2024 00:55:41 +0100 Subject: [PATCH] add memory metric --- .../backend/service/MetricService.java | 16 ++++++++++--- .../backend/service/metric/Metric.java | 16 ++++++++++++- .../service/metric/impl/IntegerMetric.java | 2 +- .../service/metric/impl/MapMetric.java | 8 ++++++- .../metric/metrics/process/MemoryMetric.java | 24 +++++++++++++++++++ 5 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 src/main/java/xyz/mcutils/backend/service/metric/metrics/process/MemoryMetric.java diff --git a/src/main/java/xyz/mcutils/backend/service/MetricService.java b/src/main/java/xyz/mcutils/backend/service/MetricService.java index 1cf2769..6a45e48 100644 --- a/src/main/java/xyz/mcutils/backend/service/MetricService.java +++ b/src/main/java/xyz/mcutils/backend/service/MetricService.java @@ -9,8 +9,11 @@ import org.springframework.stereotype.Service; 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.impl.IntegerMetric; +import xyz.mcutils.backend.service.metric.impl.MapMetric; import xyz.mcutils.backend.service.metric.metrics.RequestsPerRouteMetric; import xyz.mcutils.backend.service.metric.metrics.TotalRequestsMetric; +import xyz.mcutils.backend.service.metric.metrics.process.MemoryMetric; import java.util.ArrayList; import java.util.HashMap; @@ -41,6 +44,7 @@ public class MetricService { // Register the metrics registerMetric(new TotalRequestsMetric()); registerMetric(new RequestsPerRouteMetric()); + registerMetric(new MemoryMetric()); // Load the metrics from Redis loadMetrics(); @@ -82,8 +86,8 @@ public class MetricService { */ public void loadMetrics() { log.info("Loading metrics"); - for (Metric metric : metricsRepository.findAll()) { - metrics.put(metric.getClass(), metric); + for (Metric metricToLoad : metricsRepository.findAll()) { + } log.info("Loaded {} metrics", metrics.size()); } @@ -113,7 +117,13 @@ public class MetricService { private void writeToInflux() { List points = new ArrayList<>(); for (Metric metric : metrics.values()) { - points.add(metric.toPoint()); + 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()); 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 37ff526..9431961 100644 --- a/src/main/java/xyz/mcutils/backend/service/metric/Metric.java +++ b/src/main/java/xyz/mcutils/backend/service/metric/Metric.java @@ -1,15 +1,17 @@ package xyz.mcutils.backend.service.metric; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.influxdb.annotations.Measurement; import com.influxdb.client.write.Point; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.Setter; +import lombok.ToString; import org.springframework.data.annotation.Id; import org.springframework.data.redis.core.RedisHash; @AllArgsConstructor -@Getter @Setter +@Getter @Setter @ToString @RedisHash(value = "metric") public abstract class Metric { /** @@ -22,6 +24,18 @@ public abstract class Metric { */ private T value; + /** + * Should this metric be collected + * before pushing to Influx? + */ + @JsonIgnore + private transient boolean collector; + + /** + * Collects the metric. + */ + public void collect() {} + /** * Gets this point as a {@link Point}. * diff --git a/src/main/java/xyz/mcutils/backend/service/metric/impl/IntegerMetric.java b/src/main/java/xyz/mcutils/backend/service/metric/impl/IntegerMetric.java index d8c3a2b..8a3793c 100644 --- a/src/main/java/xyz/mcutils/backend/service/metric/impl/IntegerMetric.java +++ b/src/main/java/xyz/mcutils/backend/service/metric/impl/IntegerMetric.java @@ -6,7 +6,7 @@ import xyz.mcutils.backend.service.metric.Metric; public class IntegerMetric extends Metric { public IntegerMetric(String id) { - super(id, 0); + super(id, 0, false); } /** diff --git a/src/main/java/xyz/mcutils/backend/service/metric/impl/MapMetric.java b/src/main/java/xyz/mcutils/backend/service/metric/impl/MapMetric.java index 66c9471..9fd768c 100644 --- a/src/main/java/xyz/mcutils/backend/service/metric/impl/MapMetric.java +++ b/src/main/java/xyz/mcutils/backend/service/metric/impl/MapMetric.java @@ -9,11 +9,14 @@ import java.util.Map; public class MapMetric extends Metric> { public MapMetric(String id) { - super(id, new HashMap<>()); + super(id, new HashMap<>(), false); } @Override public Point toPoint() { + if (getValue().isEmpty()) { // The map is empty + return null; + } Point point = Point.measurement(getId()); for (Map.Entry entry : getValue().entrySet()) { switch (entry.getValue().getClass().getSimpleName()) { @@ -23,6 +26,9 @@ public class MapMetric extends Metric> { case "Double": point.addField(entry.getKey().toString(), (double) entry.getValue()); break; + case "Long": + point.addField(entry.getKey().toString(), (long) entry.getValue()); + break; default: point.addField(entry.getKey().toString(), entry.getValue().toString()); break; diff --git a/src/main/java/xyz/mcutils/backend/service/metric/metrics/process/MemoryMetric.java b/src/main/java/xyz/mcutils/backend/service/metric/metrics/process/MemoryMetric.java new file mode 100644 index 0000000..04bb754 --- /dev/null +++ b/src/main/java/xyz/mcutils/backend/service/metric/metrics/process/MemoryMetric.java @@ -0,0 +1,24 @@ +package xyz.mcutils.backend.service.metric.metrics.process; + +import xyz.mcutils.backend.service.metric.impl.IntegerMetric; +import xyz.mcutils.backend.service.metric.impl.MapMetric; + +public class MemoryMetric extends MapMetric { + + public MemoryMetric() { + super("memory"); + } + + @Override + public boolean isCollector() { + return true; + } + + @Override + public void collect() { + Runtime runtime = Runtime.getRuntime(); + + this.getValue().put("total", runtime.totalMemory()); + this.getValue().put("used", runtime.totalMemory() - runtime.freeMemory()); + } +}