From 0228f7205d87300b4332a0ae70dcd5b7c1a44b44 Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 18 Apr 2024 03:06:39 +0100 Subject: [PATCH] add connected sockets metric --- .../backend/service/MetricService.java | 6 ++---- .../metrics/ConnectedSocketsMetric.java | 21 +++++++++++++++++++ .../websocket/MetricsWebSocketHandler.java | 8 +++---- 3 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 src/main/java/xyz/mcutils/backend/service/metric/metrics/ConnectedSocketsMetric.java diff --git a/src/main/java/xyz/mcutils/backend/service/MetricService.java b/src/main/java/xyz/mcutils/backend/service/MetricService.java index c344672..3613f03 100644 --- a/src/main/java/xyz/mcutils/backend/service/MetricService.java +++ b/src/main/java/xyz/mcutils/backend/service/MetricService.java @@ -9,10 +9,7 @@ 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.metrics.RequestsPerRouteMetric; -import xyz.mcutils.backend.service.metric.metrics.TotalPlayerLookupsMetric; -import xyz.mcutils.backend.service.metric.metrics.TotalRequestsMetric; -import xyz.mcutils.backend.service.metric.metrics.TotalServerLookupsMetric; +import xyz.mcutils.backend.service.metric.metrics.*; import xyz.mcutils.backend.service.metric.metrics.process.CpuUsageMetric; import xyz.mcutils.backend.service.metric.metrics.process.MemoryMetric; @@ -49,6 +46,7 @@ public class MetricService { registerMetric(new CpuUsageMetric()); registerMetric(new TotalPlayerLookupsMetric()); registerMetric(new TotalServerLookupsMetric()); + registerMetric(new ConnectedSocketsMetric()); // Load the metrics from Redis loadMetrics(); diff --git a/src/main/java/xyz/mcutils/backend/service/metric/metrics/ConnectedSocketsMetric.java b/src/main/java/xyz/mcutils/backend/service/metric/metrics/ConnectedSocketsMetric.java new file mode 100644 index 0000000..59ed4e0 --- /dev/null +++ b/src/main/java/xyz/mcutils/backend/service/metric/metrics/ConnectedSocketsMetric.java @@ -0,0 +1,21 @@ +package xyz.mcutils.backend.service.metric.metrics; + +import xyz.mcutils.backend.service.metric.impl.IntegerMetric; +import xyz.mcutils.backend.websocket.MetricsWebSocketHandler; + +public class ConnectedSocketsMetric extends IntegerMetric { + + public ConnectedSocketsMetric() { + super("connected_sockets"); + } + + @Override + public boolean isCollector() { + return true; + } + + @Override + public void collect() { + setValue(MetricsWebSocketHandler.SESSIONS.size()); + } +} diff --git a/src/main/java/xyz/mcutils/backend/websocket/MetricsWebSocketHandler.java b/src/main/java/xyz/mcutils/backend/websocket/MetricsWebSocketHandler.java index b92b31b..8802972 100644 --- a/src/main/java/xyz/mcutils/backend/websocket/MetricsWebSocketHandler.java +++ b/src/main/java/xyz/mcutils/backend/websocket/MetricsWebSocketHandler.java @@ -22,14 +22,14 @@ import java.util.concurrent.TimeUnit; @Log4j2(topic = "WebSocket/Metrics") public class MetricsWebSocketHandler extends TextWebSocketHandler { private final long interval = TimeUnit.SECONDS.toMillis(5); - public final List sessions = new ArrayList<>(); + public static final List SESSIONS = new ArrayList<>(); private final MetricService metricService; public MetricsWebSocketHandler(MetricService metricService) { this.metricService = metricService; Timer.scheduleRepeating(() -> { - for (WebSocketSession session : sessions) { + for (WebSocketSession session : SESSIONS) { sendMetrics(session); } }, interval, interval); @@ -59,13 +59,13 @@ public class MetricsWebSocketHandler extends TextWebSocketHandler { log.info("WebSocket connection established with session id: {}", session.getId()); sendMetrics(session); - sessions.add(session); + SESSIONS.add(session); } @Override public void afterConnectionClosed(WebSocketSession session, @NotNull CloseStatus status) { log.info("WebSocket connection closed with session id: {}", session.getId()); - sessions.remove(session); + SESSIONS.remove(session); } } \ No newline at end of file