From ea076ce9448dc6ca404687c0f44566f09f38ade5 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Wed, 15 Jun 2016 13:09:24 +0200 Subject: [PATCH] Added verification for lack of response status code With this change when there is no http response status code an exception is not thrown and the tags are not set for http.status_code fixes #304 --- .../cloud/sleuth/instrument/web/TraceFilter.java | 6 +++++- .../sleuth/instrument/web/TraceFilterTests.java | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) 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()