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
This commit is contained in:
Marcin Grzejszczak
2016-06-15 13:09:24 +02:00
parent d6772db504
commit ea076ce944
2 changed files with 17 additions and 1 deletions

View File

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

View File

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