Use auto-configuration to create composite, not a BFPP

Closes gh-12122
pull/12124/head
Andy Wilkinson 7 years ago
parent 1e932860c4
commit 73460a5b25

@ -0,0 +1,37 @@
/*
* Copyright 2012-2018 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
*
* http://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 io.micrometer.core.instrument.composite.CompositeMeterRegistry;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Import;
/**
* {@link EnableAutoConfiguration Auto-configuration} for a
* {@link CompositeMeterRegistry}.
*
* @author Andy Wilkinson
* @since 2.0.0
*/
@Import({ NoOpMeterRegistryConfiguration.class,
CompositeMeterRegistryConfiguration.class })
@ConditionalOnClass(CompositeMeterRegistry.class)
public class CompositeMeterRegistryAutoConfiguration {
}

@ -0,0 +1,66 @@
/*
* Copyright 2012-2018 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
*
* http://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;
import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryConfiguration.MultipleNonPrimaryMeterRegistriesCondition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.boot.autoconfigure.condition.NoneNestedConditions;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Primary;
/**
* Configuration for a {@link CompositeMeterRegistry}.
*
* @author Andy Wilkinson
*/
@Conditional(MultipleNonPrimaryMeterRegistriesCondition.class)
class CompositeMeterRegistryConfiguration {
@Bean
@Primary
public CompositeMeterRegistry compositeMeterRegistry(Clock clock,
List<MeterRegistry> registries) {
return new CompositeMeterRegistry(clock, registries);
}
static class MultipleNonPrimaryMeterRegistriesCondition extends NoneNestedConditions {
MultipleNonPrimaryMeterRegistriesCondition() {
super(ConfigurationPhase.REGISTER_BEAN);
}
@ConditionalOnMissingBean(MeterRegistry.class)
static class NoMeterRegistryCondition {
}
@ConditionalOnSingleCandidate(MeterRegistry.class)
static class SingleInjectableMeterRegistry {
}
}
}

@ -1,120 +0,0 @@
/*
* Copyright 2012-2018 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
*
* http://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.Arrays;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.ConstructorArgumentValues;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.util.ObjectUtils;
/**
* {@link BeanFactoryPostProcessor} to register a {@link CompositeMeterRegistry} when
* necessary.
*
* @author Jon Schneider
* @author Phillip Webb
*/
@Order(Ordered.LOWEST_PRECEDENCE - 1)
class CompositeMeterRegistryPostProcessor
implements BeanDefinitionRegistryPostProcessor, BeanFactoryAware {
private static final String COMPOSITE_BEAN_NAME = "compositeMeterRegistry";
private ConfigurableListableBeanFactory beanFactory;
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
if (beanFactory instanceof ConfigurableListableBeanFactory) {
this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
}
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
throws BeansException {
}
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry)
throws BeansException {
if (this.beanFactory == null) {
return;
}
String[] registryBeans = this.beanFactory.getBeanNamesForType(MeterRegistry.class,
true, false);
registerCompositeIfNecessary(registry, registryBeans);
}
private void registerCompositeIfNecessary(BeanDefinitionRegistry registry,
String[] registryBeans) {
if (ObjectUtils.isEmpty(registryBeans)) {
registerNoOpMeterRegistry(registry);
}
if (registryBeans.length > 1 && !hasPrimaryDefinition(registryBeans)) {
registerPrimaryCompositeMeterRegistry(registry, registryBeans);
}
}
private boolean hasPrimaryDefinition(String[] registryBeans) {
return Arrays.stream(registryBeans).map(this.beanFactory::getBeanDefinition)
.anyMatch(BeanDefinition::isPrimary);
}
private void registerNoOpMeterRegistry(BeanDefinitionRegistry registry) {
// If there are no meter registries configured, we register an empty composite
// that effectively no-ops metrics instrumentation throughout the app.
GenericBeanDefinition definition = new GenericBeanDefinition();
definition.setBeanClass(CompositeMeterRegistry.class);
registry.registerBeanDefinition(COMPOSITE_BEAN_NAME, definition);
}
private void registerPrimaryCompositeMeterRegistry(BeanDefinitionRegistry registry,
String[] registryBeans) {
GenericBeanDefinition definition = new GenericBeanDefinition();
definition.setBeanClass(CompositeMeterRegistry.class);
definition.setPrimary(true);
definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_CONSTRUCTOR);
ConstructorArgumentValues arguments = new ConstructorArgumentValues();
arguments.addIndexedArgumentValue(1, getBeanReferences(registryBeans));
definition.setConstructorArgumentValues(arguments);
registry.registerBeanDefinition(COMPOSITE_BEAN_NAME, definition);
}
private ManagedList<RuntimeBeanReference> getBeanReferences(String[] names) {
ManagedList<RuntimeBeanReference> references = new ManagedList<>(names.length);
Arrays.stream(names).map(RuntimeBeanReference::new).forEach(references::add);
return references;
}
}

