Added NPE guard, fixes gh-916

This commit is contained in:
Marcin Grzejszczak
2018-03-28 15:02:55 +02:00
parent 36534558d9
commit 3713a027b3
2 changed files with 22 additions and 2 deletions

View File

@@ -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();
}
}

View File

@@ -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<ClientRequest, ClientResponse> handler() {
if (this.handler == null) {