Skip to content

Commit

Permalink
When accept-empty is false, make health indicator DOWN
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanjbaxter committed Feb 22, 2025
1 parent 4742859 commit d5e525e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 6 deletions.
3 changes: 3 additions & 0 deletions docs/modules/ROOT/pages/server/health-indicator.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ You can disable the Health Indicator by setting `management.health.config.enable

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).

NOTE: If `spring.cloud.config.server.accept-empty` is `false` and the health indicator check returns
does not return any repository data the health indicator will return `DOWN` status.

Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,19 @@ public class ConfigServerHealthIndicator extends AbstractHealthIndicator {

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

private final boolean acceptEmpty;

@Deprecated
public ConfigServerHealthIndicator(EnvironmentRepository environmentRepository) {
this.environmentRepository = environmentRepository;
this.acceptEmpty = true;
}

// autowired required or boot constructor binding produces an error
@Autowired
public ConfigServerHealthIndicator(EnvironmentRepository environmentRepository) {
public ConfigServerHealthIndicator(EnvironmentRepository environmentRepository, ConfigServerProperties properties) {
this.environmentRepository = environmentRepository;
this.acceptEmpty = properties.isAcceptEmpty();
}

@PostConstruct
Expand Down Expand Up @@ -103,6 +112,11 @@ protected void doHealthCheck(Health.Builder builder) {
return;
}
}
if (!this.acceptEmpty && details.isEmpty()) {
// If accept-empty is false and no repositories are found, meaning details is
// empty, then set status to DOWN
builder.down().withDetail("acceptEmpty", this.acceptEmpty);
}
builder.withDetail("repositories", details);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,9 @@ public ConfigTokenProvider defaultConfigTokenProvider(ObjectProvider<HttpServlet
protected static class ConfigServerActuatorConfiguration {

@Bean
public ConfigServerHealthIndicator configServerHealthIndicator(EnvironmentRepository repository) {
return new ConfigServerHealthIndicator(repository);
public ConfigServerHealthIndicator configServerHealthIndicator(EnvironmentRepository repository,
ConfigServerProperties configServerProperties) {
return new ConfigServerHealthIndicator(repository, configServerProperties);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class ConfigServerHealthIndicatorTests {
@BeforeEach
public void init() {
initMocks(this);
this.indicator = new ConfigServerHealthIndicator(this.repository);
this.indicator = new ConfigServerHealthIndicator(this.repository, new ConfigServerProperties());
this.indicator.init();
}

Expand Down Expand Up @@ -89,4 +89,13 @@ public void customLabelWorks() {
assertThat(this.indicator.health().getStatus()).as("wrong default status").isEqualTo(Status.UP);
}

@Test
public void acceptEmptyFalse() {
ConfigServerProperties configServerProperties = new ConfigServerProperties();
configServerProperties.setAcceptEmpty(false);
this.indicator = new ConfigServerHealthIndicator(this.repository, configServerProperties);
when(this.repository.findOne("myname", "myprofile", "mylabel", false)).thenReturn(this.environment);
assertThat(this.indicator.health().getStatus()).as("wrong default status").isEqualTo(Status.DOWN);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ public void configServerActuatorConfigurationWithCustomHealthStatus() {
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
EnvironmentRepositoryConfigurationTests.EnableConfigurationPropertiesBeans.class,
EnvironmentRepositoryConfiguration.ConfigServerActuatorConfiguration.class))
EnvironmentRepositoryConfiguration.ConfigServerActuatorConfiguration.class,
ConfigServerProperties.class))
.withPropertyValues("spring.cloud.config.server.health.down-health-status=CUSTOMIZED")
.run((context) -> {
ConfigServerHealthIndicator healthIndicator = context.getBean(ConfigServerHealthIndicator.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.cloud.config.environment.PropertySource;
import org.springframework.cloud.config.server.config.CompositeConfiguration;
import org.springframework.cloud.config.server.config.ConfigServerHealthIndicator;
import org.springframework.cloud.config.server.config.ConfigServerProperties;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down Expand Up @@ -140,7 +141,7 @@ public void testVersion() {
public void overridingCompositeEnvRepo_contextLoads() {
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
context.register(OverrideCompositeConfig.class, CompositeConfiguration.class,
ConfigServerHealthIndicator.class);
ConfigServerHealthIndicator.class, ConfigServerProperties.class);
context.refresh();
}
}
Expand Down

0 comments on commit d5e525e

Please # to comment.