5

For my dashboarding application I want to monitor my Spring Boot based application with internal Spring metrics. Therefor I am using the spring-boot-actuator dependency which is exposing a lot of internal metrics. There are a lot of HTTP based metrics and Tomcat metrics like current sessions, amount of HTTP calls of status X and so on. I coulnd't find any information regarding my Websocket connections.

Is there any build-in out of the box metrics exposing tool for the current Websockets with Spring or do I have to create my own metrics (e.g. for showing the data in Grafana with Prometheus) and have to manually register for example a counter which is updated when I receive a SessionConnectEvent or SessionConnectEvent?

Thanks in advance!

rieckpil
  • 10,470
  • 3
  • 32
  • 56

3 Answers3

9

If you are using STOMP over WebSocket, Spring aggregates information about internal state and counters from key infrastructure components of the setup in WebSocketMessageBrokerStats, by default this is logged every 30 seconds. It is created as a Spring bean and can be easily autowired. This is an example of the information you'll get: WebSocketMessageBrokerStats output

There are no actuator endpoints currently, but I've created them as part of my Spring WebSocket Chat sample:

Sergi Almar
  • 8,054
  • 3
  • 32
  • 30
5

Found very easy way.

If you are using STOMP over WebSocket, in your controller class just autowire WebSocketMessageBrokerStats class and use it's methods.

@Autowired
WebSocketMessageBrokerStats webSocketMessageBrokerStats;

System.out.println("Session Stats info " +webSocketMessageBrokerStats.getWebSocketSessionStatsInfo());

No need of creating any separate class.

Read https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#websocket-stomp-stats for more information.

Prakash N D
  • 143
  • 2
  • 5
3

Also, there is one more way. If you want to get more details about active sessions then it contains in SubProtocolWebSocketHandler. This bean handles active session and has a method getStats. Stats object has methods:

        int getTotalSessions();

        int getWebSocketSessions();

        int getHttpStreamingSessions();

        int getHttpPollingSessions();

        int getLimitExceededSessions();

        int getNoMessagesReceivedSessions();

        int getTransportErrorSessions();

Actually WebSocketMessageBrokerStats uses method #SubProtocolWebSocketHandler.getStatsInfo which invokes toSting on Stats object.

So, possible code will look like:

  @Autowired
  private WebSocketHandler webSocketHandler;

  @GetMapping("/total")
  public int getTotalSessions() {
    return ((SubProtocolWebSocketHandler) webSocketHandler).getStats().getTotalSessions();
  }