add cpu metric
All checks were successful
Deploy App / docker (ubuntu-latest, 2.44.0, 17, 3.8.5) (push) Successful in 1m33s

This commit is contained in:
Lee 2024-04-17 01:35:15 +01:00
parent 8f758820e1
commit 632d33197d
6 changed files with 74 additions and 6 deletions

@ -2,10 +2,8 @@ package xyz.mcutils.backend.controller;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import xyz.mcutils.backend.config.Config;
import java.util.Map; import java.util.Map;

@ -9,10 +9,9 @@ import org.springframework.stereotype.Service;
import xyz.mcutils.backend.common.Timer; import xyz.mcutils.backend.common.Timer;
import xyz.mcutils.backend.repository.MetricsRepository; import xyz.mcutils.backend.repository.MetricsRepository;
import xyz.mcutils.backend.service.metric.Metric; 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.RequestsPerRouteMetric;
import xyz.mcutils.backend.service.metric.metrics.TotalRequestsMetric; 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 xyz.mcutils.backend.service.metric.metrics.process.MemoryMetric;
import java.util.ArrayList; import java.util.ArrayList;
@ -45,6 +44,7 @@ public class MetricService {
registerMetric(new TotalRequestsMetric()); registerMetric(new TotalRequestsMetric());
registerMetric(new RequestsPerRouteMetric()); registerMetric(new RequestsPerRouteMetric());
registerMetric(new MemoryMetric()); registerMetric(new MemoryMetric());
registerMetric(new CpuUsageMetric());
// Load the metrics from Redis // Load the metrics from Redis
loadMetrics(); loadMetrics();

@ -1,7 +1,6 @@
package xyz.mcutils.backend.service.metric; package xyz.mcutils.backend.service.metric;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.influxdb.annotations.Measurement;
import com.influxdb.client.write.Point; import com.influxdb.client.write.Point;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;

@ -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<Double> {
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());
}
}

@ -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);
}
}

@ -1,6 +1,5 @@
package xyz.mcutils.backend.service.metric.metrics.process; package xyz.mcutils.backend.service.metric.metrics.process;
import xyz.mcutils.backend.service.metric.impl.IntegerMetric;
import xyz.mcutils.backend.service.metric.impl.MapMetric; import xyz.mcutils.backend.service.metric.impl.MapMetric;
public class MemoryMetric extends MapMetric<String, Long> { public class MemoryMetric extends MapMetric<String, Long> {