Set error status in Observation Servlet filter

Prior to this commit, the Observation Servlet filter would record
unhandled exceptions on the observation context but would leave the
default HTTP response status as is.
Servlet containers do set the response status in that case to 500 by
default. Not doing that at the Servlet filter level results in invalid
observations, stating that the HTTP response status is 200 (because the
error status hasn't been set yet by the container) and as a result, the
outcome is SUCCESS.

This commit ensures that the error status is set in those cases,
aligning the behavior with Servlet containers.

Fixes gh-29512
This commit is contained in:
Brian Clozel
2022-11-18 14:15:49 +01:00
parent 45dc1d2602
commit 1960666765
2 changed files with 15 additions and 2 deletions

View File

@@ -97,7 +97,18 @@ class ServerHttpObservationFilterTests {
ServerRequestObservationContext context = (ServerRequestObservationContext) this.request
.getAttribute(ServerHttpObservationFilter.CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE);
assertThat(context.getError()).isEqualTo(customError);
assertThatHttpObservation().hasLowCardinalityKeyValue("outcome", "SUCCESS");
assertThatHttpObservation().hasLowCardinalityKeyValue("outcome", "SERVER_ERROR");
}
@Test
void filterShouldSetDefaultErrorStatusForBubblingExceptions() {
assertThatThrownBy(() -> {
this.filter.doFilter(this.request, this.response, (request, response) -> {
throw new ServletException(new IllegalArgumentException("custom error"));
});
}).isInstanceOf(ServletException.class);
assertThatHttpObservation().hasLowCardinalityKeyValue("outcome", "SERVER_ERROR")
.hasLowCardinalityKeyValue("status", "500");
}
private TestObservationRegistryAssert.TestObservationRegistryAssertReturningObservationContextAssert assertThatHttpObservation() {