diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/net/H2FrameProxyHandler.java b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/net/H2FrameProxyHandler.java index e4167a4d4..b4db980b3 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/net/H2FrameProxyHandler.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/net/H2FrameProxyHandler.java @@ -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); diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/net/OmnibusH2StreamHandler.java b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/net/OmnibusH2StreamHandler.java index 81c4cc53e..ed55d0347 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/net/OmnibusH2StreamHandler.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/net/OmnibusH2StreamHandler.java @@ -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 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 diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/net/H2FrameProxyHandlerTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/net/H2FrameProxyHandlerTest.java index 217100053..31205e106 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/net/H2FrameProxyHandlerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/net/H2FrameProxyHandlerTest.java @@ -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);