Make sure health endpoint is available with no contributor
This commit makes sure that the health endpoint returns a default health status when no contributors are available. Previously, it was returning `null` which leads to a 404 when exposed via HTTP. Closes gh-18676
This commit is contained in:
parent
f57baa71e0
commit
86a1c96224
|
|
@ -62,7 +62,8 @@ public class HealthEndpoint extends HealthEndpointSupport<HealthContributor, Hea
|
|||
|
||||
@ReadOperation
|
||||
public HealthComponent health() {
|
||||
return health(ApiVersion.V3, EMPTY_PATH);
|
||||
HealthComponent health = health(ApiVersion.V3, EMPTY_PATH);
|
||||
return (health != null) ? health : DEFAULT_HEALTH;
|
||||
}
|
||||
|
||||
@ReadOperation
|
||||
|
|
|
|||
|
|
@ -34,6 +34,8 @@ import org.springframework.util.Assert;
|
|||
*/
|
||||
abstract class HealthEndpointSupport<C, T> {
|
||||
|
||||
static final Health DEFAULT_HEALTH = Health.up().build();
|
||||
|
||||
private final ContributorRegistry<C> registry;
|
||||
|
||||
private final HealthEndpointGroups groups;
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package org.springframework.boot.actuate.health;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
|
|
@ -79,7 +80,9 @@ public class HealthEndpointWebExtension extends HealthEndpointSupport<HealthCont
|
|||
boolean showAll, String... path) {
|
||||
HealthResult<HealthComponent> result = getHealth(apiVersion, securityContext, showAll, path);
|
||||
if (result == null) {
|
||||
return new WebEndpointResponse<>(WebEndpointResponse.STATUS_NOT_FOUND);
|
||||
return (Arrays.equals(path, NO_PATH))
|
||||
? new WebEndpointResponse<>(DEFAULT_HEALTH, WebEndpointResponse.STATUS_OK)
|
||||
: new WebEndpointResponse<>(WebEndpointResponse.STATUS_NOT_FOUND);
|
||||
}
|
||||
HealthComponent health = result.getHealth();
|
||||
HealthEndpointGroup group = result.getGroup();
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package org.springframework.boot.actuate.health;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
|
|
@ -81,7 +82,9 @@ public class ReactiveHealthEndpointWebExtension
|
|||
SecurityContext securityContext, boolean showAll, String... path) {
|
||||
HealthResult<Mono<? extends HealthComponent>> result = getHealth(apiVersion, securityContext, showAll, path);
|
||||
if (result == null) {
|
||||
return Mono.just(new WebEndpointResponse<>(WebEndpointResponse.STATUS_NOT_FOUND));
|
||||
return (Arrays.equals(path, NO_PATH))
|
||||
? Mono.just(new WebEndpointResponse<>(DEFAULT_HEALTH, WebEndpointResponse.STATUS_OK))
|
||||
: Mono.just(new WebEndpointResponse<>(WebEndpointResponse.STATUS_NOT_FOUND));
|
||||
}
|
||||
HealthEndpointGroup group = result.getGroup();
|
||||
return result.getHealth().map((health) -> {
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package org.springframework.boot.actuate.health;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
|
@ -51,6 +52,15 @@ class HealthEndpointTests
|
|||
assertThat(health).isInstanceOf(SystemHealth.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void healthWithNoContributorReturnsUp() {
|
||||
assertThat(this.registry).isEmpty();
|
||||
HealthComponent health = create(this.registry,
|
||||
HealthEndpointGroups.of(mock(HealthEndpointGroup.class), Collections.emptyMap())).health();
|
||||
assertThat(health.getStatus()).isEqualTo(Status.UP);
|
||||
assertThat(health).isInstanceOf(Health.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void healthWhenPathDoesNotExistReturnsNull() {
|
||||
this.registry.registerContributor("test", createContributor(this.up));
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package org.springframework.boot.actuate.health;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
|
@ -58,6 +59,18 @@ class HealthEndpointWebExtensionTests
|
|||
assertThat(response.getStatus()).isEqualTo(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void healthWithNoContributorReturnsUp() {
|
||||
assertThat(this.registry).isEmpty();
|
||||
WebEndpointResponse<HealthComponent> response = create(this.registry,
|
||||
HealthEndpointGroups.of(mock(HealthEndpointGroup.class), Collections.emptyMap()))
|
||||
.health(ApiVersion.LATEST, SecurityContext.NONE);
|
||||
assertThat(response.getStatus()).isEqualTo(200);
|
||||
HealthComponent health = response.getBody();
|
||||
assertThat(health.getStatus()).isEqualTo(Status.UP);
|
||||
assertThat(health).isInstanceOf(Health.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void healthWhenPathDoesNotExistReturnsHttp404() {
|
||||
this.registry.registerContributor("test", createContributor(this.up));
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package org.springframework.boot.actuate.health;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
|
@ -60,6 +61,18 @@ class ReactiveHealthEndpointWebExtensionTests extends
|
|||
assertThat(response.getStatus()).isEqualTo(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void healthWithNoContributorReturnsUp() {
|
||||
assertThat(this.registry).isEmpty();
|
||||
WebEndpointResponse<? extends HealthComponent> response = create(this.registry,
|
||||
HealthEndpointGroups.of(mock(HealthEndpointGroup.class), Collections.emptyMap()))
|
||||
.health(ApiVersion.LATEST, SecurityContext.NONE).block();
|
||||
assertThat(response.getStatus()).isEqualTo(200);
|
||||
HealthComponent health = response.getBody();
|
||||
assertThat(health.getStatus()).isEqualTo(Status.UP);
|
||||
assertThat(health).isInstanceOf(Health.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void healthWhenPathDoesNotExistReturnsHttp404() {
|
||||
this.registry.registerContributor("test", createContributor(this.up));
|
||||
|
|
|
|||
Loading…
Reference in New Issue