From 39d58744416cde93dc58f3c9412f4412720afa68 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 5 Jul 2018 08:02:21 -0400 Subject: [PATCH] Use connection id in server log messages if possible Issue: SPR-16966 --- .../reactive/AbstractServerHttpRequest.java | 9 +++++++++ .../DefaultServerHttpRequestBuilder.java | 10 ++++++++++ .../reactive/ReactorServerHttpRequest.java | 6 ++++++ .../reactive/UndertowServerHttpRequest.java | 9 +++++++++ .../server/adapter/HttpWebHandlerAdapter.java | 17 +++++++++++++---- 5 files changed, 47 insertions(+), 4 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/AbstractServerHttpRequest.java b/spring-web/src/main/java/org/springframework/http/server/reactive/AbstractServerHttpRequest.java index 454900d858..76891d1e7e 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/AbstractServerHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/AbstractServerHttpRequest.java @@ -179,4 +179,13 @@ public abstract class AbstractServerHttpRequest implements ServerHttpRequest { */ public abstract T getNativeRequest(); + /** + * Return an id for the underlying connection, if available. + * @since 5.1 + */ + @Nullable + public String getConnectionId() { + return null; + } + } diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/DefaultServerHttpRequestBuilder.java b/spring-web/src/main/java/org/springframework/http/server/reactive/DefaultServerHttpRequestBuilder.java index 06c024fe50..43f8525762 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/DefaultServerHttpRequestBuilder.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/DefaultServerHttpRequestBuilder.java @@ -191,6 +191,9 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder { private final ServerHttpRequest originalRequest; + @Nullable + private final String requestId; + public MutatedServerHttpRequest(URI uri, @Nullable String contextPath, HttpHeaders headers, String methodValue, MultiValueMap cookies, @@ -203,6 +206,8 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder { this.sslInfo = sslInfo != null ? sslInfo : originalRequest.getSslInfo(); this.body = body; this.originalRequest = originalRequest; + this.requestId = originalRequest instanceof AbstractServerHttpRequest ? + ((AbstractServerHttpRequest) originalRequest).getConnectionId() : null; } @Override @@ -237,6 +242,11 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder { public T getNativeRequest() { return (T) this.originalRequest; } + + @Override + public String getConnectionId() { + return this.requestId; + } } } diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/ReactorServerHttpRequest.java b/spring-web/src/main/java/org/springframework/http/server/reactive/ReactorServerHttpRequest.java index 24df6f9b2f..a3d7431cea 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/ReactorServerHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/ReactorServerHttpRequest.java @@ -174,4 +174,10 @@ class ReactorServerHttpRequest extends AbstractServerHttpRequest { return (T) this.request; } + @Override + public String getConnectionId() { + return this.request instanceof Connection ? + ((Connection) this.request).channel().id().asShortText() : null; + } + } diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpRequest.java b/spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpRequest.java index b92f5e094d..093e13638a 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpRequest.java @@ -44,6 +44,7 @@ import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; +import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; /** @@ -59,6 +60,8 @@ class UndertowServerHttpRequest extends AbstractServerHttpRequest { private final RequestBodyPublisher body; + private final String connectionId; + public UndertowServerHttpRequest(HttpServerExchange exchange, DataBufferFactory bufferFactory) throws URISyntaxException { @@ -67,6 +70,7 @@ class UndertowServerHttpRequest extends AbstractServerHttpRequest { this.exchange = exchange; this.body = new RequestBodyPublisher(exchange, bufferFactory); this.body.registerListeners(exchange); + this.connectionId = ObjectUtils.getIdentityHexString(this.exchange.getConnection()); } private static URI initUri(HttpServerExchange exchange) throws URISyntaxException { @@ -127,6 +131,11 @@ class UndertowServerHttpRequest extends AbstractServerHttpRequest { return (T) this.exchange; } + @Override + public String getConnectionId() { + return this.connectionId; + } + private static class RequestBodyPublisher extends AbstractListenerReadPublisher { diff --git a/spring-web/src/main/java/org/springframework/web/server/adapter/HttpWebHandlerAdapter.java b/spring-web/src/main/java/org/springframework/web/server/adapter/HttpWebHandlerAdapter.java index 6ba86ad110..cfc82e302e 100644 --- a/spring-web/src/main/java/org/springframework/web/server/adapter/HttpWebHandlerAdapter.java +++ b/spring-web/src/main/java/org/springframework/web/server/adapter/HttpWebHandlerAdapter.java @@ -29,6 +29,7 @@ import org.springframework.core.NestedExceptionUtils; import org.springframework.http.HttpStatus; import org.springframework.http.codec.LoggingCodecSupport; import org.springframework.http.codec.ServerCodecConfigurer; +import org.springframework.http.server.reactive.AbstractServerHttpRequest; import org.springframework.http.server.reactive.HttpHandler; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpResponse; @@ -215,10 +216,7 @@ public class HttpWebHandlerAdapter extends WebHandlerDecorator implements HttpHa public Mono handle(ServerHttpRequest request, ServerHttpResponse response) { ServerWebExchange exchange = createExchange(request, response); - - String logId = ObjectUtils.getIdentityHexString(request); - exchange.getAttributes().put(ServerWebExchange.LOG_ID_ATTRIBUTE, logId); - + exchange.getAttributes().put(ServerWebExchange.LOG_ID_ATTRIBUTE, initLogId(request)); logExchange(exchange); return getDelegate().handle(exchange) @@ -232,6 +230,17 @@ public class HttpWebHandlerAdapter extends WebHandlerDecorator implements HttpHa getCodecConfigurer(), getLocaleContextResolver(), this.applicationContext); } + private String initLogId(ServerHttpRequest request) { + String logId = null; + if (request instanceof AbstractServerHttpRequest) { + logId = ((AbstractServerHttpRequest) request).getConnectionId(); + } + if (logId == null) { + logId = ObjectUtils.getIdentityHexString(request); + } + return logId; + } + private void logExchange(ServerWebExchange exchange) { if (logger.isDebugEnabled()) { String logPrefix = exchange.getLogPrefix();