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 1beceb70c0..644d4425c4 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 @@ -18,6 +18,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.ClassLoaderMetrics; import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics; import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics; import io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics; @@ -92,6 +93,12 @@ public class MetricsAutoConfiguration { return new JvmThreadMetrics(); } + @Bean + @ConditionalOnMissingBean + public ClassLoaderMetrics classLoaderMetrics() { + return new ClassLoaderMetrics(); + } + } @Configuration 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 5ace579ba7..5c0b8b8f67 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 @@ -21,6 +21,7 @@ import java.util.List; import io.micrometer.core.instrument.Clock; import io.micrometer.core.instrument.MeterRegistry; 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; import io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics; @@ -94,7 +95,8 @@ public class MetricsAutoConfigurationTests { public void autoConfiguresJvmMetrics() { this.runner.run((context) -> assertThat(context).hasSingleBean(JvmGcMetrics.class) .hasSingleBean(JvmMemoryMetrics.class) - .hasSingleBean(JvmThreadMetrics.class)); + .hasSingleBean(JvmThreadMetrics.class) + .hasSingleBean(ClassLoaderMetrics.class)); } @Test @@ -102,7 +104,8 @@ public class MetricsAutoConfigurationTests { this.runner.withPropertyValues("management.metrics.binders.jvm.enabled=false") .run((context) -> assertThat(context).doesNotHaveBean(JvmGcMetrics.class) .doesNotHaveBean(JvmMemoryMetrics.class) - .doesNotHaveBean(JvmThreadMetrics.class)); + .doesNotHaveBean(JvmThreadMetrics.class) + .doesNotHaveBean(ClassLoaderMetrics.class)); } @Test @@ -111,7 +114,8 @@ public class MetricsAutoConfigurationTests { .run((context) -> assertThat(context).hasSingleBean(JvmGcMetrics.class) .hasBean("customJvmGcMetrics") .hasSingleBean(JvmMemoryMetrics.class) - .hasSingleBean(JvmThreadMetrics.class)); + .hasSingleBean(JvmThreadMetrics.class) + .hasSingleBean(ClassLoaderMetrics.class)); } @Test @@ -120,7 +124,8 @@ public class MetricsAutoConfigurationTests { .run((context) -> assertThat(context).hasSingleBean(JvmGcMetrics.class) .hasSingleBean(JvmMemoryMetrics.class) .hasBean("customJvmMemoryMetrics") - .hasSingleBean(JvmThreadMetrics.class)); + .hasSingleBean(JvmThreadMetrics.class) + .hasSingleBean(ClassLoaderMetrics.class)); } @Test @@ -129,9 +134,20 @@ public class MetricsAutoConfigurationTests { .run((context) -> assertThat(context).hasSingleBean(JvmGcMetrics.class) .hasSingleBean(JvmMemoryMetrics.class) .hasSingleBean(JvmThreadMetrics.class) + .hasSingleBean(ClassLoaderMetrics.class) .hasBean("customJvmThreadMetrics")); } + @Test + public void allowsCustomClassLoaderMetricsToBeUsed() { + this.runner.withUserConfiguration(CustomClassLoaderMetricsConfiguration.class) + .run((context) -> assertThat(context).hasSingleBean(JvmGcMetrics.class) + .hasSingleBean(JvmMemoryMetrics.class) + .hasSingleBean(JvmThreadMetrics.class) + .hasSingleBean(ClassLoaderMetrics.class) + .hasBean("customClassLoaderMetrics")); + } + @Test public void autoConfiguresLogbackMetrics() { this.runner.run( @@ -256,6 +272,16 @@ public class MetricsAutoConfigurationTests { } + @Configuration + static class CustomClassLoaderMetricsConfiguration { + + @Bean + ClassLoaderMetrics customClassLoaderMetrics() { + return new ClassLoaderMetrics(); + } + + } + @Configuration static class CustomLogbackMetricsConfiguration {