From efcc65bc5bb2870868cb43e540252cd87fa42a4a Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Thu, 15 Jun 2023 11:45:05 +0200 Subject: [PATCH] Apply filter order to ServerHttpObservationFilter Closes gh-35067 --- .../servlet/WebMvcObservationAutoConfiguration.java | 4 ++-- .../WebMvcObservationAutoConfigurationTests.java | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/observation/web/servlet/WebMvcObservationAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/observation/web/servlet/WebMvcObservationAutoConfiguration.java index 5a70728ac1..802b8a3e96 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/observation/web/servlet/WebMvcObservationAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/observation/web/servlet/WebMvcObservationAutoConfiguration.java @@ -44,7 +44,6 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import org.springframework.http.server.observation.DefaultServerRequestObservationConvention; import org.springframework.http.server.observation.ServerRequestObservationConvention; @@ -58,6 +57,7 @@ import org.springframework.web.servlet.DispatcherServlet; * @author Brian Clozel * @author Jon Schneider * @author Dmytro Nosan + * @author Moritz Halbritter * @since 3.0.0 */ @AutoConfiguration(after = { MetricsAutoConfiguration.class, CompositeMeterRegistryAutoConfiguration.class, @@ -90,7 +90,7 @@ public class WebMvcObservationAutoConfiguration { customTagsProvider.getIfAvailable(), contributorsProvider.orderedStream().toList()); ServerHttpObservationFilter filter = new ServerHttpObservationFilter(registry, convention); FilterRegistrationBean registration = new FilterRegistrationBean<>(filter); - registration.setOrder(Ordered.HIGHEST_PRECEDENCE + 1); + registration.setOrder(this.observationProperties.getHttp().getServer().getFilter().getOrder()); registration.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.ASYNC); return registration; } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/observation/web/servlet/WebMvcObservationAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/observation/web/servlet/WebMvcObservationAutoConfigurationTests.java index a8fe9bf9c9..7328d40b2b 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/observation/web/servlet/WebMvcObservationAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/observation/web/servlet/WebMvcObservationAutoConfigurationTests.java @@ -64,6 +64,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. * @author Tadaya Tsuyukubo * @author Madhura Bhave * @author Chanhyeong LEE + * @author Moritz Halbritter */ @ExtendWith(OutputCaptureExtension.class) @SuppressWarnings("removal") @@ -117,6 +118,15 @@ class WebMvcObservationAutoConfigurationTests { }); } + @Test + void filterRegistrationOrderCanBeOverridden() { + this.contextRunner.withPropertyValues("management.observations.http.server.filter.order=1000") + .run((context) -> { + FilterRegistrationBean registration = context.getBean(FilterRegistrationBean.class); + assertThat(registration.getOrder()).isEqualTo(1000); + }); + } + @Test void filterRegistrationBacksOffWithAnotherServerHttpObservationFilterRegistration() { this.contextRunner.withUserConfiguration(TestServerHttpObservationFilterRegistrationConfiguration.class)