@ -27,6 +27,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 org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@ -47,6 +48,7 @@ import org.springframework.core.annotation.Order;
@Configuration
@ConditionalOnClass(Timed.class)
@EnableConfigurationProperties(MetricsProperties.class)
@AutoConfigureBefore(CompositeMeterRegistryAutoConfiguration.class)
public class MetricsAutoConfiguration {
@Bean
@ -55,11 +57,6 @@ public class MetricsAutoConfiguration {
return Clock.SYSTEM;
}
@Bean
public static CompositeMeterRegistryPostProcessor compositeMeterRegistryPostProcessor() {
return new CompositeMeterRegistryPostProcessor();
}
@Bean
public static MeterRegistryPostProcessor meterRegistryPostProcessor(
ApplicationContext context) {

@ -37,7 +37,8 @@ import org.springframework.context.annotation.Configuration;
*/
@Configuration
@ConditionalOnClass(Timed.class)
@AutoConfigureAfter(MetricsAutoConfiguration.class)
@AutoConfigureAfter({ MetricsAutoConfiguration.class,
CompositeMeterRegistryAutoConfiguration.class })
public class MetricsEndpointAutoConfiguration {
@Bean

@ -0,0 +1,42 @@
/*
* Copyright 2012-2018 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
*
* http://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 io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
/**
* Configuration for a no-op meter registry when the context does not contain an
* auto-configured {@link MeterRegistry}.
*
* @author Andy Wilkinson
*/
@ConditionalOnBean(Clock.class)
@ConditionalOnMissingBean(MeterRegistry.class)
class NoOpMeterRegistryConfiguration {
@Bean
public CompositeMeterRegistry noOpMeterRegistry(Clock clock) {
return new CompositeMeterRegistry(clock);
}
}

@ -20,6 +20,7 @@ import com.netflix.spectator.atlas.AtlasConfig;
import io.micrometer.atlas.AtlasMeterRegistry;
import io.micrometer.core.instrument.Clock;
import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
@ -41,7 +42,8 @@ import org.springframework.context.annotation.Configuration;
* @since 2.0.0
*/
@Configuration
@AutoConfigureBefore(SimpleMetricsExportAutoConfiguration.class)
@AutoConfigureBefore({ CompositeMeterRegistryAutoConfiguration.class,
SimpleMetricsExportAutoConfiguration.class })
@AutoConfigureAfter(MetricsAutoConfiguration.class)
@ConditionalOnBean(Clock.class)
@ConditionalOnClass(AtlasMeterRegistry.class)

@ -20,6 +20,7 @@ import io.micrometer.core.instrument.Clock;
import io.micrometer.datadog.DatadogConfig;
import io.micrometer.datadog.DatadogMeterRegistry;
import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
@ -40,7 +41,8 @@ import org.springframework.context.annotation.Configuration;
* @since 2.0.0
*/
@Configuration
@AutoConfigureBefore(SimpleMetricsExportAutoConfiguration.class)
@AutoConfigureBefore({ CompositeMeterRegistryAutoConfiguration.class,
SimpleMetricsExportAutoConfiguration.class })
@AutoConfigureAfter(MetricsAutoConfiguration.class)
@ConditionalOnBean(Clock.class)
@ConditionalOnClass(DatadogMeterRegistry.class)

@ -21,6 +21,7 @@ import io.micrometer.core.instrument.util.HierarchicalNameMapper;
import io.micrometer.ganglia.GangliaConfig;
import io.micrometer.ganglia.GangliaMeterRegistry;
import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
@ -41,7 +42,8 @@ import org.springframework.context.annotation.Configuration;
* @since 2.0.0
*/
@Configuration
@AutoConfigureBefore(SimpleMetricsExportAutoConfiguration.class)
@AutoConfigureBefore({ CompositeMeterRegistryAutoConfiguration.class,
SimpleMetricsExportAutoConfiguration.class })
@AutoConfigureAfter(MetricsAutoConfiguration.class)
@ConditionalOnBean(Clock.class)
@ConditionalOnClass(GangliaMeterRegistry.class)

@ -21,6 +21,7 @@ import io.micrometer.core.instrument.util.HierarchicalNameMapper;
import io.micrometer.graphite.GraphiteConfig;
import io.micrometer.graphite.GraphiteMeterRegistry;
import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
@ -41,7 +42,8 @@ import org.springframework.context.annotation.Configuration;
* @since 2.0.0
*/
@Configuration
@AutoConfigureBefore(SimpleMetricsExportAutoConfiguration.class)
@AutoConfigureBefore({ CompositeMeterRegistryAutoConfiguration.class,
SimpleMetricsExportAutoConfiguration.class })
@AutoConfigureAfter(MetricsAutoConfiguration.class)
@ConditionalOnBean(Clock.class)
@ConditionalOnClass(GraphiteMeterRegistry.class)

@ -20,6 +20,7 @@ import io.micrometer.core.instrument.Clock;
import io.micrometer.influx.InfluxConfig;
import io.micrometer.influx.InfluxMeterRegistry;
import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
@ -40,7 +41,8 @@ import org.springframework.context.annotation.Configuration;
* @since 2.0.0
*/
@Configuration
@AutoConfigureBefore(SimpleMetricsExportAutoConfiguration.class)
@AutoConfigureBefore({ CompositeMeterRegistryAutoConfiguration.class,
SimpleMetricsExportAutoConfiguration.class })
@AutoConfigureAfter(MetricsAutoConfiguration.class)
@ConditionalOnBean(Clock.class)
@ConditionalOnClass(InfluxMeterRegistry.class)

@ -21,6 +21,7 @@ import io.micrometer.core.instrument.util.HierarchicalNameMapper;
import io.micrometer.jmx.JmxConfig;
import io.micrometer.jmx.JmxMeterRegistry;
import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
@ -41,7 +42,8 @@ import org.springframework.context.annotation.Configuration;
* @since 2.0.0
*/
@Configuration
@AutoConfigureBefore(SimpleMetricsExportAutoConfiguration.class)
@AutoConfigureBefore({ CompositeMeterRegistryAutoConfiguration.class,
SimpleMetricsExportAutoConfiguration.class })
@AutoConfigureAfter(MetricsAutoConfiguration.class)
@ConditionalOnBean(Clock.class)
@ConditionalOnClass(JmxMeterRegistry.class)

@ -20,6 +20,7 @@ import io.micrometer.core.instrument.Clock;
import io.micrometer.newrelic.NewRelicConfig;
import io.micrometer.newrelic.NewRelicMeterRegistry;
import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
@ -41,7 +42,8 @@ import org.springframework.context.annotation.Configuration;
* @since 2.0.0
*/
@Configuration
@AutoConfigureBefore(SimpleMetricsExportAutoConfiguration.class)
@AutoConfigureBefore({ CompositeMeterRegistryAutoConfiguration.class,
SimpleMetricsExportAutoConfiguration.class })
@AutoConfigureAfter(MetricsAutoConfiguration.class)
@ConditionalOnBean(Clock.class)
@ConditionalOnClass(NewRelicMeterRegistry.class)

@ -22,6 +22,7 @@ import io.micrometer.prometheus.PrometheusMeterRegistry;
import io.prometheus.client.CollectorRegistry;
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration;
@ -44,7 +45,8 @@ import org.springframework.context.annotation.Configuration;
* @author Jon Schneider
*/
@Configuration
@AutoConfigureBefore(SimpleMetricsExportAutoConfiguration.class)
@AutoConfigureBefore({ CompositeMeterRegistryAutoConfiguration.class,
SimpleMetricsExportAutoConfiguration.class })
@AutoConfigureAfter(MetricsAutoConfiguration.class)
@ConditionalOnBean(Clock.class)
@ConditionalOnClass(PrometheusMeterRegistry.class)

@ -20,6 +20,7 @@ import io.micrometer.core.instrument.Clock;
import io.micrometer.signalfx.SignalFxConfig;
import io.micrometer.signalfx.SignalFxMeterRegistry;
import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
@ -41,7 +42,8 @@ import org.springframework.context.annotation.Configuration;
* @since 2.0.0
*/
@Configuration
@AutoConfigureBefore(SimpleMetricsExportAutoConfiguration.class)
@AutoConfigureBefore({ CompositeMeterRegistryAutoConfiguration.class,
SimpleMetricsExportAutoConfiguration.class })
@AutoConfigureAfter(MetricsAutoConfiguration.class)
@ConditionalOnBean(Clock.class)
@ConditionalOnClass(SignalFxMeterRegistry.class)

@ -21,8 +21,8 @@ import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.simple.SimpleConfig;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsEndpointAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@ -43,7 +43,7 @@ import org.springframework.context.annotation.Configuration;
*/
@Configuration
@AutoConfigureAfter(MetricsAutoConfiguration.class)
@AutoConfigureBefore(MetricsEndpointAutoConfiguration.class)
@AutoConfigureBefore(CompositeMeterRegistryAutoConfiguration.class)
@ConditionalOnBean(Clock.class)
@EnableConfigurationProperties(SimpleProperties.class)
@ConditionalOnMissingBean(MeterRegistry.class)

@ -21,6 +21,7 @@ import io.micrometer.core.instrument.util.HierarchicalNameMapper;
import io.micrometer.statsd.StatsdConfig;
import io.micrometer.statsd.StatsdMeterRegistry;
import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
@ -41,7 +42,8 @@ import org.springframework.context.annotation.Configuration;
* @since 2.0.0
*/
@Configuration
@AutoConfigureBefore(SimpleMetricsExportAutoConfiguration.class)
@AutoConfigureBefore({ CompositeMeterRegistryAutoConfiguration.class,
SimpleMetricsExportAutoConfiguration.class })
@AutoConfigureAfter(MetricsAutoConfiguration.class)
@ConditionalOnBean(Clock.class)
@ConditionalOnClass(StatsdMeterRegistry.class)

@ -20,6 +20,7 @@ import io.micrometer.core.instrument.Clock;
import io.micrometer.wavefront.WavefrontConfig;
import io.micrometer.wavefront.WavefrontMeterRegistry;
import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
@ -40,7 +41,8 @@ import org.springframework.context.annotation.Configuration;
* @since 2.0.0
*/
@Configuration
@AutoConfigureBefore(SimpleMetricsExportAutoConfiguration.class)
@AutoConfigureBefore({ CompositeMeterRegistryAutoConfiguration.class,
SimpleMetricsExportAutoConfiguration.class })
@AutoConfigureAfter(MetricsAutoConfiguration.class)
@ConditionalOnBean(Clock.class)
@ConditionalOnClass(WavefrontMeterRegistry.class)

@ -32,6 +32,7 @@ org.springframework.boot.actuate.autoconfigure.logging.LoggersEndpointAutoConfig
org.springframework.boot.actuate.autoconfigure.mail.MailHealthIndicatorAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.management.HeapDumpWebEndpointAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.management.ThreadDumpEndpointAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.metrics.MetricsEndpointAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.metrics.amqp.RabbitMetricsAutoConfiguration,\

@ -22,31 +22,35 @@ import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link CompositeMeterRegistryPostProcessor}.
* Tests for {@link CompositeMeterRegistryAutoConfiguration}.
*
* @author Phillip Webb
* @author Andy Wilkinson
*/
public class CompositeMeterRegistryPostProcessorTests {
public class CompositeMeterRegistryAutoConfigurationTests {
private static final String COMPOSITE_NAME = "compositeMeterRegistry";
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withUserConfiguration(BaseConfig.class);
.withUserConfiguration(BaseConfig.class).withConfiguration(
AutoConfigurations.of(CompositeMeterRegistryAutoConfiguration.class));
@Test
public void registerWhenHasNoMeterRegistryShouldRegisterEmptyComposite() {
public void registerWhenHasNoMeterRegistryShouldRegisterEmptyNoOpComposite() {
this.contextRunner.withUserConfiguration(NoMeterRegistryConfig.class)
.run((context) -> {
assertThat(context).hasSingleBean(MeterRegistry.class);
CompositeMeterRegistry registry = context.getBean(COMPOSITE_NAME,
CompositeMeterRegistry registry = context.getBean("noOpMeterRegistry",
CompositeMeterRegistry.class);
assertThat(registry.getRegistries()).isEmpty();
});
@ -79,17 +83,19 @@ public class CompositeMeterRegistryPostProcessorTests {
@Test
public void registerWhenHasMultipleRegistriesAndOneIsPrimaryShouldDoNothing() {
this.contextRunner
.withUserConfiguration(MultipleMeterRegistriesWithOnePrimaryConfig.class)
.run((context) -> {
assertThat(context.getBeansOfType(MeterRegistry.class)).hasSize(2)
.containsKeys("meterRegistryOne", "meterRegistryTwo");
MeterRegistry primary = context.getBean(MeterRegistry.class);
assertThat(primary).isInstanceOf(TestMeterRegistry.class);
});
}
@Configuration
static class BaseConfig {
@Bean
public CompositeMeterRegistryPostProcessor compositeMeterRegistryPostProcessor() {
return new CompositeMeterRegistryPostProcessor();
}
@Bean
@ConditionalOnMissingBean
public Clock micrometerClock() {
@ -128,6 +134,22 @@ public class CompositeMeterRegistryPostProcessorTests {
}
@Configuration
static class MultipleMeterRegistriesWithOnePrimaryConfig {
@Bean
@Primary
public MeterRegistry meterRegistryOne() {
return new TestMeterRegistry();
}
@Bean
public MeterRegistry meterRegistryTwo() {
return new SimpleMeterRegistry();
}
}
static class TestMeterRegistry extends SimpleMeterRegistry {
}

@ -29,7 +29,6 @@ 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.composite.CompositeMeterRegistry;
import io.micrometer.core.instrument.config.MeterFilter;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.junit.Test;
@ -67,15 +66,6 @@ public class MetricsAutoConfigurationTests {
.hasBean("customClock"));
}
@Test
public void autoConfiguresACompositeMeterRegistry() {
this.contextRunner.run((context) -> {
assertThat(context).hasSingleBean(CompositeMeterRegistry.class);
assertThat(context.getBean(CompositeMeterRegistry.class).getRegistries())
.isEmpty();
});
}
@SuppressWarnings("unchecked")
@Test
public void configuresMeterRegistries() {

@ -21,6 +21,7 @@ import java.util.LinkedHashSet;
import java.util.Set;
import java.util.function.Function;
import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.amqp.RabbitMetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.cache.CacheMetricsAutoConfiguration;
@ -71,8 +72,8 @@ public final class MetricsRun {
}
private static final AutoConfigurations AUTO_CONFIGURATIONS = AutoConfigurations.of(
MetricsAutoConfiguration.class, RabbitMetricsAutoConfiguration.class,
CacheMetricsAutoConfiguration.class,
MetricsAutoConfiguration.class, CompositeMeterRegistryAutoConfiguration.class,
RabbitMetricsAutoConfiguration.class, CacheMetricsAutoConfiguration.class,
DataSourcePoolMetricsAutoConfiguration.class,
RestTemplateMetricsAutoConfiguration.class,
WebFluxMetricsAutoConfiguration.class, WebMvcMetricsAutoConfiguration.class);

Loading…
Cancel
Save