Since upgrading spring-websockets library to version 5.3.2 I've noticed the WebSocketMessageBrokerStats class no longer provides metrics for stompSubProtocolHandler. I think I've traced it down to different bean creation order between two library versions, in which the webSocketMessageBrokerStats bean is constructed before the stompWebSocketHandlerMapping which adds a stompSubProtocolHandler from adding a stomp endpoint.


A quick summary of the difference:

Broken since Spring Boot 2.4.1 with websocket 5.3.2 Here's the call order I'm seeing in the new version: 1. Bean constructed: WebSocketMessageBrokerConfigurationSupport#subProtocolWebSocketHandler 2. Bean constructed: WebSocketMessageBrokerConfigurationSupport#webSocketMessageBrokerStats - subProtocolWebSocketHandler bean at this stage has no protocolHandlers and no defaultProtocolHandler - WebSocketMessageBrokerStats#setSubProtocolWebSocketHandler sets stompSubProtocolHandler to null 3. Bean constructed: WebSocketMessageBrokerConfigurationSupport#stompWebSocketHandlerMapping - calls WebSocketMessageBrokerConfigurationSupport#registerStompEndpoints - registry.addEndpoint(...) adds one protocolHandler to webSocketHandler 4. When trying to access WebSocketMessageBrokerStats#getStompSubProtocolStatsInfo "null" string is returned.

Was working in Spring Boot 2.4.0 with websocket 5.3.1 Call chain for the 'working' version where webSocketMessageBrokerStats.stompSubProtocolStatsInfo doesn't return null. 1. Bean constructed: WebSocketMessageBrokerConfigurationSupport#subProtocolWebSocketHandler 2. Bean constructed: WebSocketMessageBrokerConfigurationSupport#stompWebSocketHandlerMapping - calls WebSocketMessageBrokerConfigurationSupport#registerStompEndpoints - registry.addEndpoint(...) adds one protocolHandler to webSocketHandler 3. Bean constructed: WebSocketMessageBrokerConfigurationSupport#webSocketMessageBrokerStats - subProtocolWebSocketHandler bean at this stage has one protocolHandlers and no defaultProtocolHandler 4. When trying to access WebSocketMessageBrokerStats#getStompSubProtocolStatsInfo processed CONNECT(0)-CONNECTED(0)-DISCONNECT(0) string is returned, which is expected.


I also created a demo app for the issue here https://github.com/countableSet/spring-websocket-metrics-bug-demo Just flip between spring boot versions 2.4.0 and 2.4.1 you should be able to see the issue in std.out.

Comment From: rstoyanchev

Thanks for the sample project. I am able to reproduce the issue. I'll experiment with changing WebSocketBrokerStats to an InitializingBean in order to defer its initialization.