diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceWebMvcConfigurer.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceWebMvcConfigurer.java index d55993e6c..75a8b4ca2 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceWebMvcConfigurer.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceWebMvcConfigurer.java @@ -19,6 +19,7 @@ package org.springframework.cloud.sleuth.instrument.web; import brave.spring.webmvc.SpanCustomizingAsyncHandlerInterceptor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; @@ -40,4 +41,9 @@ class TraceWebMvcConfigurer implements WebMvcConfigurer { public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(this.applicationContext.getBean(SpanCustomizingAsyncHandlerInterceptor.class)); } + + @Bean + SpanCustomizingAsyncHandlerInterceptor spanCustomizingAsyncHandlerInterceptor() { + return new SpanCustomizingAsyncHandlerInterceptor(); + } } diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/client/TraceWebClientBeanPostProcessor.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/client/TraceWebClientBeanPostProcessor.java index 084747661..6a696421a 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/client/TraceWebClientBeanPostProcessor.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/client/TraceWebClientBeanPostProcessor.java @@ -146,6 +146,15 @@ class TraceExchangeFilterFunction implements ExchangeFilterFunction { return continuation.doAfterSuccessOrError( (clientResponse, throwable1) -> { Throwable throwable = throwable1; + if (clientResponse == null || clientResponse.statusCode() == null) { + if (log.isDebugEnabled()) { + log.debug( + "No response was returned. Will close the span [" + + clientSpan + "]"); + } + handleReceive(clientSpan, ws, clientResponse, throwable); + return; + } boolean error = clientResponse.statusCode().is4xxClientError() || clientResponse.statusCode().is5xxServerError(); if (error) { @@ -159,8 +168,7 @@ class TraceExchangeFilterFunction implements ExchangeFilterFunction { .value() + "] and the reason is [" + clientResponse .statusCode().getReasonPhrase() + "]"); } - handler().handleReceive(clientResponse, throwable, clientSpan); - ws.close(); + handleReceive(clientSpan, ws, clientResponse, throwable); }); }) .subscriberContext(c -> { @@ -184,6 +192,12 @@ class TraceExchangeFilterFunction implements ExchangeFilterFunction { return exchange; } + private void handleReceive(Span clientSpan, Tracer.SpanInScope ws, + ClientResponse clientResponse, Throwable throwable) { + handler().handleReceive(clientResponse, throwable, clientSpan); + ws.close(); + } + @SuppressWarnings("unchecked") HttpClientHandler handler() { if (this.handler == null) {