add connected sockets metric
All checks were successful
Deploy App / docker (ubuntu-latest, 2.44.0, 17, 3.8.5) (push) Successful in 2m13s

This commit is contained in:
Lee 2024-04-18 03:06:39 +01:00
parent c46443425e
commit 0228f7205d
3 changed files with 27 additions and 8 deletions

@ -9,10 +9,7 @@ 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.metrics.RequestsPerRouteMetric; import xyz.mcutils.backend.service.metric.metrics.*;
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.process.CpuUsageMetric; import xyz.mcutils.backend.service.metric.metrics.process.CpuUsageMetric;
import xyz.mcutils.backend.service.metric.metrics.process.MemoryMetric; import xyz.mcutils.backend.service.metric.metrics.process.MemoryMetric;
@ -49,6 +46,7 @@ public class MetricService {
registerMetric(new CpuUsageMetric()); registerMetric(new CpuUsageMetric());
registerMetric(new TotalPlayerLookupsMetric()); registerMetric(new TotalPlayerLookupsMetric());
registerMetric(new TotalServerLookupsMetric()); registerMetric(new TotalServerLookupsMetric());
registerMetric(new ConnectedSocketsMetric());
// Load the metrics from Redis // Load the metrics from Redis
loadMetrics(); loadMetrics();

@ -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());
}
}

@ -22,14 +22,14 @@ 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 static final List<WebSocketSession> SESSIONS = new ArrayList<>();
private final MetricService metricService; private final MetricService metricService;
public MetricsWebSocketHandler(MetricService metricService) { public MetricsWebSocketHandler(MetricService metricService) {
this.metricService = metricService; this.metricService = metricService;
Timer.scheduleRepeating(() -> { Timer.scheduleRepeating(() -> {
for (WebSocketSession session : sessions) { for (WebSocketSession session : SESSIONS) {
sendMetrics(session); sendMetrics(session);
} }
}, interval, interval); }, interval, interval);
@ -59,13 +59,13 @@ public class MetricsWebSocketHandler extends TextWebSocketHandler {
log.info("WebSocket connection established with session id: {}", session.getId()); log.info("WebSocket connection established with session id: {}", session.getId());
sendMetrics(session); sendMetrics(session);
sessions.add(session); SESSIONS.add(session);
} }
@Override @Override
public void afterConnectionClosed(WebSocketSession session, @NotNull CloseStatus status) { public void afterConnectionClosed(WebSocketSession session, @NotNull CloseStatus status) {
log.info("WebSocket connection closed with session id: {}", session.getId()); log.info("WebSocket connection closed with session id: {}", session.getId());
sessions.remove(session); SESSIONS.remove(session);
} }
} }