Skip to content

Commit 3b53165

Browse files
committed
Expose WebSocketMessageBrokerStats contracts
Prior to this commit, the `WebSocketMessageBrokerStats` would be in charge of periodically logging WebSocket stats. This class would also publicly expose each stats type with dedicated methods, as `String`. This would not allow observation libraries to easily extract information and turn them into metrics. This commit introduces new methods exposing the `Stats` types directly and deprecates the former `String` variants. This will allow observability libraries like Micrometer to expose this as metrics: ``` MeterRegistry meterRegistry = ...; Gauge.builder("spring.stomp.frames", stats.getStompSubProtocolStats(), StompSubProtocolHandler.Stats::getTotalConnect) .tag("type", "CONNECT") .description("number of CONNECT frames processed") .register(meterRegistry); ``` Closes gh-31604
1 parent e856e7e commit 3b53165

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

spring-websocket/src/main/java/org/springframework/web/socket/config/WebSocketMessageBrokerStats.java

+41-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -52,6 +52,7 @@
5252
*
5353
* @author Rossen Stoyanchev
5454
* @author Sam Brannen
55+
* @author Brian Clozel
5556
* @since 4.1
5657
*/
5758
public class WebSocketMessageBrokerStats implements SmartInitializingSingleton {
@@ -160,25 +161,63 @@ private ScheduledFuture<?> initLoggingTask(long initialDelay) {
160161

161162
/**
162163
* Get stats about WebSocket sessions.
164+
* @deprecated as of 6.2 in favor of {@link #getWebSocketSessionStats()}.
163165
*/
166+
@Deprecated(since = "6.2", forRemoval = true)
164167
public String getWebSocketSessionStatsInfo() {
165168
return (this.webSocketHandler != null ? this.webSocketHandler.getStatsInfo() : "null");
166169
}
167170

171+
/**
172+
* Get stats about WebSocket sessions.
173+
* Can return {@code null} if no {@link #setSubProtocolWebSocketHandler(SubProtocolWebSocketHandler) WebSocket handler}
174+
* is configured.
175+
* @since 6.2
176+
*/
177+
@Nullable
178+
public SubProtocolWebSocketHandler.Stats getWebSocketSessionStats() {
179+
return (this.webSocketHandler != null ? this.webSocketHandler.getStats() : null);
180+
}
181+
168182
/**
169183
* Get stats about STOMP-related WebSocket message processing.
184+
* @deprecated as of 6.2 in favor of {@link #getStompSubProtocolStats()}.
170185
*/
186+
@Deprecated(since = "6.2", forRemoval = true)
171187
public String getStompSubProtocolStatsInfo() {
172188
return (this.stompSubProtocolHandler != null ? this.stompSubProtocolHandler.getStatsInfo() : "null");
173189
}
174190

191+
/**
192+
* Get stats about STOMP-related WebSocket message processing.
193+
* Can return {@code null} if no {@link SubProtocolHandler} was found.
194+
* @since 6.2
195+
*/
196+
@Nullable
197+
public StompSubProtocolHandler.Stats getStompSubProtocolStats() {
198+
return (this.stompSubProtocolHandler != null ? this.stompSubProtocolHandler.getStats() : null);
199+
}
200+
175201
/**
176202
* Get stats about STOMP broker relay (when using a full-featured STOMP broker).
203+
* @deprecated as of 6.2 in favor of {@link #getStompBrokerRelayStats()}.
177204
*/
205+
@Deprecated(since = "6.2", forRemoval = true)
178206
public String getStompBrokerRelayStatsInfo() {
179207
return (this.stompBrokerRelay != null ? this.stompBrokerRelay.getStatsInfo() : "null");
180208
}
181209

210+
/**
211+
* Get stats about STOMP broker relay (when using a full-featured STOMP broker).
212+
* Can return {@code null} if no {@link #setStompBrokerRelay(StompBrokerRelayMessageHandler) STOMP broker relay}
213+
* is configured.
214+
* @since 6.2
215+
*/
216+
@Nullable
217+
public StompBrokerRelayMessageHandler.Stats getStompBrokerRelayStats() {
218+
return (this.stompBrokerRelay != null ? this.stompBrokerRelay.getStats() : null);
219+
}
220+
182221
/**
183222
* Get stats about the executor processing incoming messages from WebSocket clients.
184223
*/
@@ -231,6 +270,7 @@ private String getExecutorStatsInfo(@Nullable Executor executor) {
231270
}
232271

233272
@Override
273+
@SuppressWarnings("removal")
234274
public String toString() {
235275
return "WebSocketSession[" + getWebSocketSessionStatsInfo() + "]" +
236276
", stompSubProtocol[" + getStompSubProtocolStatsInfo() + "]" +

0 commit comments

Comments
 (0)