Skip to content

Commit e231f65

Browse files
committed
Merge pull request #27660 from bono007
* pr/27660: Polish "Add support for configuring the path of disk space metrics" Add support for configuring the path of disk space metrics Closes gh-27660
2 parents d4374f4 + b626adf commit e231f65

File tree

7 files changed

+237
-10
lines changed

7 files changed

+237
-10
lines changed

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

+38
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@
1616

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

19+
import java.io.File;
1920
import java.time.Duration;
21+
import java.util.ArrayList;
22+
import java.util.Collections;
2023
import java.util.LinkedHashMap;
24+
import java.util.List;
2125
import java.util.Map;
2226

2327
import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -30,6 +34,7 @@
3034
* @author Jon Schneider
3135
* @author Alexander Abramov
3236
* @author Tadaya Tsuyukubo
37+
* @author Chris Bono
3338
* @since 2.0.0
3439
*/
3540
@ConfigurationProperties("management.metrics")
@@ -57,6 +62,8 @@ public class MetricsProperties {
5762

5863
private final Data data = new Data();
5964

65+
private final System system = new System();
66+
6067
private final Distribution distribution = new Distribution();
6168

6269
public boolean isUseGlobalRegistry() {
@@ -83,6 +90,10 @@ public Data getData() {
8390
return this.data;
8491
}
8592

93+
public System getSystem() {
94+
return this.system;
95+
}
96+
8697
public Distribution getDistribution() {
8798
return this.distribution;
8899
}
@@ -257,6 +268,33 @@ public AutoTimeProperties getAutotime() {
257268

258269
}
259270

271+
public static class System {
272+
273+
private final Diskspace diskspace = new Diskspace();
274+
275+
public Diskspace getDiskspace() {
276+
return this.diskspace;
277+
}
278+
279+
public static class Diskspace {
280+
281+
/**
282+
* Comma-separated list of paths to report disk metrics for.
283+
*/
284+
private List<File> paths = new ArrayList<>(Collections.singletonList(new File(".")));
285+
286+
public List<File> getPaths() {
287+
return this.paths;
288+
}
289+
290+
public void setPaths(List<File> paths) {
291+
this.paths = paths;
292+
}
293+
294+
}
295+
296+
}
297+
260298
public static class Distribution {
261299

262300
/**

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

+10-4
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.
@@ -17,31 +17,36 @@
1717
package org.springframework.boot.actuate.autoconfigure.metrics;
1818

1919
import java.io.File;
20+
import java.util.List;
2021

2122
import io.micrometer.core.instrument.MeterRegistry;
22-
import io.micrometer.core.instrument.binder.system.DiskSpaceMetrics;
23+
import io.micrometer.core.instrument.Tags;
2324
import io.micrometer.core.instrument.binder.system.FileDescriptorMetrics;
2425
import io.micrometer.core.instrument.binder.system.ProcessorMetrics;
2526
import io.micrometer.core.instrument.binder.system.UptimeMetrics;
2627

28+
import org.springframework.boot.actuate.metrics.system.DiskSpaceMetricsBinder;
2729
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
2830
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2931
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
3032
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
3133
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
34+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3235
import org.springframework.context.annotation.Bean;
3336
import org.springframework.context.annotation.Configuration;
3437

3538
/**
3639
* {@link EnableAutoConfiguration Auto-configuration} for system metrics.
3740
*
3841
* @author Stephane Nicoll
42+
* @author Chris Bono
3943
* @since 2.1.0
4044
*/
4145
@Configuration(proxyBeanMethods = false)
4246
@AutoConfigureAfter({ MetricsAutoConfiguration.class, CompositeMeterRegistryAutoConfiguration.class })
4347
@ConditionalOnClass(MeterRegistry.class)
4448
@ConditionalOnBean(MeterRegistry.class)
49+
@EnableConfigurationProperties(MetricsProperties.class)
4550
public class SystemMetricsAutoConfiguration {
4651

4752
@Bean
@@ -64,8 +69,9 @@ public FileDescriptorMetrics fileDescriptorMetrics() {
6469

6570
@Bean
6671
@ConditionalOnMissingBean
67-
public DiskSpaceMetrics diskSpaceMetrics() {
68-
return new DiskSpaceMetrics(new File("."));
72+
public DiskSpaceMetricsBinder diskSpaceMetrics(MetricsProperties properties) {
73+
List<File> paths = properties.getSystem().getDiskspace().getPaths();
74+
return new DiskSpaceMetricsBinder(paths, Tags.empty());
6975
}
7076

7177
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json

+7
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,13 @@
326326
"reason": "Instead, filter 'process.uptime' and 'process.start.time' metrics."
327327
}
328328
},
329+
{
330+
"name": "management.metrics.system.diskspace.paths",
331+
"type": "java.util.List<java.io.File>",
332+
"defaultValue": [
333+
"."
334+
]
335+
},
329336
{
330337
"name": "management.metrics.export.appoptics.num-threads",
331338
"type": "java.lang.Integer",

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

+31-6
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.
@@ -17,14 +17,17 @@
1717
package org.springframework.boot.actuate.autoconfigure.metrics;
1818

1919
import java.io.File;
20+
import java.util.Arrays;
21+
import java.util.Collections;
2022

21-
import io.micrometer.core.instrument.binder.system.DiskSpaceMetrics;
23+
import io.micrometer.core.instrument.Tags;
2224
import io.micrometer.core.instrument.binder.system.FileDescriptorMetrics;
2325
import io.micrometer.core.instrument.binder.system.ProcessorMetrics;
2426
import io.micrometer.core.instrument.binder.system.UptimeMetrics;
2527
import org.junit.jupiter.api.Test;
2628

2729
import org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun;
30+
import org.springframework.boot.actuate.metrics.system.DiskSpaceMetricsBinder;
2831
import org.springframework.boot.autoconfigure.AutoConfigurations;
2932
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
3033
import org.springframework.context.annotation.Bean;
@@ -81,16 +84,37 @@ void allowsCustomFileDescriptorMetricsToBeUsed() {
8184

8285
@Test
8386
void autoConfiguresDiskSpaceMetrics() {
84-
this.contextRunner.run((context) -> assertThat(context).hasSingleBean(DiskSpaceMetrics.class));
87+
this.contextRunner.run((context) -> assertThat(context).hasSingleBean(DiskSpaceMetricsBinder.class));
8588
}
8689

8790
@Test
8891
void allowsCustomDiskSpaceMetricsToBeUsed() {
8992
this.contextRunner.withUserConfiguration(CustomDiskSpaceMetricsConfiguration.class)
90-
.run((context) -> assertThat(context).hasSingleBean(DiskSpaceMetrics.class)
93+
.run((context) -> assertThat(context).hasSingleBean(DiskSpaceMetricsBinder.class)
9194
.hasBean("customDiskSpaceMetrics"));
9295
}
9396

97+
@Test
98+
void diskSpaceMetricsUsesDefaultPath() {
99+
this.contextRunner
100+
.run((context) -> assertThat(context).hasBean("diskSpaceMetrics").getBean(DiskSpaceMetricsBinder.class)
101+
.hasFieldOrPropertyWithValue("paths", Collections.singletonList(new File("."))));
102+
}
103+
104+
@Test
105+
void allowsDiskSpaceMetricsPathToBeConfiguredWithSinglePath() {
106+
this.contextRunner.withPropertyValues("management.metrics.system.diskspace.paths:..")
107+
.run((context) -> assertThat(context).hasBean("diskSpaceMetrics").getBean(DiskSpaceMetricsBinder.class)
108+
.hasFieldOrPropertyWithValue("paths", Collections.singletonList(new File(".."))));
109+
}
110+
111+
@Test
112+
void allowsDiskSpaceMetricsPathToBeConfiguredWithMultiplePaths() {
113+
this.contextRunner.withPropertyValues("management.metrics.system.diskspace.paths:.,..")
114+
.run((context) -> assertThat(context).hasBean("diskSpaceMetrics").getBean(DiskSpaceMetricsBinder.class)
115+
.hasFieldOrPropertyWithValue("paths", Arrays.asList(new File("."), new File(".."))));
116+
}
117+
94118
@Configuration(proxyBeanMethods = false)
95119
static class CustomUptimeMetricsConfiguration {
96120

@@ -125,8 +149,9 @@ FileDescriptorMetrics customFileDescriptorMetrics() {
125149
static class CustomDiskSpaceMetricsConfiguration {
126150

127151
@Bean
128-
DiskSpaceMetrics customDiskSpaceMetrics() {
129-
return new DiskSpaceMetrics(new File(System.getProperty("user.dir")));
152+
DiskSpaceMetricsBinder customDiskSpaceMetrics() {
153+
return new DiskSpaceMetricsBinder(Collections.singletonList(new File(System.getProperty("user.dir"))),
154+
Tags.empty());
130155
}
131156

132157
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2012-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.metrics.system;
18+
19+
import java.io.File;
20+
import java.util.List;
21+
22+
import io.micrometer.core.instrument.MeterRegistry;
23+
import io.micrometer.core.instrument.Tag;
24+
import io.micrometer.core.instrument.binder.MeterBinder;
25+
import io.micrometer.core.instrument.binder.system.DiskSpaceMetrics;
26+
27+
import org.springframework.util.Assert;
28+
29+
/**
30+
* A {@link MeterBinder} that binds one or more {@link DiskSpaceMetrics}.
31+
*
32+
* @author Chris Bono
33+
* @since 2.6.0
34+
*/
35+
public class DiskSpaceMetricsBinder implements MeterBinder {
36+
37+
private final List<File> paths;
38+
39+
private final Iterable<Tag> tags;
40+
41+
public DiskSpaceMetricsBinder(List<File> paths, Iterable<Tag> tags) {
42+
Assert.notEmpty(paths, "Paths must not be empty");
43+
this.paths = paths;
44+
this.tags = tags;
45+
}
46+
47+
@Override
48+
public void bindTo(MeterRegistry registry) {
49+
this.paths.forEach((path) -> new DiskSpaceMetrics(path, this.tags).bindTo(registry));
50+
}
51+
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2012-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Actuator support for system metrics.
19+
*/
20+
package org.springframework.boot.actuate.metrics.system;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2012-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.metrics.system;
18+
19+
import java.io.File;
20+
import java.util.Arrays;
21+
import java.util.Collections;
22+
23+
import io.micrometer.core.instrument.MeterRegistry;
24+
import io.micrometer.core.instrument.Tags;
25+
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
26+
import org.junit.jupiter.api.Test;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
/**
31+
* Tests for {@link DiskSpaceMetricsBinder}.
32+
*
33+
* @author Chris Bono
34+
*/
35+
class DiskSpaceMetricsBinderTests {
36+
37+
@Test
38+
void diskSpaceMetricsWithSinglePath() {
39+
MeterRegistry meterRegistry = new SimpleMeterRegistry();
40+
File path = new File(".");
41+
DiskSpaceMetricsBinder metricsBinder = new DiskSpaceMetricsBinder(Collections.singletonList(path),
42+
Tags.empty());
43+
metricsBinder.bindTo(meterRegistry);
44+
45+
Tags tags = Tags.of("path", path.getAbsolutePath());
46+
assertThat(meterRegistry.get("disk.free").tags(tags).gauge()).isNotNull();
47+
assertThat(meterRegistry.get("disk.total").tags(tags).gauge()).isNotNull();
48+
}
49+
50+
@Test
51+
void diskSpaceMetricsWithMultiplePaths() {
52+
MeterRegistry meterRegistry = new SimpleMeterRegistry();
53+
File path1 = new File(".");
54+
File path2 = new File("..");
55+
DiskSpaceMetricsBinder metricsBinder = new DiskSpaceMetricsBinder(Arrays.asList(path1, path2), Tags.empty());
56+
metricsBinder.bindTo(meterRegistry);
57+
58+
Tags tags = Tags.of("path", path1.getAbsolutePath());
59+
assertThat(meterRegistry.get("disk.free").tags(tags).gauge()).isNotNull();
60+
assertThat(meterRegistry.get("disk.total").tags(tags).gauge()).isNotNull();
61+
tags = Tags.of("path", path2.getAbsolutePath());
62+
assertThat(meterRegistry.get("disk.free").tags(tags).gauge()).isNotNull();
63+
assertThat(meterRegistry.get("disk.total").tags(tags).gauge()).isNotNull();
64+
}
65+
66+
@Test
67+
void diskSpaceMetricsWithCustomTags() {
68+
MeterRegistry meterRegistry = new SimpleMeterRegistry();
69+
File path = new File(".");
70+
Tags customTags = Tags.of("foo", "bar");
71+
DiskSpaceMetricsBinder metricsBinder = new DiskSpaceMetricsBinder(Collections.singletonList(path), customTags);
72+
metricsBinder.bindTo(meterRegistry);
73+
74+
Tags tags = Tags.of("path", path.getAbsolutePath(), "foo", "bar");
75+
assertThat(meterRegistry.get("disk.free").tags(tags).gauge()).isNotNull();
76+
assertThat(meterRegistry.get("disk.total").tags(tags).gauge()).isNotNull();
77+
}
78+
79+
}

0 commit comments

Comments
 (0)