add memory metric
All checks were successful
Deploy App / docker (ubuntu-latest, 2.44.0, 17, 3.8.5) (push) Successful in 1m44s
All checks were successful
Deploy App / docker (ubuntu-latest, 2.44.0, 17, 3.8.5) (push) Successful in 1m44s
This commit is contained in:
parent
be40a981fe
commit
7a50dccabd
@ -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<Point> 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());
|
||||
|
@ -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<T> {
|
||||
/**
|
||||
@ -22,6 +24,18 @@ public abstract class Metric<T> {
|
||||
*/
|
||||
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}.
|
||||
*
|
||||
|
@ -6,7 +6,7 @@ import xyz.mcutils.backend.service.metric.Metric;
|
||||
public class IntegerMetric extends Metric<Integer> {
|
||||
|
||||
public IntegerMetric(String id) {
|
||||
super(id, 0);
|
||||
super(id, 0, false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -9,11 +9,14 @@ import java.util.Map;
|
||||
public class MapMetric <A, B> extends Metric<Map<A, B>> {
|
||||
|
||||
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<A, B> entry : getValue().entrySet()) {
|
||||
switch (entry.getValue().getClass().getSimpleName()) {
|
||||
@ -23,6 +26,9 @@ public class MapMetric <A, B> extends Metric<Map<A, B>> {
|
||||
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;
|
||||
|
@ -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<String, Long> {
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user