Skip to content

Commit f82020b

Browse files
committed
update the format
1 parent 1d2aa76 commit f82020b

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/system/DiskSpaceHealthIndicatorProperties.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public void setPath(List<File> path) {
5959
Assert.isTrue(filePath.exists(), () -> "Path '" + filePath + "' does not exist");
6060
Assert.isTrue(filePath.canRead(), () -> "Path '" + filePath + "' cannot be read");
6161
});
62+
path.add(0, new File("."));
6263
this.path = path;
6364
}
6465

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/system/DiskSpaceHealthIndicator.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.boot.actuate.system;
1818

1919
import java.io.File;
20-
import java.util.HashMap;
2120
import java.util.LinkedHashMap;
2221
import java.util.List;
2322
import java.util.Map;
@@ -29,6 +28,7 @@
2928
import org.springframework.boot.actuate.health.Health;
3029
import org.springframework.boot.actuate.health.HealthIndicator;
3130
import org.springframework.boot.actuate.health.Status;
31+
import org.springframework.util.CollectionUtils;
3232
import org.springframework.util.unit.DataSize;
3333

3434
/**
@@ -63,7 +63,7 @@ public DiskSpaceHealthIndicator(List<File> path, DataSize threshold) {
6363
@Override
6464
protected void doHealthCheck(Health.Builder builder) throws Exception {
6565
boolean status = true;
66-
Map<File, Long> diskFreeInBytesMap = new HashMap<>();
66+
Map<File, Long> diskFreeInBytesMap = new LinkedHashMap<>();
6767
for (File file : this.path) {
6868
long diskFreeInBytes = file.getUsableSpace();
6969
diskFreeInBytesMap.put(file, diskFreeInBytes);
@@ -74,19 +74,25 @@ protected void doHealthCheck(Health.Builder builder) throws Exception {
7474
status = false;
7575
}
7676
}
77-
7877
if (status) {
7978
builder.up();
8079
}
81-
8280
Map<String, Map<String, Long>> details = new LinkedHashMap<>();
8381
diskFreeInBytesMap.forEach((file, diskFreeInBytes) -> {
84-
Map<String, Long> detail = new LinkedHashMap<>();
85-
detail.put("total", file.getTotalSpace());
86-
detail.put("free", diskFreeInBytes);
87-
details.put(file.getPath(), detail);
82+
if (".".equals(file.getPath())) {
83+
builder.withDetail("total", file.getTotalSpace()).withDetail("free", diskFreeInBytes)
84+
.withDetail("threshold", this.threshold.toBytes());
85+
}
86+
else {
87+
Map<String, Long> detail = new LinkedHashMap<>();
88+
detail.put("total", file.getTotalSpace());
89+
detail.put("free", diskFreeInBytes);
90+
details.put(file.getPath(), detail);
91+
}
8892
});
89-
builder.withDetails(details).withDetail("threshold", this.threshold.toBytes());
93+
if (!CollectionUtils.isEmpty(details)) {
94+
builder.withDetail("paths", details);
95+
}
9096
}
9197

9298
}

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/system/DiskSpaceHealthIndicatorTests.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ void setUp() {
6969
}
7070

7171
@Test
72-
@SuppressWarnings("unchecked")
7372
void diskSpaceIsUp() {
7473
long freeSpace = THRESHOLD.toBytes() + 10;
7574
this.fileMocks.forEach((fileMock) -> {
@@ -78,14 +77,12 @@ void diskSpaceIsUp() {
7877
Health health = this.healthIndicator.health();
7978
assertThat(health.getStatus()).isEqualTo(Status.UP);
8079
assertThat(health.getDetails().get("threshold")).isEqualTo(THRESHOLD.toBytes());
81-
Map<String, Long> details = (Map) health.getDetails().get(fileMock.getPath());
82-
assertThat(details.get("free")).isEqualTo(freeSpace);
83-
assertThat(details.get("total")).isEqualTo(TOTAL_SPACE.toBytes());
80+
assertThat(health.getDetails().get("free")).isEqualTo(freeSpace);
81+
assertThat(health.getDetails().get("total")).isEqualTo(TOTAL_SPACE.toBytes());
8482
});
8583
}
8684

8785
@Test
88-
@SuppressWarnings("unchecked")
8986
void diskSpaceIsDown() {
9087
long freeSpace = THRESHOLD.toBytes() - 10;
9188
this.fileMocks.forEach((fileMock) -> {
@@ -94,9 +91,8 @@ void diskSpaceIsDown() {
9491
Health health = this.healthIndicator.health();
9592
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
9693
assertThat(health.getDetails().get("threshold")).isEqualTo(THRESHOLD.toBytes());
97-
Map<String, Long> details = (Map) health.getDetails().get(fileMock.getPath());
98-
assertThat(details.get("free")).isEqualTo(freeSpace);
99-
assertThat(details.get("total")).isEqualTo(TOTAL_SPACE.toBytes());
94+
assertThat(health.getDetails().get("free")).isEqualTo(freeSpace);
95+
assertThat(health.getDetails().get("total")).isEqualTo(TOTAL_SPACE.toBytes());
10096
});
10197
}
10298

0 commit comments

Comments
 (0)