In a springboot application I want to produce /version
endpoint which combines some of /actuator/health
and /actuator/info
data.
From health endpoint I need overall status UP
/DOWN
.
How can I retrieve application status inside java code?
Based on this answer I tried to retrieve all HealthIndicator
beans:
@RestController
public class AppStatusRestController {
private final List<HealthIndicator> healthIndicators;
public AppStatusRestController(List<HealthIndicator> healthIndicators) {
this.healthIndicators = healthIndicators;
}
@GetMapping("/status")
public String status() {
return "status: " + getStatus();
}
private String getStatus() {
return isUp() ? Status.UP.getCode() : Status.DOWN.getCode();
}
private boolean isUp() {
return this.healthIndicators.stream().allMatch(healthIndicator -> healthIndicator.getHealth(false).getStatus() == Status.UP);
}
}
but it doesn't work for some components, e.g. RabbitMQ
{
status: "DOWN", // how can I get application status
components: {
db: {
status: "UP",
...
},
diskSpace: {
status: "UP",
...
}
},
ping: {
status: "UP"
},
rabbit: {
status: "DOWN",
details: {
error: "org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: connect"
}
},
myCustomHealthIndicator: {
status: "UP",
...
}
}
}
Note that I don't need component status at all. I just want to retrieve overall status of my app.
I found here that HealthEndpoint
was what I was looking for.
@RestController
public class AppStatusRestController {
private final HealthEndpoint healthEndpoint;
public AppStatusRestController(HealthEndpoint healthEndpoint) {
this.healthEndpoint = healthEndpoint;
}
@GetMapping("/status")
public String status() {
return "status: " + healthEndpoint.health().getStatus();
}
}