问题
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!
回答1:
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:
There are no actuator endpoints currently, but I've created them as part of my Spring WebSocket Chat sample:
- Trace Endpoint: exposes WebSocket traces
- Mappings Endpoint: exposes WebSocket message mappings
- Stats Endpoint: exposes WebSocketMessageBrokerStats
回答2:
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.
来源:https://stackoverflow.com/questions/49687540/exposing-metrics-about-current-websocket-connections-with-spring