Skip to content

Commit 2a2908e

Browse files
committed
Order MeterFilters and MeterRegistryCustomizers
Closes gh-14780
1 parent a296a80 commit 2a2908e

File tree

3 files changed

+55
-19
lines changed

3 files changed

+55
-19
lines changed

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

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@
1616

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

19-
import java.util.Collection;
19+
import java.util.Collections;
20+
import java.util.List;
2021

2122
import io.micrometer.core.instrument.MeterRegistry;
2223
import io.micrometer.core.instrument.binder.MeterBinder;
2324
import io.micrometer.core.instrument.config.MeterFilter;
2425

2526
import org.springframework.beans.BeansException;
27+
import org.springframework.beans.factory.ObjectProvider;
2628
import org.springframework.beans.factory.config.BeanPostProcessor;
27-
import org.springframework.context.ApplicationContext;
2829

2930
/**
3031
* {@link BeanPostProcessor} that delegates to a lazily created
@@ -36,12 +37,24 @@
3637
*/
3738
class MeterRegistryPostProcessor implements BeanPostProcessor {
3839

39-
private final ApplicationContext context;
40+
private final ObjectProvider<List<MeterBinder>> meterBinders;
41+
42+
private final ObjectProvider<List<MeterFilter>> meterFilters;
43+
44+
private final ObjectProvider<List<MeterRegistryCustomizer<?>>> meterRegistryCustomizers;
45+
46+
private final ObjectProvider<MetricsProperties> metricsProperties;
4047

4148
private volatile MeterRegistryConfigurer configurer;
4249

43-
MeterRegistryPostProcessor(ApplicationContext context) {
44-
this.context = context;
50+
MeterRegistryPostProcessor(ObjectProvider<List<MeterBinder>> meterBinders,
51+
ObjectProvider<List<MeterFilter>> meterFilters,
52+
ObjectProvider<List<MeterRegistryCustomizer<?>>> meterRegistryCustomizers,
53+
ObjectProvider<MetricsProperties> metricsProperties) {
54+
this.meterBinders = meterBinders;
55+
this.meterFilters = meterFilters;
56+
this.meterRegistryCustomizers = meterRegistryCustomizers;
57+
this.metricsProperties = metricsProperties;
4558
}
4659

4760
@Override
@@ -53,20 +66,15 @@ public Object postProcessAfterInitialization(Object bean, String beanName)
5366
return bean;
5467
}
5568

56-
@SuppressWarnings("unchecked")
5769
private MeterRegistryConfigurer getConfigurer() {
5870
if (this.configurer == null) {
59-
this.configurer = new MeterRegistryConfigurer(beansOfType(MeterBinder.class),
60-
beansOfType(MeterFilter.class),
61-
(Collection<MeterRegistryCustomizer<?>>) (Object) beansOfType(
62-
MeterRegistryCustomizer.class),
63-
this.context.getBean(MetricsProperties.class).isUseGlobalRegistry());
71+
this.configurer = new MeterRegistryConfigurer(
72+
this.meterBinders.getIfAvailable(Collections::emptyList),
73+
this.meterFilters.getIfAvailable(Collections::emptyList),
74+
this.meterRegistryCustomizers.getIfAvailable(Collections::emptyList),
75+
this.metricsProperties.getObject().isUseGlobalRegistry());
6476
}
6577
return this.configurer;
6678
}
6779

68-
private <T> Collection<T> beansOfType(Class<T> type) {
69-
return this.context.getBeansOfType(type).values();
70-
}
71-
7280
}

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616

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

19+
import java.util.List;
20+
1921
import ch.qos.logback.classic.LoggerContext;
2022
import io.micrometer.core.annotation.Timed;
2123
import io.micrometer.core.instrument.Clock;
24+
import io.micrometer.core.instrument.binder.MeterBinder;
2225
import io.micrometer.core.instrument.binder.jvm.ClassLoaderMetrics;
2326
import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics;
2427
import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics;
@@ -27,9 +30,11 @@
2730
import io.micrometer.core.instrument.binder.system.FileDescriptorMetrics;
2831
import io.micrometer.core.instrument.binder.system.ProcessorMetrics;
2932
import io.micrometer.core.instrument.binder.system.UptimeMetrics;
33+
import io.micrometer.core.instrument.config.MeterFilter;
3034
import org.slf4j.ILoggerFactory;
3135
import org.slf4j.LoggerFactory;
3236

