send metrics when connecting to the websocket
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
c5758d38e0
commit
9fd84f2e5b
@ -19,29 +19,42 @@ import java.util.concurrent.TimeUnit;
|
|||||||
|
|
||||||
@Log4j2(topic = "WebSocket/Metrics")
|
@Log4j2(topic = "WebSocket/Metrics")
|
||||||
public class MetricsWebSocketHandler extends TextWebSocketHandler {
|
public class MetricsWebSocketHandler extends TextWebSocketHandler {
|
||||||
private final long interval = TimeUnit.SECONDS.toMillis(5);
|
private final long interval = TimeUnit.SECONDS.toMillis(5);
|
||||||
public final List<WebSocketSession> sessions = new ArrayList<>();
|
public final List<WebSocketSession> sessions = new ArrayList<>();
|
||||||
|
|
||||||
public MetricsWebSocketHandler(MetricService metricService) {
|
private final MetricService metricService;
|
||||||
Timer.scheduleRepeating(() -> {
|
|
||||||
try {
|
|
||||||
WebsocketMetrics metrics = new WebsocketMetrics(Map.of(
|
|
||||||
"totalRequests", metricService.getMetric(TotalRequestsMetric.class).getValue())
|
|
||||||
);
|
|
||||||
|
|
||||||
for (WebSocketSession session : sessions) {
|
public MetricsWebSocketHandler(MetricService metricService) {
|
||||||
session.sendMessage(new TextMessage(Main.GSON.toJson(metrics)));
|
this.metricService = metricService;
|
||||||
}
|
Timer.scheduleRepeating(() -> {
|
||||||
} catch (Exception e) {
|
for (WebSocketSession session : sessions) {
|
||||||
log.error("An error occurred while sending metrics to the client", e);
|
sendMetrics(session);
|
||||||
}
|
}
|
||||||
}, interval, interval);
|
}, 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
|
@Override
|
||||||
public void afterConnectionEstablished(WebSocketSession session) {
|
public void afterConnectionEstablished(WebSocketSession session) {
|
||||||
log.info("WebSocket connection established with session id: {}", session.getId());
|
log.info("WebSocket connection established with session id: {}", session.getId());
|
||||||
|
|
||||||
|
sendMetrics(session);
|
||||||
sessions.add(session);
|
sessions.add(session);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user