mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Order MeterFilters and MeterRegistryCustomizers
Closes gh-14780
This commit is contained in:
parent
a296a805c5
commit
2a2908e74e
@ -16,15 +16,16 @@
|
||||
|
||||
package org.springframework.boot.actuate.autoconfigure.metrics;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
import io.micrometer.core.instrument.binder.MeterBinder;
|
||||
import io.micrometer.core.instrument.config.MeterFilter;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
/**
|
||||
* {@link BeanPostProcessor} that delegates to a lazily created
|
||||
@ -36,12 +37,24 @@ import org.springframework.context.ApplicationContext;
|
||||
*/
|
||||
class MeterRegistryPostProcessor implements BeanPostProcessor {
|
||||
|
||||
private final ApplicationContext context;
|
||||
private final ObjectProvider<List<MeterBinder>> meterBinders;
|
||||
|
||||
private final ObjectProvider<List<MeterFilter>> meterFilters;
|
||||
|
||||
private final ObjectProvider<List<MeterRegistryCustomizer<?>>> meterRegistryCustomizers;
|
||||
|
||||
private final ObjectProvider<MetricsProperties> metricsProperties;
|
||||
|
||||
private volatile MeterRegistryConfigurer configurer;
|
||||
|
||||
MeterRegistryPostProcessor(ApplicationContext context) {
|
||||
this.context = context;
|
||||
MeterRegistryPostProcessor(ObjectProvider<List<MeterBinder>> meterBinders,
|
||||
ObjectProvider<List<MeterFilter>> meterFilters,
|
||||
ObjectProvider<List<MeterRegistryCustomizer<?>>> meterRegistryCustomizers,
|
||||
ObjectProvider<MetricsProperties> metricsProperties) {
|
||||
this.meterBinders = meterBinders;
|
||||
this.meterFilters = meterFilters;
|
||||
this.meterRegistryCustomizers = meterRegistryCustomizers;
|
||||
this.metricsProperties = metricsProperties;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -53,20 +66,15 @@ class MeterRegistryPostProcessor implements BeanPostProcessor {
|
||||
return bean;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private MeterRegistryConfigurer getConfigurer() {
|
||||
if (this.configurer == null) {
|
||||
this.configurer = new MeterRegistryConfigurer(beansOfType(MeterBinder.class),
|
||||
beansOfType(MeterFilter.class),
|
||||
(Collection<MeterRegistryCustomizer<?>>) (Object) beansOfType(
|
||||
MeterRegistryCustomizer.class),
|
||||
this.context.getBean(MetricsProperties.class).isUseGlobalRegistry());
|
||||
this.configurer = new MeterRegistryConfigurer(
|
||||
this.meterBinders.getIfAvailable(Collections::emptyList),
|
||||
this.meterFilters.getIfAvailable(Collections::emptyList),
|
||||
this.meterRegistryCustomizers.getIfAvailable(Collections::emptyList),
|
||||
this.metricsProperties.getObject().isUseGlobalRegistry());
|
||||
}
|
||||
return this.configurer;
|
||||
}
|
||||
|
||||
private <T> Collection<T> beansOfType(Class<T> type) {
|
||||
return this.context.getBeansOfType(type).values();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -16,9 +16,12 @@
|
||||
|
||||
package org.springframework.boot.actuate.autoconfigure.metrics;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import ch.qos.logback.classic.LoggerContext;
|
||||
import io.micrometer.core.annotation.Timed;
|
||||
import io.micrometer.core.instrument.Clock;
|
||||
import io.micrometer.core.instrument.binder.MeterBinder;
|
||||
import io.micrometer.core.instrument.binder.jvm.ClassLoaderMetrics;
|
||||
import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics;
|
||||
import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics;
|
||||
@ -27,9 +30,11 @@ import io.micrometer.core.instrument.binder.logging.LogbackMetrics;
|
||||
import io.micrometer.core.instrument.binder.system.FileDescriptorMetrics;
|
||||
import io.micrometer.core.instrument.binder.system.ProcessorMetrics;
|
||||
import io.micrometer.core.instrument.binder.system.UptimeMetrics;
|
||||
import io.micrometer.core.instrument.config.MeterFilter;
|
||||
import org.slf4j.ILoggerFactory;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
|
||||
@ -39,7 +44,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
@ -68,8 +72,12 @@ public class MetricsAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public static MeterRegistryPostProcessor meterRegistryPostProcessor(
|
||||
ApplicationContext context) {
|
||||
return new MeterRegistryPostProcessor(context);
|
||||
ObjectProvider<List<MeterBinder>> meterBinders,
|
||||
ObjectProvider<List<MeterFilter>> meterFilters,
|
||||
ObjectProvider<List<MeterRegistryCustomizer<?>>> meterRegistryCustomizers,
|
||||
ObjectProvider<MetricsProperties> metricsProperties) {
|
||||
return new MeterRegistryPostProcessor(meterBinders, meterFilters,
|
||||
meterRegistryCustomizers, metricsProperties);
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
@ -19,6 +19,7 @@ package org.springframework.boot.actuate.autoconfigure.metrics;
|
||||
import java.util.List;
|
||||
|
||||
import io.micrometer.core.instrument.Clock;
|
||||
import io.micrometer.core.instrument.Meter;
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
import io.micrometer.core.instrument.binder.MeterBinder;
|
||||
import io.micrometer.core.instrument.binder.jvm.ClassLoaderMetrics;
|
||||
@ -30,6 +31,7 @@ import io.micrometer.core.instrument.binder.system.FileDescriptorMetrics;
|
||||
import io.micrometer.core.instrument.binder.system.ProcessorMetrics;
|
||||
import io.micrometer.core.instrument.binder.system.UptimeMetrics;
|
||||
import io.micrometer.core.instrument.config.MeterFilter;
|
||||
import io.micrometer.core.instrument.config.MeterFilterReply;
|
||||
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
|
||||
import org.junit.Test;
|
||||
|
||||
@ -37,6 +39,7 @@ import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@ -75,7 +78,12 @@ public class MetricsAutoConfigurationTests {
|
||||
MeterRegistry meterRegistry = context.getBean(MeterRegistry.class);
|
||||
List<MeterFilter> filters = (List<MeterFilter>) ReflectionTestUtils
|
||||
.getField(meterRegistry, "filters");
|
||||
assertThat(filters).isNotEmpty();
|
||||
assertThat(filters).hasSize(3);
|
||||
assertThat(filters.get(0).accept((Meter.Id) null))
|
||||
.isEqualTo(MeterFilterReply.DENY);
|
||||
assertThat(filters.get(1)).isInstanceOf(PropertiesMeterFilter.class);
|
||||
assertThat(filters.get(2).accept((Meter.Id) null))
|
||||
.isEqualTo(MeterFilterReply.ACCEPT);
|
||||
verify((MeterBinder) context.getBean("meterBinder"))
|
||||
.bindTo(meterRegistry);
|
||||
verify(context.getBean(MeterRegistryCustomizer.class))
|
||||
@ -262,6 +270,18 @@ public class MetricsAutoConfigurationTests {
|
||||
return mock(MeterBinder.class);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Order(1)
|
||||
MeterFilter acceptMeterFilter() {
|
||||
return MeterFilter.accept();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Order(-1)
|
||||
MeterFilter denyMeterFilter() {
|
||||
return MeterFilter.deny();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
|
Loading…
Reference in New Issue
Block a user