Video in progress

What do you monitor in a WebSocket-based service, and where do you get those metrics in Spring?

The number of open connections, connect/disconnect rate and reasons, message counts, thread pool queues, send latency and slow clients. Spring has WebSocketMessageBrokerStats, and you add the rest yourself with Micrometer.

📖 Answer

In an HTTP service, RPS and p99 latency say a lot. In WebSocket they aren't enough: a connection lives for hours and the problem shows up somewhere else.

The main metrics I watch:

  • open connections (gauge), per node. Uneven distribution shows up here;
  • connect and disconnect rate. If disconnects spike, it's a deploy, network or memory problem;
  • disconnect reasons by close code. If the share of 1006 goes up, it's the proxy or the network; if it's 1009, someone is sending large messages;
  • message rate, incoming and outgoing;
  • the inbound/outbound pool queue: if it keeps growing, the handler is slow;
  • end-to-end latency: put the creation time into the payload and compute it when the client sends an ack;
  • disconnects caused by slow clients.

In Spring, with @EnableWebSocketMessageBroker the WebSocketMessageBrokerStats bean is there automatically. By default it writes to the INFO log every 30 minutes: the number of sessions, STOMP frames, executor states. You can change the period, but a log is no substitute for a metric.

A simple gauge with Micrometer:

@Component
class WsMetrics {
    WsMetrics(MeterRegistry reg, SimpUserRegistry users) {
        Gauge.builder("ws.users.online", users, SimpUserRegistry::getUserCount)
             .register(reg);
    }
}

For disconnect reasons I listen to SessionDisconnectEvent and increment a counter tagged by closeStatus.

For alerts, the rate of change is more useful than an absolute number: "connections dropped 30% in 5 minutes" is almost always a real incident.