diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/web/servlet/WebMvcMetricsAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/web/servlet/WebMvcMetricsAutoConfiguration.java index aebd1fb681..ab525e02c3 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/web/servlet/WebMvcMetricsAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/web/servlet/WebMvcMetricsAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -82,6 +82,7 @@ public class WebMvcMetricsAutoConfiguration { } @Bean + @ConditionalOnMissingBean public FilterRegistrationBean webMvcMetricsFilter(MeterRegistry registry, WebMvcTagsProvider tagsProvider) { ServerRequest request = this.properties.getWeb().getServer().getRequest(); diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/web/servlet/WebMvcMetricsAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/web/servlet/WebMvcMetricsAutoConfigurationTests.java index 4a003d461c..a2fe5444af 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/web/servlet/WebMvcMetricsAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/web/servlet/WebMvcMetricsAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (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 void afterMaxUrisReachedFurtherUrisAreDenied(CapturedOutput output) { this.contextRunner.withUserConfiguration(TestController.class) @@ -248,4 +257,15 @@ class WebMvcMetricsAutoConfigurationTests { } + @Configuration(proxyBeanMethods = false) + static class TestWebMvcMetricsFilterConfiguration { + + @Bean + @SuppressWarnings("unchecked") + FilterRegistrationBean testWebMvcMetricsFilter() { + return mock(FilterRegistrationBean.class); + } + + } + } diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/actuator/metrics.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/actuator/metrics.adoc index 942caee297..638439837e 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/actuator/metrics.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/actuator/metrics.adoc @@ -693,7 +693,7 @@ Metrics are tagged by the name of the executor, which is derived from the bean n ==== Spring MVC Metrics 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`. -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 <> 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. @@ -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. Applications can opt in and record exceptions by <>. +By default, all requests are handled. +To customize the filter, provide a `@Bean` that implements `FilterRegistrationBean`. + [[actuator.metrics.supported.spring-webflux]]