Do not set response status in ServerHttpObservationFilter

Prior to this commit, the `ServerHttpObservationFilter` would set the
response status (and possibly overwrite it) in case an exception is
found as an attribute.
While the exception itself should be used in the observation, the filter
should have no side effect on the response.

Fixes gh-29353
This commit is contained in:
Brian Clozel
2022-10-19 12:11:00 +02:00
parent f99c02fc94
commit 57bbc09fca
2 changed files with 1 additions and 3 deletions

View File

@@ -27,7 +27,6 @@ import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.observation.DefaultServerRequestObservationConvention;
import org.springframework.http.observation.ServerHttpObservationDocumentation;
import org.springframework.http.observation.ServerRequestObservationContext;
@@ -117,7 +116,6 @@ public class ServerHttpObservationFilter extends OncePerRequestFilter {
if (!request.isAsyncStarted()) {
Throwable error = fetchException(request);
if (error != null) {
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
observation.error(error);
}
observation.stop();

View File

@@ -70,7 +70,7 @@ class ServerHttpObservationFilterTests {
ServerRequestObservationContext context = (ServerRequestObservationContext) this.request
.getAttribute(ServerHttpObservationFilter.CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE);
assertThat(context.getError()).isEqualTo(customError);
assertThatHttpObservation().hasLowCardinalityKeyValue("outcome", "SERVER_ERROR");
assertThatHttpObservation().hasLowCardinalityKeyValue("exception", "IllegalArgumentException");
}
@Test