Auto-configure Micrometer's Clock in a single place

Closes gh-11977
pull/11967/merge
Andy Wilkinson 7 years ago
parent 2c40383524
commit cc3d7648c1

@ -17,6 +17,7 @@
package org.springframework.boot.actuate.autoconfigure.metrics; package org.springframework.boot.actuate.autoconfigure.metrics;
import io.micrometer.core.annotation.Timed; import io.micrometer.core.annotation.Timed;
import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics; import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics;
import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics; import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics;
import io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics; import io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics;
@ -50,6 +51,12 @@ import org.springframework.integration.support.management.IntegrationManagementC
@EnableConfigurationProperties(MetricsProperties.class) @EnableConfigurationProperties(MetricsProperties.class)
public class MetricsAutoConfiguration { public class MetricsAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public Clock micrometerClock() {
return Clock.SYSTEM;
}
@Bean @Bean
public static CompositeMeterRegistryPostProcessor compositeMeterRegistryPostProcessor() { public static CompositeMeterRegistryPostProcessor compositeMeterRegistryPostProcessor() {
return new CompositeMeterRegistryPostProcessor(); return new CompositeMeterRegistryPostProcessor();

@ -44,12 +44,6 @@ import org.springframework.context.annotation.Configuration;
@EnableConfigurationProperties(AtlasProperties.class) @EnableConfigurationProperties(AtlasProperties.class)
public class AtlasMetricsExportAutoConfiguration { public class AtlasMetricsExportAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public Clock micrometerClock() {
return Clock.SYSTEM;
}
@Bean @Bean
@ConditionalOnMissingBean(AtlasConfig.class) @ConditionalOnMissingBean(AtlasConfig.class)
public AtlasConfig atlasConfig(AtlasProperties atlasProperties) { public AtlasConfig atlasConfig(AtlasProperties atlasProperties) {

@ -45,12 +45,6 @@ import org.springframework.context.annotation.Configuration;
@EnableConfigurationProperties(DatadogProperties.class) @EnableConfigurationProperties(DatadogProperties.class)
public class DatadogMetricsExportAutoConfiguration { public class DatadogMetricsExportAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public Clock micrometerClock() {
return Clock.SYSTEM;
}
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public DatadogConfig datadogConfig(DatadogProperties datadogProperties) { public DatadogConfig datadogConfig(DatadogProperties datadogProperties) {

@ -44,12 +44,6 @@ import org.springframework.context.annotation.Configuration;
@EnableConfigurationProperties(GangliaProperties.class) @EnableConfigurationProperties(GangliaProperties.class)
public class GangliaMetricsExportAutoConfiguration { public class GangliaMetricsExportAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public Clock micrometerClock() {
return Clock.SYSTEM;
}
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public GangliaConfig gangliaConfig(GangliaProperties gangliaProperties) { public GangliaConfig gangliaConfig(GangliaProperties gangliaProperties) {

@ -44,12 +44,6 @@ import org.springframework.context.annotation.Configuration;
@EnableConfigurationProperties(GraphiteProperties.class) @EnableConfigurationProperties(GraphiteProperties.class)
public class GraphiteMetricsExportAutoConfiguration { public class GraphiteMetricsExportAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public Clock micrometerClock() {
return Clock.SYSTEM;
}
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public GraphiteConfig graphiteConfig(GraphiteProperties graphiteProperties) { public GraphiteConfig graphiteConfig(GraphiteProperties graphiteProperties) {

@ -43,12 +43,6 @@ import org.springframework.context.annotation.Configuration;
@EnableConfigurationProperties(InfluxProperties.class) @EnableConfigurationProperties(InfluxProperties.class)
public class InfluxMetricsExportAutoConfiguration { public class InfluxMetricsExportAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public Clock micrometerClock() {
return Clock.SYSTEM;
}
@Bean @Bean
@ConditionalOnMissingBean(InfluxConfig.class) @ConditionalOnMissingBean(InfluxConfig.class)
public InfluxConfig influxConfig(InfluxProperties influxProperties) { public InfluxConfig influxConfig(InfluxProperties influxProperties) {

@ -44,12 +44,6 @@ import org.springframework.context.annotation.Configuration;
@EnableConfigurationProperties(JmxProperties.class) @EnableConfigurationProperties(JmxProperties.class)
public class JmxMetricsExportAutoConfiguration { public class JmxMetricsExportAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public Clock micrometerClock() {
return Clock.SYSTEM;
}
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public JmxConfig jmxConfig(JmxProperties jmxProperties) { public JmxConfig jmxConfig(JmxProperties jmxProperties) {

@ -46,12 +46,6 @@ import org.springframework.context.annotation.Configuration;
@EnableConfigurationProperties(PrometheusProperties.class) @EnableConfigurationProperties(PrometheusProperties.class)
public class PrometheusMetricsExportAutoConfiguration { public class PrometheusMetricsExportAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public Clock micrometerClock() {
return Clock.SYSTEM;
}
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public PrometheusConfig prometheusConfig(PrometheusProperties prometheusProperties) { public PrometheusConfig prometheusConfig(PrometheusProperties prometheusProperties) {

@ -46,12 +46,6 @@ import org.springframework.context.annotation.Configuration;
@ConditionalOnMissingBean(MeterRegistry.class) @ConditionalOnMissingBean(MeterRegistry.class)
public class SimpleMetricsExportAutoConfiguration { public class SimpleMetricsExportAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public Clock micrometerClock() {
return Clock.SYSTEM;
}
@Bean @Bean
@ConditionalOnMissingBean(MeterRegistry.class) @ConditionalOnMissingBean(MeterRegistry.class)
public SimpleMeterRegistry simpleMeterRegistry(SimpleConfig config, Clock clock) { public SimpleMeterRegistry simpleMeterRegistry(SimpleConfig config, Clock clock) {

@ -44,12 +44,6 @@ import org.springframework.context.annotation.Configuration;
@EnableConfigurationProperties(StatsdProperties.class) @EnableConfigurationProperties(StatsdProperties.class)
public class StatsdMetricsExportAutoConfiguration { public class StatsdMetricsExportAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public Clock micrometerClock() {
return Clock.SYSTEM;
}
@Bean @Bean
@ConditionalOnMissingBean(StatsdConfig.class) @ConditionalOnMissingBean(StatsdConfig.class)
public StatsdConfig statsdConfig(StatsdProperties statsdProperties) { public StatsdConfig statsdConfig(StatsdProperties statsdProperties) {

@ -20,6 +20,7 @@ import java.util.UUID;
import javax.sql.DataSource; import javax.sql.DataSource;
import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.MockClock; import io.micrometer.core.instrument.MockClock;
import io.micrometer.core.instrument.composite.CompositeMeterRegistry; import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
@ -180,6 +181,20 @@ public class MetricsAutoConfigurationTests {
}); });
} }
@Test
public void autoConfiguresAClock() {
this.contextRunner.with(MetricsRun.limitedTo())
.run((context) -> assertThat(context).hasSingleBean(Clock.class));
}
@Test
public void allowsCustomClockToBeConfigured() {
this.contextRunner.with(MetricsRun.limitedTo())
.withUserConfiguration(CustomClockConfiguration.class)
.run((context) -> assertThat(context).hasSingleBean(Clock.class)
.hasBean("customClock"));
}
@Configuration @Configuration
static class PrimarySimpleMeterRegistryConfiguration { static class PrimarySimpleMeterRegistryConfiguration {
@ -211,4 +226,14 @@ public class MetricsAutoConfigurationTests {
} }
@Configuration
static class CustomClockConfiguration {
@Bean
public Clock customClock() {
return Clock.SYSTEM;
}
}
} }

Loading…
Cancel
Save