From 9662c6fb643180c1b4bd93e2d300c2d1928ffdf2 Mon Sep 17 00:00:00 2001 From: Violeta Georgieva Date: Thu, 6 Oct 2022 10:28:45 +0300 Subject: [PATCH] Check for log level (#2528) --- .../AbstractChannelMetricsHandler.java | 20 +++++++++---- .../resources/PooledConnectionProvider.java | 12 +++++--- .../AddressResolverGroupMetrics.java | 4 ++- .../netty/transport/ServerTransport.java | 6 ++-- .../netty/transport/TransportConfig.java | 4 ++- .../netty/transport/TransportConnector.java | 10 +++++-- .../reactor/netty/http/HttpOperations.java | 12 +++++--- .../AbstractHttpClientMetricsHandler.java | 16 ++++++++--- .../AbstractHttpServerMetricsHandler.java | 28 ++++++++++++++----- .../server/WebsocketServerOperations.java | 2 +- 10 files changed, 82 insertions(+), 32 deletions(-) diff --git a/reactor-netty-core/src/main/java/reactor/netty/channel/AbstractChannelMetricsHandler.java b/reactor-netty-core/src/main/java/reactor/netty/channel/AbstractChannelMetricsHandler.java index 13213cc432..98b8eac2eb 100644 --- a/reactor-netty-core/src/main/java/reactor/netty/channel/AbstractChannelMetricsHandler.java +++ b/reactor-netty-core/src/main/java/reactor/netty/channel/AbstractChannelMetricsHandler.java @@ -54,7 +54,9 @@ public void channelActive(ChannelHandlerContext ctx) { recorder().recordServerConnectionOpened(ctx.channel().localAddress()); } catch (RuntimeException e) { - log.warn("Exception caught while recording metrics.", e); + if (log.isWarnEnabled()) { + log.warn("Exception caught while recording metrics.", e); + } // Allow request-response exchange to continue, unaffected by metrics problem } } @@ -68,7 +70,9 @@ public void channelInactive(ChannelHandlerContext ctx) { recorder().recordServerConnectionClosed(ctx.channel().localAddress()); } catch (RuntimeException e) { - log.warn("Exception caught while recording metrics.", e); + if (log.isWarnEnabled()) { + log.warn("Exception caught while recording metrics.", e); + } // Allow request-response exchange to continue, unaffected by metrics problem } } @@ -111,7 +115,9 @@ else if (msg instanceof DatagramPacket) { } } catch (RuntimeException e) { - log.warn("Exception caught while recording metrics.", e); + if (log.isWarnEnabled()) { + log.warn("Exception caught while recording metrics.", e); + } // Allow request-response exchange to continue, unaffected by metrics problem } @@ -137,7 +143,9 @@ else if (msg instanceof DatagramPacket) { } } catch (RuntimeException e) { - log.warn("Exception caught while recording metrics.", e); + if (log.isWarnEnabled()) { + log.warn("Exception caught while recording metrics.", e); + } // Allow request-response exchange to continue, unaffected by metrics problem } @@ -151,7 +159,9 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { recordException(ctx, remoteAddress != null ? remoteAddress : ctx.channel().remoteAddress()); } catch (RuntimeException e) { - log.warn("Exception caught while recording metrics.", e); + if (log.isWarnEnabled()) { + log.warn("Exception caught while recording metrics.", e); + } // Allow request-response exchange to continue, unaffected by metrics problem } diff --git a/reactor-netty-core/src/main/java/reactor/netty/resources/PooledConnectionProvider.java b/reactor-netty-core/src/main/java/reactor/netty/resources/PooledConnectionProvider.java index fc0b697c4a..3af9b502aa 100644 --- a/reactor-netty-core/src/main/java/reactor/netty/resources/PooledConnectionProvider.java +++ b/reactor-netty-core/src/main/java/reactor/netty/resources/PooledConnectionProvider.java @@ -337,8 +337,10 @@ protected static final class PoolFactory { Double.parseDouble(System.getProperty(ReactorNetty.POOL_GET_PERMITS_SAMPLING_RATE, "0")); if (getPermitsSamplingRate > 1d) { DEFAULT_POOL_GET_PERMITS_SAMPLING_RATE = 0; - log.warn("Invalid configuration [" + ReactorNetty.POOL_GET_PERMITS_SAMPLING_RATE + "=" + getPermitsSamplingRate + - "], the value must be between 0d and 1d (percentage). SamplingAllocationStrategy in not enabled."); + if (log.isWarnEnabled()) { + log.warn("Invalid configuration [" + ReactorNetty.POOL_GET_PERMITS_SAMPLING_RATE + "=" + getPermitsSamplingRate + + "], the value must be between 0d and 1d (percentage). SamplingAllocationStrategy in not enabled."); + } } else { DEFAULT_POOL_GET_PERMITS_SAMPLING_RATE = getPermitsSamplingRate; @@ -351,8 +353,10 @@ protected static final class PoolFactory { Double.parseDouble(System.getProperty(ReactorNetty.POOL_RETURN_PERMITS_SAMPLING_RATE, "0")); if (returnPermitsSamplingRate > 1d) { DEFAULT_POOL_RETURN_PERMITS_SAMPLING_RATE = 0; - log.warn("Invalid configuration [" + ReactorNetty.POOL_RETURN_PERMITS_SAMPLING_RATE + "=" + returnPermitsSamplingRate + - "], the value must be between 0d and 1d (percentage). SamplingAllocationStrategy is enabled."); + if (log.isWarnEnabled()) { + log.warn("Invalid configuration [" + ReactorNetty.POOL_RETURN_PERMITS_SAMPLING_RATE + "=" + returnPermitsSamplingRate + + "], the value must be between 0d and 1d (percentage). SamplingAllocationStrategy is enabled."); + } } else { DEFAULT_POOL_RETURN_PERMITS_SAMPLING_RATE = returnPermitsSamplingRate; diff --git a/reactor-netty-core/src/main/java/reactor/netty/transport/AddressResolverGroupMetrics.java b/reactor-netty-core/src/main/java/reactor/netty/transport/AddressResolverGroupMetrics.java index efd1f2f09a..52456b56aa 100644 --- a/reactor-netty-core/src/main/java/reactor/netty/transport/AddressResolverGroupMetrics.java +++ b/reactor-netty-core/src/main/java/reactor/netty/transport/AddressResolverGroupMetrics.java @@ -127,7 +127,9 @@ void record(long resolveTimeStart, String status, SocketAddress remoteAddress) { status); } catch (RuntimeException e) { - log.warn("Exception caught while recording metrics.", e); + if (log.isWarnEnabled()) { + log.warn("Exception caught while recording metrics.", e); + } // Allow request-response exchange to continue, unaffected by metrics problem } } diff --git a/reactor-netty-core/src/main/java/reactor/netty/transport/ServerTransport.java b/reactor-netty-core/src/main/java/reactor/netty/transport/ServerTransport.java index ec5c2af63b..52fe15890a 100644 --- a/reactor-netty-core/src/main/java/reactor/netty/transport/ServerTransport.java +++ b/reactor-netty-core/src/main/java/reactor/netty/transport/ServerTransport.java @@ -400,7 +400,7 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { .eventLoop() .schedule(enableAutoReadTask, 1, TimeUnit.SECONDS) .addListener(future -> { - if (!future.isSuccess()) { + if (!future.isSuccess() && log.isDebugEnabled()) { log.debug(format(ctx.channel(), "Cannot enable auto-read"), future.cause()); } }); @@ -419,7 +419,9 @@ void enableAutoReadTask(Channel channel) { static void forceClose(Channel child, Throwable t) { child.unsafe().closeForcibly(); - log.warn(format(child, "Failed to register an accepted channel: {}"), child, t); + if (log.isWarnEnabled()) { + log.warn(format(child, "Failed to register an accepted channel: {}"), child, t); + } } } diff --git a/reactor-netty-core/src/main/java/reactor/netty/transport/TransportConfig.java b/reactor-netty-core/src/main/java/reactor/netty/transport/TransportConfig.java index 2416bb56a8..0e02b5bd16 100644 --- a/reactor-netty-core/src/main/java/reactor/netty/transport/TransportConfig.java +++ b/reactor-netty-core/src/main/java/reactor/netty/transport/TransportConfig.java @@ -388,7 +388,9 @@ else if (alloc instanceof UnpooledByteBufAllocator) { MicrometerEventLoopMeterRegistrar.INSTANCE.registerMetrics(channel.eventLoop()); } catch (RuntimeException e) { - log.warn("Exception caught while recording metrics.", e); + if (log.isWarnEnabled()) { + log.warn("Exception caught while recording metrics.", e); + } // Allow request-response exchange to continue, unaffected by metrics problem } } diff --git a/reactor-netty-core/src/main/java/reactor/netty/transport/TransportConnector.java b/reactor-netty-core/src/main/java/reactor/netty/transport/TransportConnector.java index 2842328971..70902b14cd 100644 --- a/reactor-netty-core/src/main/java/reactor/netty/transport/TransportConnector.java +++ b/reactor-netty-core/src/main/java/reactor/netty/transport/TransportConnector.java @@ -169,12 +169,16 @@ static void setChannelOptions(Channel channel, Map, ?> options, } try { if (!channel.config().setOption((ChannelOption) e.getKey(), e.getValue())) { - log.warn(format(channel, "Unknown channel option '{}' for channel '{}'"), e.getKey(), channel); + if (log.isWarnEnabled()) { + log.warn(format(channel, "Unknown channel option '{}' for channel '{}'"), e.getKey(), channel); + } } } catch (Throwable t) { - log.warn(format(channel, "Failed to set channel option '{}' with value '{}' for channel '{}'"), - e.getKey(), e.getValue(), channel, t); + if (log.isWarnEnabled()) { + log.warn(format(channel, "Failed to set channel option '{}' with value '{}' for channel '{}'"), + e.getKey(), e.getValue(), channel, t); + } } } } diff --git a/reactor-netty-http/src/main/java/reactor/netty/http/HttpOperations.java b/reactor-netty-http/src/main/java/reactor/netty/http/HttpOperations.java index c16c337637..d5d27bf568 100644 --- a/reactor-netty-http/src/main/java/reactor/netty/http/HttpOperations.java +++ b/reactor-netty-http/src/main/java/reactor/netty/http/HttpOperations.java @@ -120,8 +120,10 @@ public NettyOutbound send(Publisher source) { return Mono.error(e); } if (HttpUtil.getContentLength(outboundHttpMessage(), -1) == 0) { - log.debug(format(channel(), "Dropped HTTP content, " + - "since response has Content-Length: 0 {}"), toPrettyHexDump(msg)); + if (log.isDebugEnabled()) { + log.debug(format(channel(), "Dropped HTTP content, " + + "since response has Content-Length: 0 {}"), toPrettyHexDump(msg)); + } msg.release(); return FutureMono.from(channel().writeAndFlush(newFullBodyMessage(Unpooled.EMPTY_BUFFER))); } @@ -154,8 +156,10 @@ public NettyOutbound sendObject(Object message) { throw e; } if (HttpUtil.getContentLength(outboundHttpMessage(), -1) == 0) { - log.debug(format(channel(), "Dropped HTTP content, " + - "since response has Content-Length: 0 {}"), toPrettyHexDump(b)); + if (log.isDebugEnabled()) { + log.debug(format(channel(), "Dropped HTTP content, " + + "since response has Content-Length: 0 {}"), toPrettyHexDump(b)); + } b.release(); return channel().writeAndFlush(newFullBodyMessage(Unpooled.EMPTY_BUFFER)); } diff --git a/reactor-netty-http/src/main/java/reactor/netty/http/client/AbstractHttpClientMetricsHandler.java b/reactor-netty-http/src/main/java/reactor/netty/http/client/AbstractHttpClientMetricsHandler.java index 19e0b0ca94..31e475d39d 100644 --- a/reactor-netty-http/src/main/java/reactor/netty/http/client/AbstractHttpClientMetricsHandler.java +++ b/reactor-netty-http/src/main/java/reactor/netty/http/client/AbstractHttpClientMetricsHandler.java @@ -94,14 +94,18 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) recordWrite(address); } catch (RuntimeException e) { - log.warn("Exception caught while recording metrics.", e); + if (log.isWarnEnabled()) { + log.warn("Exception caught while recording metrics.", e); + } // Allow request-response exchange to continue, unaffected by metrics problem } }); } } catch (RuntimeException e) { - log.warn("Exception caught while recording metrics.", e); + if (log.isWarnEnabled()) { + log.warn("Exception caught while recording metrics.", e); + } // Allow request-response exchange to continue, unaffected by metrics problem } //"FutureReturnValueIgnored" this is deliberate @@ -125,7 +129,9 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) { } } catch (RuntimeException e) { - log.warn("Exception caught while recording metrics.", e); + if (log.isWarnEnabled()) { + log.warn("Exception caught while recording metrics.", e); + } // Allow request-response exchange to continue, unaffected by metrics problem } ctx.fireChannelRead(msg); @@ -137,7 +143,9 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { recordException(ctx); } catch (RuntimeException e) { - log.warn("Exception caught while recording metrics.", e); + if (log.isWarnEnabled()) { + log.warn("Exception caught while recording metrics.", e); + } // Allow request-response exchange to continue, unaffected by metrics problem } ctx.fireExceptionCaught(cause); diff --git a/reactor-netty-http/src/main/java/reactor/netty/http/server/AbstractHttpServerMetricsHandler.java b/reactor-netty-http/src/main/java/reactor/netty/http/server/AbstractHttpServerMetricsHandler.java index 16e04bb01b..495fa0a09c 100644 --- a/reactor-netty-http/src/main/java/reactor/netty/http/server/AbstractHttpServerMetricsHandler.java +++ b/reactor-netty-http/src/main/java/reactor/netty/http/server/AbstractHttpServerMetricsHandler.java @@ -73,7 +73,9 @@ public void channelActive(ChannelHandlerContext ctx) { recorder().recordServerConnectionOpened(ctx.channel().localAddress()); } catch (RuntimeException e) { - log.warn("Exception caught while recording metrics.", e); + if (log.isWarnEnabled()) { + log.warn("Exception caught while recording metrics.", e); + } // Allow request-response exchange to continue, unaffected by metrics problem } } @@ -87,7 +89,9 @@ public void channelInactive(ChannelHandlerContext ctx) { recorder().recordServerConnectionClosed(ctx.channel().localAddress()); } catch (RuntimeException e) { - log.warn("Exception caught while recording metrics.", e); + if (log.isWarnEnabled()) { + log.warn("Exception caught while recording metrics.", e); + } // Allow request-response exchange to continue, unaffected by metrics problem } } @@ -118,7 +122,9 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) ops.method().name(), ops.status().codeAsText().toString()); } catch (RuntimeException e) { - log.warn("Exception caught while recording metrics.", e); + if (log.isWarnEnabled()) { + log.warn("Exception caught while recording metrics.", e); + } // Allow request-response exchange to continue, unaffected by metrics problem } // ops.hostAddress() == null when request decoding failed, in this case @@ -133,7 +139,9 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) } } catch (RuntimeException e) { - log.warn("Exception caught while recording metrics.", e); + if (log.isWarnEnabled()) { + log.warn("Exception caught while recording metrics.", e); + } // Allow request-response exchange to continue, unaffected by metrics problem } } @@ -144,7 +152,9 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) } } catch (RuntimeException e) { - log.warn("Exception caught while recording metrics.", e); + if (log.isWarnEnabled()) { + log.warn("Exception caught while recording metrics.", e); + } // Allow request-response exchange to continue, unaffected by metrics problem } finally { @@ -183,7 +193,9 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) { } } catch (RuntimeException e) { - log.warn("Exception caught while recording metrics.", e); + if (log.isWarnEnabled()) { + log.warn("Exception caught while recording metrics.", e); + } // Allow request-response exchange to continue, unaffected by metrics problem } @@ -201,7 +213,9 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { } } catch (RuntimeException e) { - log.warn("Exception caught while recording metrics.", e); + if (log.isWarnEnabled()) { + log.warn("Exception caught while recording metrics.", e); + } // Allow request-response exchange to continue, unaffected by metrics problem } diff --git a/reactor-netty-http/src/main/java/reactor/netty/http/server/WebsocketServerOperations.java b/reactor-netty-http/src/main/java/reactor/netty/http/server/WebsocketServerOperations.java index a35fa2f3e8..cac8744394 100644 --- a/reactor-netty-http/src/main/java/reactor/netty/http/server/WebsocketServerOperations.java +++ b/reactor-netty-http/src/main/java/reactor/netty/http/server/WebsocketServerOperations.java @@ -123,7 +123,7 @@ final class WebsocketServerOperations extends HttpServerOperations // This change is needed after the Netty change https://github.com/netty/netty/pull/11966 channel.read(); } - else { + else if (log.isDebugEnabled()) { log.debug(format(channel, "Cannot bind WebsocketServerOperations after the handshake.")); } });