Skip to content

Commit 9f21413

Browse files
committed
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
1 parent 9ecc548 commit 9f21413

File tree

4 files changed

+90
-5
lines changed

4 files changed

+90
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.metrics;
18+
19+
import java.util.List;
20+
21+
import io.micrometer.core.instrument.Clock;
22+
import io.micrometer.core.instrument.MeterRegistry;
23+
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
24+
25+
/**
26+
* Specialization of {@link CompositeMeterRegistry} used to identify the auto-configured
27+
* composite.
28+
*
29+
* @author Andy Wilkinson
30+
*/
31+
class AutoConfiguredCompositeMeterRegistry extends CompositeMeterRegistry {
32+
33+
AutoConfiguredCompositeMeterRegistry(Clock clock, List<MeterRegistry> registries) {
34+
super(clock, registries);
35+
}
36+
37+
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/CompositeMeterRegistryConfiguration.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -42,8 +42,8 @@ class CompositeMeterRegistryConfiguration {
4242

4343
@Bean
4444
@Primary
45-
CompositeMeterRegistry compositeMeterRegistry(Clock clock, List<MeterRegistry> registries) {
46-
return new CompositeMeterRegistry(clock, registries);
45+
AutoConfiguredCompositeMeterRegistry compositeMeterRegistry(Clock clock, List<MeterRegistry> registries) {
46+
return new AutoConfiguredCompositeMeterRegistry(clock, registries);
4747
}
4848

4949
static class MultipleNonPrimaryMeterRegistriesCondition extends NoneNestedConditions {

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/MeterRegistryConfigurer.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -61,7 +61,9 @@ void configure(MeterRegistry registry) {
6161
// Customizers must be applied before binders, as they may add custom
6262
// tags or alter timer or summary configuration.
6363
customize(registry);
64-
addFilters(registry);
64+
if (!(registry instanceof AutoConfiguredCompositeMeterRegistry)) {
65+
addFilters(registry);
66+
}
6567
if (!this.hasCompositeMeterRegistry || registry instanceof CompositeMeterRegistry) {
6668
addBinders(registry);
6769
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/MetricsAutoConfigurationIntegrationTests.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.metrics;
1818

19+
import java.util.Arrays;
20+
import java.util.Set;
21+
1922
import io.micrometer.core.instrument.MeterRegistry;
2023
import io.micrometer.core.instrument.MockClock;
2124
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
2225
import io.micrometer.core.instrument.simple.SimpleConfig;
2326
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
2427
import io.micrometer.graphite.GraphiteMeterRegistry;
2528
import io.micrometer.jmx.JmxMeterRegistry;
29+
import org.assertj.core.api.InstanceOfAssertFactories;
2630
import org.junit.jupiter.api.Test;
2731

2832
import org.springframework.boot.actuate.autoconfigure.metrics.export.graphite.GraphiteMetricsExportAutoConfiguration;
@@ -109,6 +113,35 @@ void compositeCreatedWithMultipleRegistries() {
109113
});
110114
}
111115

116+
@Test
117+
void autoConfiguredCompositeDoesNotHaveMeterFiltersApplied() {
118+
new ApplicationContextRunner().with(MetricsRun.limitedTo(GraphiteMetricsExportAutoConfiguration.class,
119+
JmxMetricsExportAutoConfiguration.class)).run((context) -> {
120+
MeterRegistry composite = context.getBean(MeterRegistry.class);
121+
assertThat(composite).extracting("filters", InstanceOfAssertFactories.ARRAY).hasSize(0);
122+
assertThat(composite).isInstanceOf(CompositeMeterRegistry.class);
123+
Set<MeterRegistry> registries = ((CompositeMeterRegistry) composite).getRegistries();
124+
assertThat(registries).hasSize(2);
125+
assertThat(registries).hasAtLeastOneElementOfType(GraphiteMeterRegistry.class)
126+
.hasAtLeastOneElementOfType(JmxMeterRegistry.class);
127+
assertThat(registries).allSatisfy((registry) -> assertThat(registry)
128+
.extracting("filters", InstanceOfAssertFactories.ARRAY).hasSize(1));
129+
});
130+
}
131+
132+
@Test
133+
void userConfiguredCompositeHasMeterFiltersApplied() {
134+
new ApplicationContextRunner().with(MetricsRun.limitedTo())
135+
.withUserConfiguration(CompositeMeterRegistryConfiguration.class).run((context) -> {
136+
MeterRegistry composite = context.getBean(MeterRegistry.class);
137+
assertThat(composite).extracting("filters", InstanceOfAssertFactories.ARRAY).hasSize(1);
138+
assertThat(composite).isInstanceOf(CompositeMeterRegistry.class);
139+
Set<MeterRegistry> registries = ((CompositeMeterRegistry) composite).getRegistries();
140+
assertThat(registries).hasSize(2);
141+
assertThat(registries).hasOnlyElementsOfTypes(SimpleMeterRegistry.class);
142+
});
143+
}
144+
112145
@Configuration(proxyBeanMethods = false)
113146
static class PrimaryMeterRegistryConfiguration {
114147

@@ -120,4 +153,17 @@ MeterRegistry simpleMeterRegistry() {
120153

121154
}
122155

156+
@Configuration(proxyBeanMethods = false)
157+
static class CompositeMeterRegistryConfiguration {
158+
159+
@Bean
160+
CompositeMeterRegistry compositeMeterRegistry() {
161+
CompositeMeterRegistry compositeMeterRegistry = new CompositeMeterRegistry(new MockClock(),
162+
Arrays.asList(new SimpleMeterRegistry(), new SimpleMeterRegistry()));
163+
System.out.println(compositeMeterRegistry.getRegistries());
164+
return compositeMeterRegistry;
165+
}
166+
167+
}
168+
123169
}

0 commit comments

Comments
 (0)