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.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.MemoryMetric;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -41,6 +44,7 @@ public class MetricService {
|
|||||||
// Register the metrics
|
// Register the metrics
|
||||||
registerMetric(new TotalRequestsMetric());
|
registerMetric(new TotalRequestsMetric());
|
||||||
registerMetric(new RequestsPerRouteMetric());
|
registerMetric(new RequestsPerRouteMetric());
|
||||||
|
registerMetric(new MemoryMetric());
|
||||||
|
|
||||||
// Load the metrics from Redis
|
// Load the metrics from Redis
|
||||||
loadMetrics();
|
loadMetrics();
|
||||||
@ -82,8 +86,8 @@ public class MetricService {
|
|||||||
*/
|
*/
|
||||||
public void loadMetrics() {
|
public void loadMetrics() {
|
||||||
log.info("Loading metrics");
|
log.info("Loading metrics");
|
||||||
for (Metric<?> metric : metricsRepository.findAll()) {
|
for (Metric<?> metricToLoad : metricsRepository.findAll()) {
|
||||||
metrics.put(metric.getClass(), metric);
|
|
||||||
}
|
}
|
||||||
log.info("Loaded {} metrics", metrics.size());
|
log.info("Loaded {} metrics", metrics.size());
|
||||||
}
|
}
|
||||||
@ -113,7 +117,13 @@ public class MetricService {
|
|||||||
private void writeToInflux() {
|
private void writeToInflux() {
|
||||||
List<Point> points = new ArrayList<>();
|
List<Point> points = new ArrayList<>();
|
||||||
for (Metric<?> metric : metrics.values()) {
|
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);
|
influxWriteApi.writePoints(points);
|
||||||
log.info("Wrote {} metrics to Influx", metrics.size());
|
log.info("Wrote {} metrics to Influx", metrics.size());
|
||||||
|
@ -1,15 +1,17 @@
|
|||||||
package xyz.mcutils.backend.service.metric;
|
package xyz.mcutils.backend.service.metric;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import com.influxdb.annotations.Measurement;
|
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;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
import lombok.ToString;
|
||||||
import org.springframework.data.annotation.Id;
|
import org.springframework.data.annotation.Id;
|
||||||
import org.springframework.data.redis.core.RedisHash;
|
import org.springframework.data.redis.core.RedisHash;
|
||||||
|
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@Getter @Setter
|
@Getter @Setter @ToString
|
||||||
@RedisHash(value = "metric")
|
@RedisHash(value = "metric")
|
||||||
public abstract class Metric<T> {
|
public abstract class Metric<T> {
|
||||||
/**
|
/**
|
||||||
@ -22,6 +24,18 @@ public abstract class Metric<T> {
|
|||||||
*/
|
*/
|
||||||
private T value;
|
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}.
|
* 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 class IntegerMetric extends Metric<Integer> {
|
||||||
|
|
||||||
public IntegerMetric(String id) {
|
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 class MapMetric <A, B> extends Metric<Map<A, B>> {
|
||||||
|
|
||||||
public MapMetric(String id) {
|
public MapMetric(String id) {
|
||||||
super(id, new HashMap<>());
|
super(id, new HashMap<>(), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Point toPoint() {
|
public Point toPoint() {
|
||||||
|
if (getValue().isEmpty()) { // The map is empty
|
||||||
|
return null;
|
||||||
|
}
|
||||||
Point point = Point.measurement(getId());
|
Point point = Point.measurement(getId());
|
||||||
for (Map.Entry<A, B> entry : getValue().entrySet()) {
|
for (Map.Entry<A, B> entry : getValue().entrySet()) {
|
||||||
switch (entry.getValue().getClass().getSimpleName()) {
|
switch (entry.getValue().getClass().getSimpleName()) {
|
||||||
@ -23,6 +26,9 @@ public class MapMetric <A, B> extends Metric<Map<A, B>> {
|
|||||||
case "Double":
|
case "Double":
|
||||||
point.addField(entry.getKey().toString(), (double) entry.getValue());
|
point.addField(entry.getKey().toString(), (double) entry.getValue());
|
||||||
break;
|
break;
|
||||||
|
case "Long":
|
||||||
|
point.addField(entry.getKey().toString(), (long) entry.getValue());
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
point.addField(entry.getKey().toString(), entry.getValue().toString());
|
point.addField(entry.getKey().toString(), entry.getValue().toString());
|
||||||
break;
|
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