From 632d33197d74aba2157f72cc094203d77fbd4393 Mon Sep 17 00:00:00 2001 From: Liam Date: Wed, 17 Apr 2024 01:35:15 +0100 Subject: [PATCH] add cpu metric --- .../backend/controller/HealthController.java | 2 - .../backend/service/MetricService.java | 4 +- .../backend/service/metric/Metric.java | 1 - .../service/metric/impl/DoubleMetric.java | 49 +++++++++++++++++++ .../metrics/process/CpuUsageMetric.java | 23 +++++++++ .../metric/metrics/process/MemoryMetric.java | 1 - 6 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 src/main/java/xyz/mcutils/backend/service/metric/impl/DoubleMetric.java create mode 100644 src/main/java/xyz/mcutils/backend/service/metric/metrics/process/CpuUsageMetric.java diff --git a/src/main/java/xyz/mcutils/backend/controller/HealthController.java b/src/main/java/xyz/mcutils/backend/controller/HealthController.java index cb14fbc..eabd223 100644 --- a/src/main/java/xyz/mcutils/backend/controller/HealthController.java +++ b/src/main/java/xyz/mcutils/backend/controller/HealthController.java @@ -2,10 +2,8 @@ package xyz.mcutils.backend.controller; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; -import xyz.mcutils.backend.config.Config; import java.util.Map; diff --git a/src/main/java/xyz/mcutils/backend/service/MetricService.java b/src/main/java/xyz/mcutils/backend/service/MetricService.java index 1823520..9efdf33 100644 --- a/src/main/java/xyz/mcutils/backend/service/MetricService.java +++ b/src/main/java/xyz/mcutils/backend/service/MetricService.java @@ -9,10 +9,9 @@ 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.CpuUsageMetric; import xyz.mcutils.backend.service.metric.metrics.process.MemoryMetric; import java.util.ArrayList; @@ -45,6 +44,7 @@ public class MetricService { registerMetric(new TotalRequestsMetric()); registerMetric(new RequestsPerRouteMetric()); registerMetric(new MemoryMetric()); + registerMetric(new CpuUsageMetric()); // Load the metrics from Redis loadMetrics(); 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 9431961..9975305 100644 --- a/src/main/java/xyz/mcutils/backend/service/metric/Metric.java +++ b/src/main/java/xyz/mcutils/backend/service/metric/Metric.java @@ -1,7 +1,6 @@ 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; diff --git a/src/main/java/xyz/mcutils/backend/service/metric/impl/DoubleMetric.java b/src/main/java/xyz/mcutils/backend/service/metric/impl/DoubleMetric.java new file mode 100644 index 0000000..561147e --- /dev/null +++ b/src/main/java/xyz/mcutils/backend/service/metric/impl/DoubleMetric.java @@ -0,0 +1,49 @@ +package xyz.mcutils.backend.service.metric.impl; + +import com.influxdb.client.write.Point; +import xyz.mcutils.backend.service.metric.Metric; + +public class DoubleMetric extends Metric { + + public DoubleMetric(String id) { + super(id, 0D, false); + } + + /** + * Increment the value of this metric. + * + * @param amount the amount to increment by + */ + public void increment(double amount) { + setValue(getValue() + amount); + } + + /** + * Increment the value of this metric by 1. + */ + public void increment() { + increment(1); + } + + /** + * Decrement the value of this metric. + * + * @param amount the amount to decrement by + */ + public void decrement(double amount) { + setValue(getValue() - amount); + } + + /** + * Decrement the value of this metric by 1. + */ + public void decrement() { + decrement(1D); + } + + @Override + public Point toPoint() { + return Point.measurement(getId()) + .addField("value", getValue()); + } +} diff --git a/src/main/java/xyz/mcutils/backend/service/metric/metrics/process/CpuUsageMetric.java b/src/main/java/xyz/mcutils/backend/service/metric/metrics/process/CpuUsageMetric.java new file mode 100644 index 0000000..51d8118 --- /dev/null +++ b/src/main/java/xyz/mcutils/backend/service/metric/metrics/process/CpuUsageMetric.java @@ -0,0 +1,23 @@ +package xyz.mcutils.backend.service.metric.metrics.process; + +import com.sun.management.OperatingSystemMXBean; +import xyz.mcutils.backend.service.metric.impl.DoubleMetric; + +import java.lang.management.ManagementFactory; + +public class CpuUsageMetric extends DoubleMetric { + /** + * The OperatingSystemMXBean instance to get the CPU load from. + */ + private static final OperatingSystemMXBean OS_BEAN = ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class); + + public CpuUsageMetric() { + super("cpu_usage"); + } + + + @Override + public void collect() { + this.setValue(OS_BEAN.getProcessCpuLoad() * 100); + } +} 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 index 3cafc97..2e64aa9 100644 --- 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 @@ -1,6 +1,5 @@ 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 {