37+
import org.springframework.beans.factory.ObjectProvider;
3338
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
3439
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
3540
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
@@ -39,7 +44,6 @@
3944
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
4045
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
4146
import org.springframework.boot.context.properties.EnableConfigurationProperties;
42-
import org.springframework.context.ApplicationContext;
4347
import org.springframework.context.annotation.Bean;
4448
import org.springframework.context.annotation.ConditionContext;
4549
import org.springframework.context.annotation.Conditional;
@@ -68,8 +72,12 @@ public Clock micrometerClock() {
6872

6973
@Bean
7074
public static MeterRegistryPostProcessor meterRegistryPostProcessor(
71-
ApplicationContext context) {
72-
return new MeterRegistryPostProcessor(context);
75+
ObjectProvider<List<MeterBinder>> meterBinders,
76+
ObjectProvider<List<MeterFilter>> meterFilters,
77+
ObjectProvider<List<MeterRegistryCustomizer<?>>> meterRegistryCustomizers,
78+
ObjectProvider<MetricsProperties> metricsProperties) {
79+
return new MeterRegistryPostProcessor(meterBinders, meterFilters,
80+
meterRegistryCustomizers, metricsProperties);
7381
}
7482

7583
@Bean

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.List;
2020

2121
import io.micrometer.core.instrument.Clock;
22+
import io.micrometer.core.instrument.Meter;
2223
import io.micrometer.core.instrument.MeterRegistry;
2324
import io.micrometer.core.instrument.binder.MeterBinder;
2425
import io.micrometer.core.instrument.binder.jvm.ClassLoaderMetrics;
@@ -30,13 +31,15 @@
3031
import io.micrometer.core.instrument.binder.system.ProcessorMetrics;
3132
import io.micrometer.core.instrument.binder.system.UptimeMetrics;
3233
import io.micrometer.core.instrument.config.MeterFilter;
34+
import io.micrometer.core.instrument.config.MeterFilterReply;
3335
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
3436
import org.junit.Test;
3537

3638
import org.springframework.boot.autoconfigure.AutoConfigurations;
3739
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
3840
import org.springframework.context.annotation.Bean;
3941
import org.springframework.context.annotation.Configuration;
42+
import org.springframework.core.annotation.Order;
4043
import org.springframework.test.util.ReflectionTestUtils;
4144

4245
import static org.assertj.core.api.Assertions.assertThat;
@@ -75,7 +78,12 @@ public void configuresMeterRegistries() {
7578
MeterRegistry meterRegistry = context.getBean(MeterRegistry.class);
7679
List<MeterFilter> filters = (List<MeterFilter>) ReflectionTestUtils
7780
.getField(meterRegistry, "filters");
78-
assertThat(filters).isNotEmpty();
81+
assertThat(filters).hasSize(3);
82+
assertThat(filters.get(0).accept((Meter.Id) null))
83+
.isEqualTo(MeterFilterReply.DENY);
84+
assertThat(filters.get(1)).isInstanceOf(PropertiesMeterFilter.class);
85+
assertThat(filters.get(2).accept((Meter.Id) null))
86+
.isEqualTo(MeterFilterReply.ACCEPT);
7987
verify((MeterBinder) context.getBean("meterBinder"))
8088
.bindTo(meterRegistry);
8189
verify(context.getBean(MeterRegistryCustomizer.class))
@@ -262,6 +270,18 @@ MeterBinder meterBinder() {
262270
return mock(MeterBinder.class);
263271
}
264272

273+
@Bean
274+
@Order(1)
275+
MeterFilter acceptMeterFilter() {
276+
return MeterFilter.accept();
277+
}
278+
279+
@Bean
280+
@Order(-1)
281+
MeterFilter denyMeterFilter() {
282+
return MeterFilter.deny();
283+
}
284+
265285
}
266286

267287
@Configuration

0 commit comments

Comments
 (0)