Skip to content

Commit

Permalink
Add auto-configuration for DiskSpaceMetrics
Browse files Browse the repository at this point in the history
  • Loading branch information
onobc authored and snicoll committed Jun 11, 2021
1 parent 151e0eb commit e3f03dd
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,4 +69,10 @@ public ClassLoaderMetrics classLoaderMetrics() {
return new ClassLoaderMetrics();
}

@Bean
@ConditionalOnMissingBean
public DiskSpaceMetrics diskSpaceMetrics() {
return new DiskSpaceMetrics(new File("."));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,6 +38,7 @@
*
* @author Andy Wilkinson
* @author Stephane Nicoll
* @author Chris Bono
*/
class JvmMetricsAutoConfigurationTests {

Expand All @@ -43,41 +47,53 @@ 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
void allowsCustomJvmGcMetricsToBeUsed() {
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
void allowsCustomJvmMemoryMetricsToBeUsed() {
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
void allowsCustomJvmThreadMetricsToBeUsed() {
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
void allowsCustomClassLoaderMetricsToBeUsed() {
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)
Expand Down Expand Up @@ -120,4 +136,14 @@ ClassLoaderMetrics customClassLoaderMetrics() {

}

@Configuration(proxyBeanMethods = false)
static class CustomDiskSpaceMetricsConfiguration {

@Bean
DiskSpaceMetrics customDiskSpaceMetrics() {
return new DiskSpaceMetrics(new File(System.getProperty("user.dir")));
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -554,14 +554,15 @@ 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:

* Various memory and buffer pool details
* Statistics related to garbage collection
* Threads utilization
* The Number of classes loaded/unloaded
* Disk space available



Expand Down

0 comments on commit e3f03dd

Please sign in to comment.