Apply filter order to ServerHttpObservationFilter

Closes gh-35067
pull/35914/head
Moritz Halbritter 1 year ago
parent 140c37ceba
commit efcc65bc5b

@ -44,7 +44,6 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.http.server.observation.DefaultServerRequestObservationConvention; import org.springframework.http.server.observation.DefaultServerRequestObservationConvention;
import org.springframework.http.server.observation.ServerRequestObservationConvention; import org.springframework.http.server.observation.ServerRequestObservationConvention;
@ -58,6 +57,7 @@ import org.springframework.web.servlet.DispatcherServlet;
* @author Brian Clozel * @author Brian Clozel
* @author Jon Schneider * @author Jon Schneider
* @author Dmytro Nosan * @author Dmytro Nosan
* @author Moritz Halbritter
* @since 3.0.0 * @since 3.0.0
*/ */
@AutoConfiguration(after = { MetricsAutoConfiguration.class, CompositeMeterRegistryAutoConfiguration.class, @AutoConfiguration(after = { MetricsAutoConfiguration.class, CompositeMeterRegistryAutoConfiguration.class,
@ -90,7 +90,7 @@ public class WebMvcObservationAutoConfiguration {
customTagsProvider.getIfAvailable(), contributorsProvider.orderedStream().toList()); customTagsProvider.getIfAvailable(), contributorsProvider.orderedStream().toList());
ServerHttpObservationFilter filter = new ServerHttpObservationFilter(registry, convention); ServerHttpObservationFilter filter = new ServerHttpObservationFilter(registry, convention);
FilterRegistrationBean<ServerHttpObservationFilter> registration = new FilterRegistrationBean<>(filter); FilterRegistrationBean<ServerHttpObservationFilter> registration = new FilterRegistrationBean<>(filter);
registration.setOrder(Ordered.HIGHEST_PRECEDENCE + 1); registration.setOrder(this.observationProperties.getHttp().getServer().getFilter().getOrder());
registration.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.ASYNC); registration.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.ASYNC);
return registration; return registration;
} }

@ -64,6 +64,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
* @author Tadaya Tsuyukubo * @author Tadaya Tsuyukubo
* @author Madhura Bhave * @author Madhura Bhave
* @author Chanhyeong LEE * @author Chanhyeong LEE
* @author Moritz Halbritter
*/ */
@ExtendWith(OutputCaptureExtension.class) @ExtendWith(OutputCaptureExtension.class)
@SuppressWarnings("removal") @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 @Test
void filterRegistrationBacksOffWithAnotherServerHttpObservationFilterRegistration() { void filterRegistrationBacksOffWithAnotherServerHttpObservationFilterRegistration() {
this.contextRunner.withUserConfiguration(TestServerHttpObservationFilterRegistrationConfiguration.class) this.contextRunner.withUserConfiguration(TestServerHttpObservationFilterRegistrationConfiguration.class)

Loading…
Cancel
Save