Support relaxed HealthMvcEndpoint status mappings
Update HealthMvcEndpoint so that relaxed names can be used as keys in the `endpoints.health.mapping` property. Fixes gh-2465
This commit is contained in:
parent
49583c0aa3
commit
3b93a82dd6
|
|
@ -25,6 +25,7 @@ import org.springframework.boot.actuate.endpoint.Endpoint;
|
|||
import org.springframework.boot.actuate.endpoint.HealthEndpoint;
|
||||
import org.springframework.boot.actuate.health.Health;
|
||||
import org.springframework.boot.actuate.health.Status;
|
||||
import org.springframework.boot.bind.RelaxedNames;
|
||||
import org.springframework.boot.bind.RelaxedPropertyResolver;
|
||||
import org.springframework.context.EnvironmentAware;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
|
@ -128,13 +129,27 @@ public class HealthMvcEndpoint implements MvcEndpoint, EnvironmentAware {
|
|||
"message", "This endpoint is disabled"), HttpStatus.NOT_FOUND);
|
||||
}
|
||||
Health health = getHealth(principal);
|
||||
HttpStatus status = this.statusMapping.get(health.getStatus().getCode());
|
||||
HttpStatus status = getStatus(health);
|
||||
if (status != null) {
|
||||
return new ResponseEntity<Health>(health, status);
|
||||
}
|
||||
return health;
|
||||
}
|
||||
|
||||
private HttpStatus getStatus(Health health) {
|
||||
String code = health.getStatus().getCode();
|
||||
if (code != null) {
|
||||
code = code.toLowerCase().replace("_", "-");
|
||||
for (String candidate : RelaxedNames.forCamelCase(code)) {
|
||||
HttpStatus status = this.statusMapping.get(candidate);
|
||||
if (status != null) {
|
||||
return status;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Health getHealth(Principal principal) {
|
||||
long accessTime = System.currentTimeMillis();
|
||||
if (isCacheStale(accessTime)) {
|
||||
|
|
|
|||
|
|
@ -91,8 +91,8 @@ public class HealthMvcEndpointTests {
|
|||
assertEquals(HttpStatus.SERVICE_UNAVAILABLE, response.getStatusCode());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void customMapping() {
|
||||
given(this.endpoint.invoke()).willReturn(
|
||||
new Health.Builder().status("OK").build());
|
||||
|
|
@ -105,6 +105,20 @@ public class HealthMvcEndpointTests {
|
|||
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void customMappingWithRelaxedName() {
|
||||
given(this.endpoint.invoke()).willReturn(
|
||||
new Health.Builder().outOfService().build());
|
||||
this.mvc.setStatusMapping(Collections.singletonMap("out-of-service",
|
||||
HttpStatus.INTERNAL_SERVER_ERROR));
|
||||
Object result = this.mvc.invoke(null);
|
||||
assertTrue(result instanceof ResponseEntity);
|
||||
ResponseEntity<Health> response = (ResponseEntity<Health>) result;
|
||||
assertTrue(response.getBody().getStatus().equals(Status.OUT_OF_SERVICE));
|
||||
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void secure() {
|
||||
given(this.endpoint.invoke()).willReturn(
|
||||
|
|
|
|||
Loading…
Reference in New Issue