-
Notifications
You must be signed in to change notification settings - Fork 690
Add WebSocket client metrics #4118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LivingLikeKrillin
wants to merge
13
commits into
reactor:main
Choose a base branch
from
LivingLikeKrillin:feature/websocket-client-metrics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2ff601e
Add WebSocket client metrics constants and recorder implementations
LivingLikeKrillin 05824a6
Add WebSocket client metrics channel handlers
LivingLikeKrillin dcbecaa
Add WebSocket client metrics tests
LivingLikeKrillin 159101e
Merge branch 'main' into feature/websocket-client-metrics
LivingLikeKrillin e473cd5
Address code review feedback for WebSocket client metrics
LivingLikeKrillin 80180b7
Address code review round 2 feedback for WebSocket client metrics
LivingLikeKrillin 4acb65a
Add per-message frame measurement for WebSocket client metrics
LivingLikeKrillin ea075f6
Add WebSocket client fragmented message metrics tests
LivingLikeKrillin 7dfb184
Address code review round 3 feedback for WebSocket client metrics
LivingLikeKrillin e392191
Apply suggestions from code review
violetagg 5563139
Apply suggestions from code review
violetagg a1705a9
Apply suggestion
violetagg b48178d
Add WebSocket client metrics handlers to reflect-config.json
LivingLikeKrillin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
269 changes: 269 additions & 0 deletions
269
...y-http/src/main/java/reactor/netty/http/client/AbstractWebSocketClientMetricsHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,269 @@ | ||
| /* | ||
| * Copyright (c) 2026 VMware, Inc. or its affiliates, All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package reactor.netty.http.client; | ||
|
|
||
| import io.netty.channel.Channel; | ||
| import io.netty.channel.ChannelDuplexHandler; | ||
| import io.netty.channel.ChannelHandlerContext; | ||
| import io.netty.channel.ChannelPromise; | ||
| import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame; | ||
| import io.netty.handler.codec.http.websocketx.PingWebSocketFrame; | ||
| import io.netty.handler.codec.http.websocketx.PongWebSocketFrame; | ||
| import io.netty.handler.codec.http.websocketx.WebSocketFrame; | ||
| import org.jspecify.annotations.Nullable; | ||
| import reactor.util.context.ContextView; | ||
| import reactor.util.Logger; | ||
| import reactor.util.Loggers; | ||
|
|
||
| import java.net.SocketAddress; | ||
| import java.time.Duration; | ||
|
|
||
| import static reactor.netty.ReactorNetty.format; | ||
|
|
||
| /** | ||
| * {@link ChannelDuplexHandler} for handling WebSocket {@link HttpClient} metrics. | ||
| * | ||
| * @author LivingLikeKrillin | ||
| * @since 1.3.5 | ||
| */ | ||
| abstract class AbstractWebSocketClientMetricsHandler extends ChannelDuplexHandler { | ||
|
|
||
| private static final Logger log = Loggers.getLogger(AbstractWebSocketClientMetricsHandler.class); | ||
|
|
||
| final String method; | ||
| final @Nullable SocketAddress proxyAddress; | ||
| final SocketAddress remoteAddress; | ||
|
|
||
| final String path; | ||
|
|
||
| final ContextView contextView; | ||
|
|
||
| long dataReceived; | ||
|
|
||
| long dataSent; | ||
|
|
||
| long dataReceivedTime; | ||
|
|
||
| long dataSentTime; | ||
|
|
||
| long connectionStartTime; | ||
|
|
||
| long handshakeStartTime; | ||
|
|
||
| protected AbstractWebSocketClientMetricsHandler(SocketAddress remoteAddress, @Nullable SocketAddress proxyAddress, | ||
| String path, ContextView contextView, String method) { | ||
| this.method = method; | ||
| this.path = path; | ||
| this.contextView = contextView; | ||
| this.proxyAddress = proxyAddress; | ||
| this.remoteAddress = remoteAddress; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isSharable() { | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public void handlerAdded(ChannelHandlerContext ctx) throws Exception { | ||
| super.handlerAdded(ctx); | ||
| connectionStartTime = System.nanoTime(); | ||
| } | ||
|
|
||
|
violetagg marked this conversation as resolved.
|
||
| @Override | ||
| public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { | ||
| try { | ||
| if (connectionStartTime > 0) { | ||
| recordConnectionClosed(); | ||
| } | ||
| } | ||
| catch (RuntimeException e) { | ||
| if (log.isWarnEnabled()) { | ||
| log.warn(format(ctx.channel(), "Exception caught while recording metrics."), e); | ||
| } | ||
| } | ||
| super.handlerRemoved(ctx); | ||
| } | ||
|
|
||
| void startHandshake(Channel channel) { | ||
| handshakeStartTime = System.nanoTime(); | ||
| } | ||
|
|
||
| void recordHandshakeComplete(Channel channel, String status) { | ||
| Duration time = Duration.ofNanos(System.nanoTime() - handshakeStartTime); | ||
| if (proxyAddress == null) { | ||
| recorder().recordWebSocketHandshakeTime(remoteAddress, path, status, time); | ||
| } | ||
| else { | ||
| recorder().recordWebSocketHandshakeTime(remoteAddress, proxyAddress, path, status, time); | ||
| } | ||
| } | ||
|
|
||
| void recordHandshakeFailure(Channel channel) { | ||
| Duration time = Duration.ofNanos(System.nanoTime() - handshakeStartTime); | ||
| if (proxyAddress == null) { | ||
| recorder().recordWebSocketHandshakeTime(remoteAddress, path, "ERROR", time); | ||
| } | ||
| else { | ||
| recorder().recordWebSocketHandshakeTime(remoteAddress, proxyAddress, path, "ERROR", time); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| @SuppressWarnings("FutureReturnValueIgnored") | ||
| public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) { | ||
| try { | ||
| if (msg instanceof WebSocketFrame) { | ||
| WebSocketFrame frame = (WebSocketFrame) msg; | ||
| if (isDataFrame(frame)) { | ||
| if (dataSentTime == 0) { | ||
| dataSentTime = System.nanoTime(); | ||
| } | ||
| dataSent += extractProcessedDataFromBuffer(frame); | ||
|
|
||
| if (frame.isFinalFragment()) { | ||
| // VoidChannelPromise does not support addListener, unvoid to ensure the listener fires | ||
| promise = promise.unvoid(); | ||
| promise.addListener(f -> { | ||
| try { | ||
| recordWrite(remoteAddress); | ||
| dataSentTime = 0; | ||
| } | ||
| catch (RuntimeException e) { | ||
| if (log.isWarnEnabled()) { | ||
| log.warn(format(ctx.channel(), "Exception caught while recording metrics."), e); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| catch (RuntimeException e) { | ||
| if (log.isWarnEnabled()) { | ||
| log.warn(format(ctx.channel(), "Exception caught while recording metrics."), e); | ||
| } | ||
| } | ||
|
violetagg marked this conversation as resolved.
|
||
|
|
||
| //"FutureReturnValueIgnored" this is deliberate | ||
| ctx.write(msg, promise); | ||
| } | ||
|
|
||
| @Override | ||
| public void channelRead(ChannelHandlerContext ctx, Object msg) { | ||
| try { | ||
| if (msg instanceof WebSocketFrame) { | ||
| WebSocketFrame frame = (WebSocketFrame) msg; | ||
| if (isDataFrame(frame)) { | ||
| if (dataReceivedTime == 0) { | ||
| dataReceivedTime = System.nanoTime(); | ||
| } | ||
| dataReceived += extractProcessedDataFromBuffer(frame); | ||
|
|
||
| if (frame.isFinalFragment()) { | ||
| recordRead(remoteAddress); | ||
| dataReceivedTime = 0; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| catch (RuntimeException e) { | ||
| if (log.isWarnEnabled()) { | ||
| log.warn(format(ctx.channel(), "Exception caught while recording metrics."), e); | ||
| } | ||
| } | ||
|
|
||
| ctx.fireChannelRead(msg); | ||
| } | ||
|
|
||
| @Override | ||
| public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { | ||
| try { | ||
| recordException(); | ||
| } | ||
| catch (RuntimeException e) { | ||
| if (log.isWarnEnabled()) { | ||
| log.warn(format(ctx.channel(), "Exception caught while recording metrics."), e); | ||
| } | ||
| } | ||
|
|
||
| ctx.fireExceptionCaught(cause); | ||
| } | ||
|
|
||
| static boolean isDataFrame(WebSocketFrame msg) { | ||
| return !(msg instanceof CloseWebSocketFrame) && | ||
| !(msg instanceof PingWebSocketFrame) && | ||
| !(msg instanceof PongWebSocketFrame); | ||
| } | ||
|
|
||
| private static long extractProcessedDataFromBuffer(WebSocketFrame msg) { | ||
| return msg.content().readableBytes(); | ||
| } | ||
|
|
||
| protected abstract WebSocketClientMetricsRecorder recorder(); | ||
|
|
||
| protected void recordConnectionClosed() { | ||
| Duration duration = Duration.ofNanos(System.nanoTime() - connectionStartTime); | ||
| if (proxyAddress == null) { | ||
| recorder().recordWebSocketConnectionDuration(remoteAddress, path, duration); | ||
| } | ||
| else { | ||
| recorder().recordWebSocketConnectionDuration(remoteAddress, proxyAddress, path, duration); | ||
| } | ||
| } | ||
|
|
||
| protected void recordException() { | ||
| if (proxyAddress == null) { | ||
| recorder().incrementErrorsCount(remoteAddress, path); | ||
| } | ||
| else { | ||
| recorder().incrementErrorsCount(remoteAddress, proxyAddress, path); | ||
| } | ||
| } | ||
|
|
||
| protected void recordRead(SocketAddress address) { | ||
| if (proxyAddress == null) { | ||
| recorder().recordDataReceivedTime(address, path, method, "n/a", | ||
| Duration.ofNanos(System.nanoTime() - dataReceivedTime)); | ||
|
|
||
| recorder().recordDataReceived(address, path, dataReceived); | ||
| } | ||
| else { | ||
| recorder().recordDataReceivedTime(address, proxyAddress, path, method, "n/a", | ||
| Duration.ofNanos(System.nanoTime() - dataReceivedTime)); | ||
|
|
||
| recorder().recordDataReceived(address, proxyAddress, path, dataReceived); | ||
| } | ||
| dataReceived = 0; | ||
| } | ||
|
|
||
| protected void recordWrite(SocketAddress address) { | ||
| if (proxyAddress == null) { | ||
| recorder().recordDataSentTime(address, path, method, | ||
| Duration.ofNanos(System.nanoTime() - dataSentTime)); | ||
|
|
||
| recorder().recordDataSent(address, path, dataSent); | ||
| } | ||
| else { | ||
| recorder().recordDataSentTime(address, proxyAddress, path, method, | ||
| Duration.ofNanos(System.nanoTime() - dataSentTime)); | ||
|
|
||
| recorder().recordDataSent(address, proxyAddress, path, dataSent); | ||
| } | ||
| dataSent = 0; | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.