Fixed duplicated logs in tracefilter
This commit is contained in:
@@ -115,6 +115,16 @@ public class TraceFilter extends OncePerRequestFilter {
|
||||
if (spanFromRequest != null) {
|
||||
this.tracer.continueSpan(spanFromRequest);
|
||||
}
|
||||
// in case of a response with exception status a exception controller will close the span
|
||||
if (!httpStatusSuccessful(response) && isSpanContinued(request)) {
|
||||
// it means that the span was already detached once and we're processing an error
|
||||
try {
|
||||
filterChain.doFilter(request, response);
|
||||
} finally {
|
||||
this.tracer.close(spanFromRequest);
|
||||
}
|
||||
return;
|
||||
}
|
||||
addToResponseIfNotPresent(response, Span.SAMPLED_NAME, skip ? Span.SPAN_NOT_SAMPLED : Span.SPAN_SAMPLED);
|
||||
String name = HTTP_COMPONENT + ":" + uri;
|
||||
spanFromRequest = createSpan(request, skip, spanFromRequest, name);
|
||||
@@ -148,12 +158,8 @@ public class TraceFilter extends OncePerRequestFilter {
|
||||
} else {
|
||||
spanFromRequest.logEvent(Span.SERVER_SEND);
|
||||
}
|
||||
// in case of a response with exception status a exception controller will close the span
|
||||
HttpStatus httpStatus = HttpStatus.valueOf(response.getStatus());
|
||||
if (httpStatus.is2xxSuccessful() || httpStatus.is3xxRedirection()) {
|
||||
this.tracer.close(spanFromRequest);
|
||||
} else if(isSpanContinued(request)) {
|
||||
// it means that the span was already detached once and we're processing an error
|
||||
// in case of a response with exception status will close the span when exception dispatch is handled
|
||||
if (httpStatusSuccessful(response)) {
|
||||
this.tracer.close(spanFromRequest);
|
||||
} else {
|
||||
this.tracer.detach(spanFromRequest);
|
||||
@@ -162,6 +168,11 @@ public class TraceFilter extends OncePerRequestFilter {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean httpStatusSuccessful(HttpServletResponse response) {
|
||||
HttpStatus httpStatus = HttpStatus.valueOf(response.getStatus());
|
||||
return httpStatus.is2xxSuccessful() || httpStatus.is3xxRedirection();
|
||||
}
|
||||
|
||||
private Span getSpanFromAttribute(HttpServletRequest request) {
|
||||
return (Span) request.getAttribute(TRACE_REQUEST_ATTR);
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Random;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.netflix.loadbalancer.BaseLoadBalancer;
|
||||
import com.netflix.loadbalancer.ILoadBalancer;
|
||||
@@ -45,6 +46,7 @@ import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.cloud.netflix.feign.EnableFeignClients;
|
||||
import org.springframework.cloud.netflix.feign.FeignClient;
|
||||
import org.springframework.cloud.netflix.ribbon.RibbonClient;
|
||||
import org.springframework.cloud.sleuth.Log;
|
||||
import org.springframework.cloud.sleuth.Sampler;
|
||||
import org.springframework.cloud.sleuth.Span;
|
||||
import org.springframework.cloud.sleuth.SpanReporter;
|
||||
@@ -213,6 +215,12 @@ public class WebClientTests {
|
||||
Optional<Span> storedSpan = this.listener.getSpans().stream()
|
||||
.filter(span -> "404".equals(span.tags().get("http.status_code"))).findFirst();
|
||||
then(storedSpan.isPresent()).isTrue();
|
||||
this.listener.getSpans().stream()
|
||||
.forEach(span -> {
|
||||
int initialSize = span.logs().size();
|
||||
int distinctSize = span.logs().stream().map(Log::getEvent).distinct().collect(Collectors.toList()).size();
|
||||
then(initialSize).as("there are no duplicate log entries").isEqualTo(distinctSize);
|
||||
});
|
||||
then(this.testErrorController.getSpan()).isNotNull();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user