Skip to content

Commit a2e6fbf

Browse files
committed
Merge pull request #26001 from bono007
* pr/26001: Polish "Add auto-configuration for DiskSpaceMetrics" Add auto-configuration for DiskSpaceMetrics Closes gh-26001
2 parents 151e0eb + a90c718 commit a2e6fbf

File tree

3 files changed

+47
-19
lines changed

3 files changed

+47
-19
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/JvmMetricsAutoConfiguration.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,11 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.metrics;
1818

19+
import java.io.File;
20+
1921
import io.micrometer.core.instrument.MeterRegistry;
2022
import io.micrometer.core.instrument.binder.jvm.ClassLoaderMetrics;
23+
import io.micrometer.core.instrument.binder.jvm.DiskSpaceMetrics;
2124
import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics;
2225
import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics;
2326
import io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics;
@@ -66,4 +69,10 @@ public ClassLoaderMetrics classLoaderMetrics() {
6669
return new ClassLoaderMetrics();
6770
}
6871

72+
@Bean
73+
@ConditionalOnMissingBean
74+
public DiskSpaceMetrics diskSpaceMetrics() {
75+
return new DiskSpaceMetrics(new File("."));
76+
}
77+
6978
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/JvmMetricsAutoConfigurationTests.java

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,15 +16,20 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.metrics;
1818

19+
import java.io.File;
20+
1921
import io.micrometer.core.instrument.binder.jvm.ClassLoaderMetrics;
22+
import io.micrometer.core.instrument.binder.jvm.DiskSpaceMetrics;
2023
import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics;
2124
import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics;
2225
import io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics;
2326
import org.junit.jupiter.api.Test;
2427

2528
import org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun;
2629
import org.springframework.boot.autoconfigure.AutoConfigurations;
30+
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
2731
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
32+
import org.springframework.boot.test.context.runner.ContextConsumer;
2833
import org.springframework.context.annotation.Bean;
2934
import org.springframework.context.annotation.Configuration;
3035

@@ -35,6 +40,7 @@
3540
*
3641
* @author Andy Wilkinson
3742
* @author Stephane Nicoll
43+
* @author Chris Bono
3844
*/
3945
class JvmMetricsAutoConfigurationTests {
4046

@@ -43,41 +49,43 @@ class JvmMetricsAutoConfigurationTests {
4349

4450
@Test
4551
void autoConfiguresJvmMetrics() {
46-
this.contextRunner.run(
47-
(context) -> assertThat(context).hasSingleBean(JvmGcMetrics.class).hasSingleBean(JvmMemoryMetrics.class)
48-
.hasSingleBean(JvmThreadMetrics.class).hasSingleBean(ClassLoaderMetrics.class));
52+
this.contextRunner.run(assertMetricsBeans());
4953
}
5054

5155
@Test
5256
void allowsCustomJvmGcMetricsToBeUsed() {
5357
this.contextRunner.withUserConfiguration(CustomJvmGcMetricsConfiguration.class)
54-
.run((context) -> assertThat(context).hasSingleBean(JvmGcMetrics.class).hasBean("customJvmGcMetrics")
55-
.hasSingleBean(JvmMemoryMetrics.class).hasSingleBean(JvmThreadMetrics.class)
56-
.hasSingleBean(ClassLoaderMetrics.class));
58+
.run(assertMetricsBeans().andThen((context) -> assertThat(context).hasBean("customJvmGcMetrics")));
5759
}
5860

5961
@Test
6062
void allowsCustomJvmMemoryMetricsToBeUsed() {
6163
this.contextRunner.withUserConfiguration(CustomJvmMemoryMetricsConfiguration.class)
62-
.run((context) -> assertThat(context).hasSingleBean(JvmGcMetrics.class)
63-
.hasSingleBean(JvmMemoryMetrics.class).hasBean("customJvmMemoryMetrics")
64-
.hasSingleBean(JvmThreadMetrics.class).hasSingleBean(ClassLoaderMetrics.class));
64+
.run(assertMetricsBeans().andThen((context) -> assertThat(context).hasBean("customJvmMemoryMetrics")));
6565
}
6666

6767
@Test
6868
void allowsCustomJvmThreadMetricsToBeUsed() {
6969
this.contextRunner.withUserConfiguration(CustomJvmThreadMetricsConfiguration.class)
70-
.run((context) -> assertThat(context).hasSingleBean(JvmGcMetrics.class)
71-
.hasSingleBean(JvmMemoryMetrics.class).hasSingleBean(JvmThreadMetrics.class)
72-
.hasSingleBean(ClassLoaderMetrics.class).hasBean("customJvmThreadMetrics"));
70+
.run(assertMetricsBeans().andThen((context) -> assertThat(context).hasBean("customJvmThreadMetrics")));
7371
}
7472

7573
@Test
7674
void allowsCustomClassLoaderMetricsToBeUsed() {
77-
this.contextRunner.withUserConfiguration(CustomClassLoaderMetricsConfiguration.class)
78-
.run((context) -> assertThat(context).hasSingleBean(JvmGcMetrics.class)
79-
.hasSingleBean(JvmMemoryMetrics.class).hasSingleBean(JvmThreadMetrics.class)
80-
.hasSingleBean(ClassLoaderMetrics.class).hasBean("customClassLoaderMetrics"));
75+
this.contextRunner.withUserConfiguration(CustomClassLoaderMetricsConfiguration.class).run(
76+
assertMetricsBeans().andThen((context) -> assertThat(context).hasBean("customClassLoaderMetrics")));
77+
}
78+
79+
@Test
80+
void allowsCustomDiskSpaceMetricsToBeUsed() {
81+
this.contextRunner.withUserConfiguration(CustomDiskSpaceMetricsConfiguration.class)
82+
.run(assertMetricsBeans().andThen((context) -> assertThat(context).hasBean("customDiskSpaceMetrics")));
83+
}
84+
85+
private ContextConsumer<AssertableApplicationContext> assertMetricsBeans() {
86+
return (context) -> assertThat(context).hasSingleBean(JvmGcMetrics.class).hasSingleBean(JvmMemoryMetrics.class)
87+
.hasSingleBean(JvmThreadMetrics.class).hasSingleBean(ClassLoaderMetrics.class)
88+
.hasSingleBean(DiskSpaceMetrics.class);
8189
}
8290

8391
@Configuration(proxyBeanMethods = false)
@@ -120,4 +128,14 @@ ClassLoaderMetrics customClassLoaderMetrics() {
120128

121129
}
122130

131+
@Configuration(proxyBeanMethods = false)
132+
static class CustomDiskSpaceMetricsConfiguration {
133+
134+
@Bean
135+
DiskSpaceMetrics customDiskSpaceMetrics() {
136+
return new DiskSpaceMetrics(new File(System.getProperty("user.dir")));
137+
}
138+
139+
}
140+
123141
}

spring-boot-project/spring-boot-docs/src/docs/asciidoc/actuator/metrics.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,14 +554,15 @@ In most situations, the out-of-the-box defaults will provide sensible metrics th
554554
[[actuator.metrics.supported.jvm]]
555555
==== JVM Metrics
556556
Auto-configuration will enable JVM Metrics using core Micrometer classes.
557-
JVM metrics are published under the `jvm.` meter name.
557+
JVM metrics are published under the `jvm.` and `disk.` meter names.
558558

559559
The following JVM metrics are provided:
560560

561561
* Various memory and buffer pool details
562562
* Statistics related to garbage collection
563563
* Threads utilization
564564
* The Number of classes loaded/unloaded
565+
* Disk space available
565566

566567

567568

0 commit comments

Comments
 (0)