Skip to content

Commit bfa537f

Browse files
ilia1243ryanjbaxter
authored andcommitted
[config-server] Allow overriding of accept-empty property in health indicator (#3190)
Allow developer to set the property 'spring.cloud.config.server.health.accept-empty' to override the property 'spring.cloud.config.server.accept-empty'. Signed-off-by: ilia1243 <8808144+ilia1243@users.noreply.github.com>
1 parent a35f24f commit bfa537f

File tree

4 files changed

+68
-5
lines changed

4 files changed

+68
-5
lines changed

docs/modules/ROOT/pages/server/health-indicator.adoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ You can disable the Health Indicator by setting `spring.cloud.config.server.heal
2525

2626
Also, you can provide a custom `down` status of your own by setting property `spring.cloud.config.server.health.down-health-status` (valued to `"DOWN'` by default).
2727

28-
NOTE: If `spring.cloud.config.server.accept-empty` is `false` and the health indicator check returns
29-
does not return any repository data the health indicator will return `DOWN` status.
30-
28+
NOTE: If `spring.cloud.config.server.accept-empty` is `false` and the health indicator check
29+
does not return any repository data the health indicator will return `down` status.
30+
You can override this by setting `spring.cloud.config.server.health.accept-empty=true`.

spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/ConfigServerHealthIndicator.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class ConfigServerHealthIndicator extends AbstractHealthIndicator {
5151

5252
private String downHealthStatus = Status.DOWN.getCode();
5353

54-
private final boolean acceptEmpty;
54+
private boolean acceptEmpty;
5555

5656
@Deprecated
5757
public ConfigServerHealthIndicator(EnvironmentRepository environmentRepository) {
@@ -116,7 +116,7 @@ protected void doHealthCheck(Health.Builder builder) {
116116
// If accept-empty is false and no repositories are found, meaning details is
117117
// empty, then set status to DOWN
118118
// If there are details but none of them have sources, then set status to DOWN
119-
builder.down().withDetail("acceptEmpty", this.acceptEmpty);
119+
builder.status(this.downHealthStatus).withDetail("acceptEmpty", this.acceptEmpty);
120120
}
121121
builder.withDetail("repositories", details);
122122

@@ -138,6 +138,14 @@ public void setDownHealthStatus(String downHealthStatus) {
138138
this.downHealthStatus = downHealthStatus;
139139
}
140140

141+
public boolean isAcceptEmpty() {
142+
return acceptEmpty;
143+
}
144+
145+
public void setAcceptEmpty(boolean acceptEmpty) {
146+
this.acceptEmpty = acceptEmpty;
147+
}
148+
141149
/**
142150
* Repository properties.
143151
*/

spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/config/ConfigServerHealthIndicatorTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ public void acceptEmptyFalseNoRepos() {
9898
assertThat(this.indicator.health().getStatus()).as("wrong default status").isEqualTo(Status.DOWN);
9999
}
100100

101+
@Test
102+
public void acceptEmptyFalseNoReposCustomizedDownStatus() {
103+
ConfigServerProperties configServerProperties = new ConfigServerProperties();
104+
configServerProperties.setAcceptEmpty(false);
105+
this.indicator = new ConfigServerHealthIndicator(this.repository, configServerProperties);
106+
ReflectionTestUtils.setField(this.indicator, "downHealthStatus", "CUSTOM");
107+
when(this.repository.findOne("myname", "myprofile", "mylabel", false)).thenReturn(this.environment);
108+
assertThat(this.indicator.health().getStatus()).as("wrong exception status").isEqualTo(new Status(("CUSTOM")));
109+
}
110+
101111
@Test
102112
public void acceptEmptyFalseNoPropertySources() {
103113
Repository repo = new Repository();

spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/config/EnvironmentRepositoryConfigurationTests.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,20 @@ public void customGitCredentialsProvider() {
8282
});
8383
}
8484

85+
@Test
86+
public void configServerActuatorConfigurationWithDefaultSettings() {
87+
new ApplicationContextRunner()
88+
.withConfiguration(AutoConfigurations.of(
89+
EnvironmentRepositoryConfigurationTests.EnableConfigurationPropertiesBeans.class,
90+
EnvironmentRepositoryConfiguration.ConfigServerActuatorConfiguration.class,
91+
ConfigServerProperties.class))
92+
.run((context) -> {
93+
ConfigServerHealthIndicator healthIndicator = context.getBean(ConfigServerHealthIndicator.class);
94+
assertThat(ReflectionTestUtils.getField(healthIndicator, "downHealthStatus")).isEqualTo("DOWN");
95+
assertThat(ReflectionTestUtils.getField(healthIndicator, "acceptEmpty")).isEqualTo(true);
96+
});
97+
}
98+
8599
@Test
86100
public void configServerActuatorConfigurationWithCustomHealthStatus() {
87101
new ApplicationContextRunner()
@@ -96,6 +110,37 @@ public void configServerActuatorConfigurationWithCustomHealthStatus() {
96110
});
97111
}
98112

113+
@Test
114+
public void configServerActuatorConfigurationWithServerAcceptEmptyFalse() {
115+
new ApplicationContextRunner()
116+
.withConfiguration(AutoConfigurations.of(
117+
EnvironmentRepositoryConfigurationTests.EnableConfigurationPropertiesBeans.class,
118+
EnvironmentRepositoryConfiguration.ConfigServerActuatorConfiguration.class,
119+
ConfigServerProperties.class))
120+
.withPropertyValues("spring.cloud.config.server.accept-empty=false")
121+
.run((context) -> {
122+
ConfigServerHealthIndicator healthIndicator = context.getBean(ConfigServerHealthIndicator.class);
123+
assertThat(ReflectionTestUtils.getField(healthIndicator, "acceptEmpty")).isEqualTo(false);
124+
});
125+
}
126+
127+
@Test
128+
public void configServerActuatorConfigurationWithOverriddenAcceptEmptyTrue() {
129+
new ApplicationContextRunner()
130+
.withConfiguration(AutoConfigurations.of(
131+
EnvironmentRepositoryConfigurationTests.EnableConfigurationPropertiesBeans.class,
132+
EnvironmentRepositoryConfiguration.ConfigServerActuatorConfiguration.class,
133+
ConfigServerProperties.class))
134+
.withPropertyValues(
135+
"spring.cloud.config.server.accept-empty=false",
136+
"spring.cloud.config.server.health.accept-empty=true"
137+
)
138+
.run((context) -> {
139+
ConfigServerHealthIndicator healthIndicator = context.getBean(ConfigServerHealthIndicator.class);
140+
assertThat(ReflectionTestUtils.getField(healthIndicator, "acceptEmpty")).isEqualTo(true);
141+
});
142+
}
143+
99144
@TestConfiguration
100145
public static class TestBeans {
101146

0 commit comments

Comments
 (0)