From e3f03dd50a03dbfb269220285f752e22d1af6fb1 Mon Sep 17 00:00:00 2001 From: bono007 Date: Sat, 10 Apr 2021 23:06:09 -0500 Subject: [PATCH] Add auto-configuration for DiskSpaceMetrics See gh-26001 --- .../metrics/JvmMetricsAutoConfiguration.java | 9 +++++ .../JvmMetricsAutoConfigurationTests.java | 40 +++++++++++++++---- .../src/docs/asciidoc/actuator/metrics.adoc | 3 +- 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/JvmMetricsAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/JvmMetricsAutoConfiguration.java index 629e3dc8f3..ea46bc95d2 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/JvmMetricsAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/JvmMetricsAutoConfiguration.java @@ -16,8 +16,11 @@ package org.springframework.boot.actuate.autoconfigure.metrics; +import java.io.File; + import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.binder.jvm.ClassLoaderMetrics; +import io.micrometer.core.instrument.binder.jvm.DiskSpaceMetrics; import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics; import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics; import io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics; @@ -66,4 +69,10 @@ public class JvmMetricsAutoConfiguration { return new ClassLoaderMetrics(); } + @Bean + @ConditionalOnMissingBean + public DiskSpaceMetrics diskSpaceMetrics() { + return new DiskSpaceMetrics(new File(".")); + } + } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/JvmMetricsAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/JvmMetricsAutoConfigurationTests.java index 72c39c9d34..294129b914 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/JvmMetricsAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/JvmMetricsAutoConfigurationTests.java @@ -16,7 +16,10 @@ package org.springframework.boot.actuate.autoconfigure.metrics; +import java.io.File; + import io.micrometer.core.instrument.binder.jvm.ClassLoaderMetrics; +import io.micrometer.core.instrument.binder.jvm.DiskSpaceMetrics; import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics; import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics; import io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics; @@ -35,6 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Andy Wilkinson * @author Stephane Nicoll + * @author Chris Bono */ class JvmMetricsAutoConfigurationTests { @@ -43,9 +47,9 @@ class JvmMetricsAutoConfigurationTests { @Test void autoConfiguresJvmMetrics() { - this.contextRunner.run( - (context) -> assertThat(context).hasSingleBean(JvmGcMetrics.class).hasSingleBean(JvmMemoryMetrics.class) - .hasSingleBean(JvmThreadMetrics.class).hasSingleBean(ClassLoaderMetrics.class)); + this.contextRunner.run((context) -> assertThat(context).hasSingleBean(JvmGcMetrics.class) + .hasSingleBean(JvmMemoryMetrics.class).hasSingleBean(JvmThreadMetrics.class) + .hasSingleBean(ClassLoaderMetrics.class).hasSingleBean(DiskSpaceMetrics.class)); } @Test @@ -53,7 +57,7 @@ class JvmMetricsAutoConfigurationTests { this.contextRunner.withUserConfiguration(CustomJvmGcMetricsConfiguration.class) .run((context) -> assertThat(context).hasSingleBean(JvmGcMetrics.class).hasBean("customJvmGcMetrics") .hasSingleBean(JvmMemoryMetrics.class).hasSingleBean(JvmThreadMetrics.class) - .hasSingleBean(ClassLoaderMetrics.class)); + .hasSingleBean(ClassLoaderMetrics.class).hasSingleBean(DiskSpaceMetrics.class)); } @Test @@ -61,7 +65,8 @@ class JvmMetricsAutoConfigurationTests { this.contextRunner.withUserConfiguration(CustomJvmMemoryMetricsConfiguration.class) .run((context) -> assertThat(context).hasSingleBean(JvmGcMetrics.class) .hasSingleBean(JvmMemoryMetrics.class).hasBean("customJvmMemoryMetrics") - .hasSingleBean(JvmThreadMetrics.class).hasSingleBean(ClassLoaderMetrics.class)); + .hasSingleBean(JvmThreadMetrics.class).hasSingleBean(ClassLoaderMetrics.class) + .hasSingleBean(DiskSpaceMetrics.class)); } @Test @@ -69,7 +74,8 @@ class JvmMetricsAutoConfigurationTests { this.contextRunner.withUserConfiguration(CustomJvmThreadMetricsConfiguration.class) .run((context) -> assertThat(context).hasSingleBean(JvmGcMetrics.class) .hasSingleBean(JvmMemoryMetrics.class).hasSingleBean(JvmThreadMetrics.class) - .hasSingleBean(ClassLoaderMetrics.class).hasBean("customJvmThreadMetrics")); + .hasBean("customJvmThreadMetrics").hasSingleBean(ClassLoaderMetrics.class) + .hasSingleBean(DiskSpaceMetrics.class)); } @Test @@ -77,7 +83,17 @@ class JvmMetricsAutoConfigurationTests { this.contextRunner.withUserConfiguration(CustomClassLoaderMetricsConfiguration.class) .run((context) -> assertThat(context).hasSingleBean(JvmGcMetrics.class) .hasSingleBean(JvmMemoryMetrics.class).hasSingleBean(JvmThreadMetrics.class) - .hasSingleBean(ClassLoaderMetrics.class).hasBean("customClassLoaderMetrics")); + .hasSingleBean(ClassLoaderMetrics.class).hasBean("customClassLoaderMetrics") + .hasSingleBean(DiskSpaceMetrics.class)); + } + + @Test + void allowsCustomDiskSpaceMetricsToBeUsed() { + this.contextRunner.withUserConfiguration(CustomDiskSpaceMetricsConfiguration.class) + .run((context) -> assertThat(context).hasSingleBean(JvmGcMetrics.class) + .hasSingleBean(JvmMemoryMetrics.class).hasSingleBean(JvmThreadMetrics.class) + .hasSingleBean(ClassLoaderMetrics.class).hasSingleBean(DiskSpaceMetrics.class) + .hasBean("customDiskSpaceMetrics")); } @Configuration(proxyBeanMethods = false) @@ -120,4 +136,14 @@ class JvmMetricsAutoConfigurationTests { } + @Configuration(proxyBeanMethods = false) + static class CustomDiskSpaceMetricsConfiguration { + + @Bean + DiskSpaceMetrics customDiskSpaceMetrics() { + return new DiskSpaceMetrics(new File(System.getProperty("user.dir"))); + } + + } + } diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/actuator/metrics.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/actuator/metrics.adoc index 9692532be3..47765a8460 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/actuator/metrics.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/actuator/metrics.adoc @@ -554,7 +554,7 @@ In most situations, the out-of-the-box defaults will provide sensible metrics th [[actuator.metrics.supported.jvm]] ==== JVM Metrics Auto-configuration will enable JVM Metrics using core Micrometer classes. -JVM metrics are published under the `jvm.` meter name. +JVM metrics are published under the `jvm.` and `disk.` meter names. The following JVM metrics are provided: @@ -562,6 +562,7 @@ The following JVM metrics are provided: * Statistics related to garbage collection * Threads utilization * The Number of classes loaded/unloaded +* Disk space available