From 595c246cce4f8e533b41d44cf82dde6fa0d05b5c Mon Sep 17 00:00:00 2001 From: samlightfoot Date: Thu, 30 Jul 2026 19:57:25 +0100 Subject: [PATCH] Skip logging operators in DefaultExchangeFunction when possible Prior to this commit, `DefaultExchangeFunction.exchange` added logging operations within `doOnRequest`/`doOnCancel` operators unconditionally, which costs two subscriber wrappers per request even though the log message construction itself is already guarded lazily. This commit gates the operators on `isDebugEnabled()`, checked per exchange so runtime log level changes are still honored. Signed-off-by: samlightfoot --- .../reactive/function/client/ExchangeFunctions.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ExchangeFunctions.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ExchangeFunctions.java index b494d2e1d7a..26fc1b876f9 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ExchangeFunctions.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ExchangeFunctions.java @@ -99,10 +99,14 @@ public abstract class ExchangeFunctions { HttpMethod httpMethod = clientRequest.method(); URI url = clientRequest.url(); - return this.connector - .connect(httpMethod, url, httpRequest -> clientRequest.writeTo(httpRequest, this.strategies)) - .doOnRequest(n -> logRequest(clientRequest)) - .doOnCancel(() -> logger.debug(clientRequest.logPrefix() + "Cancel signal (to close connection)")) + Mono responseMono = this.connector + .connect(httpMethod, url, httpRequest -> clientRequest.writeTo(httpRequest, this.strategies)); + if (logger.isDebugEnabled()) { + responseMono = responseMono + .doOnRequest(n -> logRequest(clientRequest)) + .doOnCancel(() -> logger.debug(clientRequest.logPrefix() + "Cancel signal (to close connection)")); + } + return responseMono .onErrorResume(WebClientUtils.WRAP_EXCEPTION_PREDICATE, t -> wrapException(t, clientRequest)) .map(httpResponse -> { String logPrefix = getLogPrefix(clientRequest, httpResponse);