From cc3d7648c1042200f7d321bbad0099ee1e607cfc Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 9 Feb 2018 15:25:44 +0000 Subject: [PATCH] Auto-configure Micrometer's Clock in a single place Closes gh-11977 --- .../metrics/MetricsAutoConfiguration.java | 7 ++++++ .../AtlasMetricsExportAutoConfiguration.java | 6 ----- ...DatadogMetricsExportAutoConfiguration.java | 6 ----- ...GangliaMetricsExportAutoConfiguration.java | 6 ----- ...raphiteMetricsExportAutoConfiguration.java | 6 ----- .../InfluxMetricsExportAutoConfiguration.java | 6 ----- .../JmxMetricsExportAutoConfiguration.java | 6 ----- ...metheusMetricsExportAutoConfiguration.java | 6 ----- .../SimpleMetricsExportAutoConfiguration.java | 6 ----- .../StatsdMetricsExportAutoConfiguration.java | 6 ----- .../MetricsAutoConfigurationTests.java | 25 +++++++++++++++++++ 11 files changed, 32 insertions(+), 54 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/MetricsAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/MetricsAutoConfiguration.java index d18e83b0c0..0c162af9a5 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/MetricsAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/MetricsAutoConfiguration.java @@ -17,6 +17,7 @@ package org.springframework.boot.actuate.autoconfigure.metrics; 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.JvmMemoryMetrics; import io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics; @@ -50,6 +51,12 @@ import org.springframework.integration.support.management.IntegrationManagementC @EnableConfigurationProperties(MetricsProperties.class) public class MetricsAutoConfiguration { + @Bean + @ConditionalOnMissingBean + public Clock micrometerClock() { + return Clock.SYSTEM; + } + @Bean public static CompositeMeterRegistryPostProcessor compositeMeterRegistryPostProcessor() { return new CompositeMeterRegistryPostProcessor(); diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/atlas/AtlasMetricsExportAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/atlas/AtlasMetricsExportAutoConfiguration.java index 9964ae69f8..d45b03ccb8 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/atlas/AtlasMetricsExportAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/atlas/AtlasMetricsExportAutoConfiguration.java @@ -44,12 +44,6 @@ import org.springframework.context.annotation.Configuration; @EnableConfigurationProperties(AtlasProperties.class) public class AtlasMetricsExportAutoConfiguration { - @Bean - @ConditionalOnMissingBean - public Clock micrometerClock() { - return Clock.SYSTEM; - } - @Bean @ConditionalOnMissingBean(AtlasConfig.class) public AtlasConfig atlasConfig(AtlasProperties atlasProperties) { diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/datadog/DatadogMetricsExportAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/datadog/DatadogMetricsExportAutoConfiguration.java index c55b847e6d..1300643a14 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/datadog/DatadogMetricsExportAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/datadog/DatadogMetricsExportAutoConfiguration.java @@ -45,12 +45,6 @@ import org.springframework.context.annotation.Configuration; @EnableConfigurationProperties(DatadogProperties.class) public class DatadogMetricsExportAutoConfiguration { - @Bean - @ConditionalOnMissingBean - public Clock micrometerClock() { - return Clock.SYSTEM; - } - @Bean @ConditionalOnMissingBean public DatadogConfig datadogConfig(DatadogProperties datadogProperties) { diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/ganglia/GangliaMetricsExportAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/ganglia/GangliaMetricsExportAutoConfiguration.java index d68d53041e..c60d041805 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/ganglia/GangliaMetricsExportAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/ganglia/GangliaMetricsExportAutoConfiguration.java @@ -44,12 +44,6 @@ import org.springframework.context.annotation.Configuration; @EnableConfigurationProperties(GangliaProperties.class) public class GangliaMetricsExportAutoConfiguration { - @Bean - @ConditionalOnMissingBean - public Clock micrometerClock() { - return Clock.SYSTEM; - } - @Bean @ConditionalOnMissingBean public GangliaConfig gangliaConfig(GangliaProperties gangliaProperties) { diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/graphite/GraphiteMetricsExportAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/graphite/GraphiteMetricsExportAutoConfiguration.java index 4652145e9c..79aed49797 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/graphite/GraphiteMetricsExportAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/graphite/GraphiteMetricsExportAutoConfiguration.java @@ -44,12 +44,6 @@ import org.springframework.context.annotation.Configuration; @EnableConfigurationProperties(GraphiteProperties.class) public class GraphiteMetricsExportAutoConfiguration { - @Bean - @ConditionalOnMissingBean - public Clock micrometerClock() { - return Clock.SYSTEM; - } - @Bean @ConditionalOnMissingBean public GraphiteConfig graphiteConfig(GraphiteProperties graphiteProperties) { diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/influx/InfluxMetricsExportAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/influx/InfluxMetricsExportAutoConfiguration.java index cccbebb3aa..b68872992c 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/influx/InfluxMetricsExportAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/influx/InfluxMetricsExportAutoConfiguration.java @@ -43,12 +43,6 @@ import org.springframework.context.annotation.Configuration; @EnableConfigurationProperties(InfluxProperties.class) public class InfluxMetricsExportAutoConfiguration { - @Bean - @ConditionalOnMissingBean - public Clock micrometerClock() { - return Clock.SYSTEM; - } - @Bean @ConditionalOnMissingBean(InfluxConfig.class) public InfluxConfig influxConfig(InfluxProperties influxProperties) { diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/jmx/JmxMetricsExportAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/jmx/JmxMetricsExportAutoConfiguration.java index 29b45d1bad..239484393d 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/jmx/JmxMetricsExportAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/jmx/JmxMetricsExportAutoConfiguration.java @@ -44,12 +44,6 @@ import org.springframework.context.annotation.Configuration; @EnableConfigurationProperties(JmxProperties.class) public class JmxMetricsExportAutoConfiguration { - @Bean - @ConditionalOnMissingBean - public Clock micrometerClock() { - return Clock.SYSTEM; - } - @Bean @ConditionalOnMissingBean public JmxConfig jmxConfig(JmxProperties jmxProperties) { diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfiguration.java index 6a5b0eabea..e6a02411c2 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfiguration.java @@ -46,12 +46,6 @@ import org.springframework.context.annotation.Configuration; @EnableConfigurationProperties(PrometheusProperties.class) public class PrometheusMetricsExportAutoConfiguration { - @Bean - @ConditionalOnMissingBean - public Clock micrometerClock() { - return Clock.SYSTEM; - } - @Bean @ConditionalOnMissingBean public PrometheusConfig prometheusConfig(PrometheusProperties prometheusProperties) { diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/simple/SimpleMetricsExportAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/simple/SimpleMetricsExportAutoConfiguration.java index e991bc7b2e..c4c1f62cfa 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/simple/SimpleMetricsExportAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/simple/SimpleMetricsExportAutoConfiguration.java @@ -46,12 +46,6 @@ import org.springframework.context.annotation.Configuration; @ConditionalOnMissingBean(MeterRegistry.class) public class SimpleMetricsExportAutoConfiguration { - @Bean - @ConditionalOnMissingBean - public Clock micrometerClock() { - return Clock.SYSTEM; - } - @Bean @ConditionalOnMissingBean(MeterRegistry.class) public SimpleMeterRegistry simpleMeterRegistry(SimpleConfig config, Clock clock) { diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdMetricsExportAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdMetricsExportAutoConfiguration.java index 274a16568c..2943d6eaa7 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdMetricsExportAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdMetricsExportAutoConfiguration.java @@ -44,12 +44,6 @@ import org.springframework.context.annotation.Configuration; @EnableConfigurationProperties(StatsdProperties.class) public class StatsdMetricsExportAutoConfiguration { - @Bean - @ConditionalOnMissingBean - public Clock micrometerClock() { - return Clock.SYSTEM; - } - @Bean @ConditionalOnMissingBean(StatsdConfig.class) public StatsdConfig statsdConfig(StatsdProperties statsdProperties) { diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/MetricsAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/MetricsAutoConfigurationTests.java index 78a85f5001..dc524c5784 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/MetricsAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/MetricsAutoConfigurationTests.java @@ -20,6 +20,7 @@ import java.util.UUID; import javax.sql.DataSource; +import io.micrometer.core.instrument.Clock; import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.MockClock; 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 static class PrimarySimpleMeterRegistryConfiguration { @@ -211,4 +226,14 @@ public class MetricsAutoConfigurationTests { } + @Configuration + static class CustomClockConfiguration { + + @Bean + public Clock customClock() { + return Clock.SYSTEM; + } + + } + }