fix websocket metrics
All checks were successful
Deploy App / docker (ubuntu-latest, 2.44.0, 17, 3.8.5) (push) Successful in 2m7s

This commit is contained in:
Lee 2024-04-21 18:37:21 +01:00
parent d394c21f69
commit beda7fa230
2 changed files with 14 additions and 6 deletions

@ -40,14 +40,22 @@ public abstract class WebSocket extends TextWebSocketHandler {
session.sendMessage(new TextMessage(message)); session.sendMessage(new TextMessage(message));
} }
/**
* Called when a session connects to the WebSocket.
*
* @param session the session that connected
*/
abstract public void onSessionConnect(WebSocketSession session);
@Override @Override
public void afterConnectionEstablished(@NotNull WebSocketSession session) throws Exception { public final void afterConnectionEstablished(@NotNull WebSocketSession session) {
this.sessions.add(session); this.sessions.add(session);
log.info("Connection established: {}", session.getId()); log.info("Connection established: {}", session.getId());
this.onSessionConnect(session);
} }
@Override @Override
public void afterConnectionClosed(@NotNull WebSocketSession session, @NotNull CloseStatus status) throws Exception { public final void afterConnectionClosed(@NotNull WebSocketSession session, @NotNull CloseStatus status) {
this.sessions.remove(session); this.sessions.remove(session);
log.info("Connection closed: {}", session.getId()); log.info("Connection closed: {}", session.getId());
} }

@ -32,8 +32,8 @@ public class MetricsWebSocket extends WebSocket {
} }
@Override @Override
public void afterConnectionEstablished(@NotNull WebSocketSession session) { public void onSessionConnect(WebSocketSession session) {
sendMetrics(session); // Send metrics to the client when they connect sendMetrics(session);
} }
/** /**
@ -43,11 +43,11 @@ public class MetricsWebSocket extends WebSocket {
*/ */
private void sendMetrics(WebSocketSession session) { private void sendMetrics(WebSocketSession session) {
try { try {
session.sendMessage(new TextMessage(Main.GSON.toJson(Map.of( this.sendMessage(session, Main.GSON.toJson(Map.of(
"totalRequests", metricService.getMetric(TotalRequestsMetric.class).getValue(), "totalRequests", metricService.getMetric(TotalRequestsMetric.class).getValue(),
"uniqueServerLookups", metricService.getMetric(UniqueServerLookupsMetric.class).getValue(), "uniqueServerLookups", metricService.getMetric(UniqueServerLookupsMetric.class).getValue(),
"uniquePlayerLookups", metricService.getMetric(UniquePlayerLookupsMetric.class).getValue() "uniquePlayerLookups", metricService.getMetric(UniquePlayerLookupsMetric.class).getValue()
)))); )));
} catch (Exception e) { } catch (Exception e) {
log.error("An error occurred while sending metrics to the client", e); log.error("An error occurred while sending metrics to the client", e);
} }