Add backend tag to H2FrameProxyHandler writability changed metric

This commit is contained in:
Chris Eager authored and Chris Eager committed 2026-09-16 10:05:55 -04:00
1 parent 02b86d67ad
commit 11b6894dae
3 files changed
+14 -8

No files matched your search

@@ -20,9 +20,11 @@ public class H2FrameProxyHandler extends ChannelInboundHandlerAdapter {
private static final Logger logger = LoggerFactory.getLogger(H2FrameProxyHandler.class);
private static final String WRITABILITY_CHANGED_COUNTER_NAME = MetricsUtil.name(H2FrameProxyHandler.class, "writabilityChanged");
private static final String BACKEND_TAG_NAME = "backend";
private final Channel peerStream;
private final String proxyNameTag;
private final String backendNameTag;
// If we fail to write to the peerStream, we want to close the inbound channel. Rather than allocate a new listener
// that captures the inbound ChannelHandlerContext on every message, we capture the ChannelHandlerContext in
@@ -30,9 +32,10 @@ public class H2FrameProxyHandler extends ChannelInboundHandlerAdapter {
// one channel, but we already have a designated peerStream so this handler is fundamentally single-channel.
private ChannelFutureListener closeInboundOnPeerFailure = null;
public H2FrameProxyHandler(final Channel peerStream, final String proxyNameTag) {
public H2FrameProxyHandler(final Channel peerStream, final String proxyNameTag, final String backendNameTag) {
this.peerStream = peerStream;
this.proxyNameTag = proxyNameTag;
this.backendNameTag = backendNameTag;
}
@Override
@@ -50,8 +53,9 @@ public class H2FrameProxyHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelWritabilityChanged(final ChannelHandlerContext peerCtx) throws Exception {
Metrics.counter(WRITABILITY_CHANGED_COUNTER_NAME,
"isWritable", Boolean.toString(peerCtx.channel().isWritable()),
"proxy", proxyNameTag)
"isWritable", Boolean.toString(peerCtx.channel().isWritable()),
"proxy", proxyNameTag,
BACKEND_TAG_NAME, backendNameTag)
.increment();
ctx.channel().config().setAutoRead(peerStream.isWritable());
super.channelWritabilityChanged(peerCtx);
@@ -145,7 +145,7 @@ public class OmnibusH2StreamHandler extends ChannelInboundHandlerAdapter {
// Connected, open a new H2 stream to the backend so we can proxy the client's frames
logger.trace("Opening a HTTP/2 stream to the backend {}", target);
final Channel backendConnection = connectFuture.channel();
createBackendProxyStream(ctx, backendConnection, headersFrame);
createBackendProxyStream(ctx, backendConnection, headersFrame, backendTag);
});
}
@@ -155,13 +155,15 @@ public class OmnibusH2StreamHandler extends ChannelInboundHandlerAdapter {
/// @param backendConnection An established H2 connection [Channel], on which a new h2 stream will be opened
/// @param headersFrame The first `headersFrame` from the client h2 stream that should be forwarded to the new
/// backend stream
/// @param backendTag The backend this stream targets, for metrics
private void createBackendProxyStream(
final ChannelHandlerContext clientStreamCtx,
final Channel backendConnection,
final Http2HeadersFrame headersFrame) {
final Http2HeadersFrame headersFrame,
final String backendTag) {
new Http2StreamChannelBootstrap(backendConnection)
// Forwards response frames from the backend back to the client stream
.handler(new H2FrameProxyHandler(clientStreamCtx.channel(), "responseStream"))
.handler(new H2FrameProxyHandler(clientStreamCtx.channel(), "responseStream", backendTag))
.open()
.addListener((io.netty.util.concurrent.Future<Http2StreamChannel> streamFuture) -> {
if (!streamFuture.isSuccess()) {
@@ -194,7 +196,7 @@ public class OmnibusH2StreamHandler extends ChannelInboundHandlerAdapter {
clientStreamCtx.pipeline().replace(
OmnibusH2StreamHandler.this,
"backend-to-client-proxy",
new H2FrameProxyHandler(backendStream, "requestStream"));
new H2FrameProxyHandler(backendStream, "requestStream", backendTag));
clientStreamCtx.channel().pipeline().fireChannelRead(headersFrame);
// Resume inbound reads, which should now be forwarded
@@ -20,7 +20,7 @@ class H2FrameProxyHandlerTest {
@Test
void proxyWritabilityChanged() {
final EmbeddedChannel target = new EmbeddedChannel();
final EmbeddedChannel source = new EmbeddedChannel(new H2FrameProxyHandler(target, "test"));
final EmbeddedChannel source = new EmbeddedChannel(new H2FrameProxyHandler(target, "test", "test-backend"));
// Set a tiny watermark to guarantee an unflushed write sets to unwritable, and then buffer some data
final byte[] bufferedData = "8 bytes!".getBytes(StandardCharsets.UTF_8);