Browse Source

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
pull/29364/head
Brian Clozel 2 years ago
parent
commit
57bbc09fca
  1. 2
      spring-web/src/main/java/org/springframework/web/filter/ServerHttpObservationFilter.java
  2. 2
      spring-web/src/test/java/org/springframework/web/filter/ServerHttpObservationFilterTests.java

2
spring-web/src/main/java/org/springframework/web/filter/ServerHttpObservationFilter.java

@ -27,7 +27,6 @@ import jakarta.servlet.ServletException; @@ -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 { @@ -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();

2
spring-web/src/test/java/org/springframework/web/filter/ServerHttpObservationFilterTests.java

@ -70,7 +70,7 @@ class ServerHttpObservationFilterTests { @@ -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

Loading…
Cancel
Save