From 9fd84f2e5b7fcde28c26a0c28770468a1e63c513 Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 18 Apr 2024 01:04:25 +0100 Subject: [PATCH] send metrics when connecting to the websocket --- .../websocket/MetricsWebSocketHandler.java | 37 +++++++++++++------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/src/main/java/xyz/mcutils/backend/websocket/MetricsWebSocketHandler.java b/src/main/java/xyz/mcutils/backend/websocket/MetricsWebSocketHandler.java index 7c3010c..4aa4e12 100644 --- a/src/main/java/xyz/mcutils/backend/websocket/MetricsWebSocketHandler.java +++ b/src/main/java/xyz/mcutils/backend/websocket/MetricsWebSocketHandler.java @@ -19,29 +19,42 @@ import java.util.concurrent.TimeUnit; @Log4j2(topic = "WebSocket/Metrics") public class MetricsWebSocketHandler extends TextWebSocketHandler { - private final long interval = TimeUnit.SECONDS.toMillis(5); + private final long interval = TimeUnit.SECONDS.toMillis(5); public final List sessions = new ArrayList<>(); - public MetricsWebSocketHandler(MetricService metricService) { - Timer.scheduleRepeating(() -> { - try { - WebsocketMetrics metrics = new WebsocketMetrics(Map.of( - "totalRequests", metricService.getMetric(TotalRequestsMetric.class).getValue()) - ); + private final MetricService metricService; - for (WebSocketSession session : sessions) { - session.sendMessage(new TextMessage(Main.GSON.toJson(metrics))); - } - } catch (Exception e) { - log.error("An error occurred while sending metrics to the client", e); + public MetricsWebSocketHandler(MetricService metricService) { + this.metricService = metricService; + Timer.scheduleRepeating(() -> { + for (WebSocketSession session : sessions) { + sendMetrics(session); } }, interval, interval); } + /** + * Sends the metrics to the client. + * + * @param session the session to send the metrics to + */ + private void sendMetrics(WebSocketSession session) { + try { + WebsocketMetrics metrics = new WebsocketMetrics(Map.of( + "totalRequests", metricService.getMetric(TotalRequestsMetric.class).getValue()) + ); + + session.sendMessage(new TextMessage(Main.GSON.toJson(metrics))); + } catch (Exception e) { + log.error("An error occurred while sending metrics to the client", e); + } + } + @Override public void afterConnectionEstablished(WebSocketSession session) { log.info("WebSocket connection established with session id: {}", session.getId()); + sendMetrics(session); sessions.add(session); }