From a4913712cbe9be97527e85ac32fabde3b5d81f34 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Sat, 2 Dec 2017 08:29:07 +0100 Subject: [PATCH] Do not expose the composite ReactiveHealthIndicator as a bean Previously, a `ReactiveHealthIndicator` bean was exposed to define the health indicator to use for the reactive endpoint. Having it exposed as a bean has the side effect that the regular `HealthIndicator` composite is picked up and a "reactive" entry is added to the health details. This commit creates such indicator internally as it should be. Closes gh-11222 --- ...ndpointManagementContextConfiguration.java | 10 ++--- ...veManagementContextConfigurationTests.java | 39 +++++++++++++++++++ 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/health/HealthWebEndpointManagementContextConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/health/HealthWebEndpointManagementContextConfiguration.java index 264317c600..20b6dba5fe 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/health/HealthWebEndpointManagementContextConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/health/HealthWebEndpointManagementContextConfiguration.java @@ -65,12 +65,13 @@ public class HealthWebEndpointManagementContextConfiguration { @ConditionalOnWebApplication(type = Type.REACTIVE) static class ReactiveWebHealthConfiguration { - @Bean - public ReactiveHealthIndicator reactiveHealthIndicator( + private final ReactiveHealthIndicator reactiveHealthIndicator; + + ReactiveWebHealthConfiguration( ObjectProvider healthAggregator, ObjectProvider> reactiveHealthIndicators, ObjectProvider> healthIndicators) { - return new CompositeReactiveHealthIndicatorFactory() + this.reactiveHealthIndicator = new CompositeReactiveHealthIndicatorFactory() .createReactiveHealthIndicator( healthAggregator.getIfAvailable(OrderedHealthAggregator::new), reactiveHealthIndicators @@ -83,10 +84,9 @@ public class HealthWebEndpointManagementContextConfiguration { @ConditionalOnEnabledEndpoint @ConditionalOnBean(HealthEndpoint.class) public ReactiveHealthEndpointWebExtension reactiveHealthEndpointWebExtension( - ReactiveHealthIndicator reactiveHealthIndicator, HealthStatusHttpMapper healthStatusHttpMapper, HealthEndpointProperties properties) { - return new ReactiveHealthEndpointWebExtension(reactiveHealthIndicator, + return new ReactiveHealthEndpointWebExtension(this.reactiveHealthIndicator, healthStatusHttpMapper, properties.isShowDetails()); } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/health/HealthWebEndpointReactiveManagementContextConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/health/HealthWebEndpointReactiveManagementContextConfigurationTests.java index 8352fc3114..c75bb0d9d6 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/health/HealthWebEndpointReactiveManagementContextConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/health/HealthWebEndpointReactiveManagementContextConfigurationTests.java @@ -19,10 +19,17 @@ package org.springframework.boot.actuate.autoconfigure.health; import java.util.Map; import org.junit.Test; +import reactor.core.publisher.Mono; +import org.springframework.boot.actuate.health.Health; +import org.springframework.boot.actuate.health.HealthEndpoint; +import org.springframework.boot.actuate.health.HealthIndicator; import org.springframework.boot.actuate.health.HealthStatusHttpMapper; import org.springframework.boot.actuate.health.ReactiveHealthEndpointWebExtension; +import org.springframework.boot.actuate.health.ReactiveHealthIndicator; import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; import org.springframework.test.util.ReflectionTestUtils; import static org.assertj.core.api.Assertions.assertThat; @@ -72,4 +79,36 @@ public class HealthWebEndpointReactiveManagementContextConfigurationTests { }); } + @Test + public void regularAndReactiveHealthIndicatorsMatch() { + this.contextRunner + .withUserConfiguration(HealthIndicatorsConfiguration.class) + .run((context) -> { + HealthEndpoint endpoint = context.getBean(HealthEndpoint.class); + ReactiveHealthEndpointWebExtension extension = context + .getBean(ReactiveHealthEndpointWebExtension.class); + Health endpointHealth = endpoint.health(); + Health extensionHealth = extension.health(true).block().getBody(); + assertThat(endpointHealth.getDetails()) + .containsOnlyKeys("application", "first", "second"); + assertThat(extensionHealth.getDetails()) + .containsOnlyKeys("application", "first", "second"); + }); + } + + @Configuration + static class HealthIndicatorsConfiguration { + + @Bean + public HealthIndicator firstHealthIndicator() { + return () -> Health.up().build(); + } + + @Bean + public ReactiveHealthIndicator secondHealthIndicator() { + return () -> Mono.just(Health.up().build()); + } + + } + }