Allow WebMvcFilter to be overridden by user configuration

Closes gh-28428
pull/29975/head
Stephane Nicoll 3 years ago
parent 270e162479
commit c02faea9c9

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2020 the original author or authors. * Copyright 2012-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -82,6 +82,7 @@ public class WebMvcMetricsAutoConfiguration {
} }
@Bean @Bean
@ConditionalOnMissingBean
public FilterRegistrationBean<WebMvcMetricsFilter> webMvcMetricsFilter(MeterRegistry registry, public FilterRegistrationBean<WebMvcMetricsFilter> webMvcMetricsFilter(MeterRegistry registry,
WebMvcTagsProvider tagsProvider) { WebMvcTagsProvider tagsProvider) {
ServerRequest request = this.properties.getWeb().getServer().getRequest(); ServerRequest request = this.properties.getWeb().getServer().getRequest();

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2020 the original author or authors. * Copyright 2012-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -121,6 +121,15 @@ class WebMvcMetricsAutoConfigurationTests {
}); });
} }
@Test
void filterRegistrationBacksOff() {
this.contextRunner.withUserConfiguration(TestWebMvcMetricsFilterConfiguration.class).run((context) -> {
assertThat(context).hasSingleBean(FilterRegistrationBean.class);
assertThat(context.getBean(FilterRegistrationBean.class))
.isSameAs(context.getBean("testWebMvcMetricsFilter"));
});
}
@Test @Test
void afterMaxUrisReachedFurtherUrisAreDenied(CapturedOutput output) { void afterMaxUrisReachedFurtherUrisAreDenied(CapturedOutput output) {
this.contextRunner.withUserConfiguration(TestController.class) this.contextRunner.withUserConfiguration(TestController.class)
@ -248,4 +257,15 @@ class WebMvcMetricsAutoConfigurationTests {
} }
@Configuration(proxyBeanMethods = false)
static class TestWebMvcMetricsFilterConfiguration {
@Bean
@SuppressWarnings("unchecked")
FilterRegistrationBean<WebMvcMetricsFilter> testWebMvcMetricsFilter() {
return mock(FilterRegistrationBean.class);
}
}
} }

@ -693,7 +693,7 @@ Metrics are tagged by the name of the executor, which is derived from the bean n
==== Spring MVC Metrics ==== Spring MVC Metrics
Auto-configuration enables the instrumentation of all requests handled by Spring MVC controllers and functional handlers. Auto-configuration enables the instrumentation of all requests handled by Spring MVC controllers and functional handlers.
By default, metrics are generated with the name, `http.server.requests`. By default, metrics are generated with the name, `http.server.requests`.
You can customized the name by setting the configprop:management.metrics.web.server.request.metric-name[] property. You can customize the name by setting the configprop:management.metrics.web.server.request.metric-name[] property.
`@Timed` annotations are supported on `@Controller` classes and `@RequestMapping` methods (see <<actuator#actuator.metrics.supported.timed-annotation>> for details). `@Timed` annotations are supported on `@Controller` classes and `@RequestMapping` methods (see <<actuator#actuator.metrics.supported.timed-annotation>> for details).
If you do not want to record metrics for all Spring MVC requests, you can set configprop:management.metrics.web.server.request.autotime.enabled[] to `false` and exclusively use `@Timed` annotations instead. If you do not want to record metrics for all Spring MVC requests, you can set configprop:management.metrics.web.server.request.autotime.enabled[] to `false` and exclusively use `@Timed` annotations instead.
@ -726,6 +726,9 @@ To replace the default tags, provide a `@Bean` that implements `WebMvcTagsProvid
TIP: In some cases, exceptions handled in web controllers are not recorded as request metrics tags. TIP: In some cases, exceptions handled in web controllers are not recorded as request metrics tags.
Applications can opt in and record exceptions by <<web#web.servlet.spring-mvc.error-handling, setting handled exceptions as request attributes>>. Applications can opt in and record exceptions by <<web#web.servlet.spring-mvc.error-handling, setting handled exceptions as request attributes>>.
By default, all requests are handled.
To customize the filter, provide a `@Bean` that implements `FilterRegistrationBean<WebMvcMetricsFilter>`.
[[actuator.metrics.supported.spring-webflux]] [[actuator.metrics.supported.spring-webflux]]

Loading…
Cancel
Save