diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/AutoConfiguredCompositeMeterRegistry.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/AutoConfiguredCompositeMeterRegistry.java new file mode 100644 index 0000000000..d38b43c271 --- /dev/null +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/AutoConfiguredCompositeMeterRegistry.java @@ -0,0 +1,37 @@ +/* + * Copyright 2012-2020 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.actuate.autoconfigure.metrics; + +import java.util.List; + +import io.micrometer.core.instrument.Clock; +import io.micrometer.core.instrument.MeterRegistry; +import io.micrometer.core.instrument.composite.CompositeMeterRegistry; + +/** + * Specialization of {@link CompositeMeterRegistry} used to identify the auto-configured + * composite. + * + * @author Andy Wilkinson + */ +class AutoConfiguredCompositeMeterRegistry extends CompositeMeterRegistry { + + AutoConfiguredCompositeMeterRegistry(Clock clock, List registries) { + super(clock, registries); + } + +} diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/CompositeMeterRegistryConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/CompositeMeterRegistryConfiguration.java index cd15fac2ca..4e75221565 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/CompositeMeterRegistryConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/CompositeMeterRegistryConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 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. @@ -42,8 +42,8 @@ class CompositeMeterRegistryConfiguration { @Bean @Primary - CompositeMeterRegistry compositeMeterRegistry(Clock clock, List registries) { - return new CompositeMeterRegistry(clock, registries); + AutoConfiguredCompositeMeterRegistry compositeMeterRegistry(Clock clock, List registries) { + return new AutoConfiguredCompositeMeterRegistry(clock, registries); } static class MultipleNonPrimaryMeterRegistriesCondition extends NoneNestedConditions { diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/MeterRegistryConfigurer.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/MeterRegistryConfigurer.java index 3cbe4a451d..fd062849a0 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/MeterRegistryConfigurer.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/MeterRegistryConfigurer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 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. @@ -61,7 +61,9 @@ class MeterRegistryConfigurer { // Customizers must be applied before binders, as they may add custom // tags or alter timer or summary configuration. customize(registry); - addFilters(registry); + if (!(registry instanceof AutoConfiguredCompositeMeterRegistry)) { + addFilters(registry); + } if (!this.hasCompositeMeterRegistry || registry instanceof CompositeMeterRegistry) { addBinders(registry); } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/MetricsAutoConfigurationIntegrationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/MetricsAutoConfigurationIntegrationTests.java index 42cae9566a..40a5677549 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/MetricsAutoConfigurationIntegrationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/MetricsAutoConfigurationIntegrationTests.java @@ -16,6 +16,9 @@ package org.springframework.boot.actuate.autoconfigure.metrics; +import java.util.Arrays; +import java.util.Set; + import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.MockClock; import io.micrometer.core.instrument.composite.CompositeMeterRegistry; @@ -23,6 +26,7 @@ import io.micrometer.core.instrument.simple.SimpleConfig; import io.micrometer.core.instrument.simple.SimpleMeterRegistry; import io.micrometer.graphite.GraphiteMeterRegistry; import io.micrometer.jmx.JmxMeterRegistry; +import org.assertj.core.api.InstanceOfAssertFactories; import org.junit.jupiter.api.Test; import org.springframework.boot.actuate.autoconfigure.metrics.export.graphite.GraphiteMetricsExportAutoConfiguration; @@ -109,6 +113,35 @@ class MetricsAutoConfigurationIntegrationTests { }); } + @Test + void autoConfiguredCompositeDoesNotHaveMeterFiltersApplied() { + new ApplicationContextRunner().with(MetricsRun.limitedTo(GraphiteMetricsExportAutoConfiguration.class, + JmxMetricsExportAutoConfiguration.class)).run((context) -> { + MeterRegistry composite = context.getBean(MeterRegistry.class); + assertThat(composite).extracting("filters", InstanceOfAssertFactories.ARRAY).hasSize(0); + assertThat(composite).isInstanceOf(CompositeMeterRegistry.class); + Set registries = ((CompositeMeterRegistry) composite).getRegistries(); + assertThat(registries).hasSize(2); + assertThat(registries).hasAtLeastOneElementOfType(GraphiteMeterRegistry.class) + .hasAtLeastOneElementOfType(JmxMeterRegistry.class); + assertThat(registries).allSatisfy((registry) -> assertThat(registry) + .extracting("filters", InstanceOfAssertFactories.ARRAY).hasSize(1)); + }); + } + + @Test + void userConfiguredCompositeHasMeterFiltersApplied() { + new ApplicationContextRunner().with(MetricsRun.limitedTo()) + .withUserConfiguration(CompositeMeterRegistryConfiguration.class).run((context) -> { + MeterRegistry composite = context.getBean(MeterRegistry.class); + assertThat(composite).extracting("filters", InstanceOfAssertFactories.ARRAY).hasSize(1); + assertThat(composite).isInstanceOf(CompositeMeterRegistry.class); + Set registries = ((CompositeMeterRegistry) composite).getRegistries(); + assertThat(registries).hasSize(2); + assertThat(registries).hasOnlyElementsOfTypes(SimpleMeterRegistry.class); + }); + } + @Configuration(proxyBeanMethods = false) static class PrimaryMeterRegistryConfiguration { @@ -120,4 +153,17 @@ class MetricsAutoConfigurationIntegrationTests { } + @Configuration(proxyBeanMethods = false) + static class CompositeMeterRegistryConfiguration { + + @Bean + CompositeMeterRegistry compositeMeterRegistry() { + CompositeMeterRegistry compositeMeterRegistry = new CompositeMeterRegistry(new MockClock(), + Arrays.asList(new SimpleMeterRegistry(), new SimpleMeterRegistry())); + System.out.println(compositeMeterRegistry.getRegistries()); + return compositeMeterRegistry; + } + + } + }