Stop applying MeterFilters to auto-configured composite registry
Previously, all MeterFilter beans were applied to all MeterRegistry beans. As a result, when a composite registry was auto-configured, both the composite and all of its delegates would have the same MeterFilters applied. This made it impossible for one of the delegate registries to have a locally-configured filter that would allow a meter that would be denied by one of the MeterFilter beans applied to the composite. This commit update MeterRegistryConfigurer to skips the auto-configured composite meter registry when applying MeterFilter beans to MeterRegistry beans. As a result, the composite's filters will no longer deny a meter before it reaches a delegate that would have accepted it due to one of its locally-configured filters. Closes gh-23381
This commit is contained in:
@@ -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<MeterRegistry> registries) {
|
||||
super(clock, registries);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<MeterRegistry> registries) {
|
||||
return new CompositeMeterRegistry(clock, registries);
|
||||
AutoConfiguredCompositeMeterRegistry compositeMeterRegistry(Clock clock, List<MeterRegistry> registries) {
|
||||
return new AutoConfiguredCompositeMeterRegistry(clock, registries);
|
||||
}
|
||||
|
||||
static class MultipleNonPrimaryMeterRegistriesCondition extends NoneNestedConditions {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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<MeterRegistry> 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<MeterRegistry> 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user