diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceFilter.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceFilter.java index 1f88656af..cc1db1df8 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceFilter.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceFilter.java @@ -216,6 +216,9 @@ public class TraceFilter extends GenericFilterBean { } private boolean httpStatusSuccessful(HttpServletResponse response) { + if (response.getStatus() == 0) { + return false; + } HttpStatus httpStatus = HttpStatus.valueOf(response.getStatus()); return httpStatus.is2xxSuccessful() || httpStatus.is3xxRedirection(); } @@ -308,7 +311,8 @@ public class TraceFilter extends GenericFilterBean { this.tracer.addTag(this.traceKeys.getHttp().getStatusCode(), String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR)); } - else if ((httpStatus < 200) || (httpStatus > 399)) { + // only tag valid http statuses + else if (httpStatus >= 100 && (httpStatus < 200) || (httpStatus > 399)) { this.tracer.addTag(this.traceKeys.getHttp().getStatusCode(), String.valueOf(response.getStatus())); } diff --git a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/web/TraceFilterTests.java b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/web/TraceFilterTests.java index 0add851d1..427b49424 100644 --- a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/web/TraceFilterTests.java +++ b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/web/TraceFilterTests.java @@ -131,6 +131,18 @@ public class TraceFilterTests { then(TestSpanContextHolder.getCurrentSpan()).isNull(); } + @Test + public void shouldNotStoreHttpStatusCodeWhenResponseCodeHasNotYetBeenSet() throws Exception { + TraceFilter filter = new TraceFilter(this.tracer, this.traceKeys, this.spanReporter, + this.spanExtractor, this.spanInjector, this.httpTraceKeysInjector); + this.response.setStatus(0); + filter.doFilter(this.request, this.response, this.filterChain); + + assertThat(this.span.tags()).doesNotContainKey("http.status_code"); + + then(TestSpanContextHolder.getCurrentSpan()).isNull(); + } + @Test public void startsNewTraceWithParentIdInHeaders() throws Exception { this.request = builder()