Sub topic 4
Writing a custom health indicator
Spring Boot Actuator ships health indicators for common dependencies such as databases, disk space, and message brokers. Your application often depends on something Actuator cannot know about, such as an internal service or a third-party API. A custom HealthIndicator lets you report that dependency's state in the same /actuator/health result.
This chapter assumes you can build a Spring Boot application, have Actuator on the classpath, and have already exposed the health endpoint. It also assumes you know how the health endpoint aggregates its components.
How a health indicator works
A HealthIndicator is a Spring bean with one method, health(). It returns a Health object that holds a status (UP, DOWN, OUT_OF_SERVICE, or UNKNOWN) and optional details. Actuator collects every HealthIndicator bean and merges their results into one overall status.
The component name comes from the bean class name. Actuator removes the HealthIndicator suffix and lowercases the first letter. A class named PaymentGatewayHealthIndicator therefore appears as paymentGateway.
The example
The indicator below calls a hypothetical PaymentClient. It reports UP if the call succeeds and DOWN if it throws.
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
@Component
public class PaymentGatewayHealthIndicator implements HealthIndicator {
private final PaymentClient client; // hypothetical client for an external service
public PaymentGatewayHealthIndicator(PaymentClient client) {
this.client = client;
}
@Override
public Health health() {
try {
client.ping();
return Health.up().withDetail("gateway", "reachable").build();
} catch (Exception ex) {
return Health.down(ex).build();
}
}
}Health.down(ex) adds the exception to the result under an error key. withDetail adds your own key and value pairs.
With show-details: when-authorized configured, an authorized caller sees output like this:
{
"status": "DOWN",
"components": {
"paymentGateway": {
"status": "DOWN",
"details": {
"error": "java.net.ConnectException: Connection refused"
}
}
}
}Anonymous callers see only the status, which avoids leaking internal details.
Common errors
- Slow checks. The indicator runs on every request to the health endpoint. A ping with no timeout can hang the endpoint and make probes fail. Set a short timeout on the client and keep the check cheap.
- Exceptions escaping. Actuator catches exceptions from
health()and reportsDOWN, but you lose control over the details. Catch them yourself. - Wrong status for the situation. A failing optional dependency should not always take the whole application out of service. Return
DOWNonly if the application cannot work without it. Otherwise, consider reporting a detail or a custom status. - Leaking secrets. Details appear in the response for authorized users. Do not put credentials, URLs with tokens, or stack traces in them.
- Expecting a different name. The component name follows the bean name rule above. Rename the class if you want a different key.
Controlling the aggregate status
The overall status is the most severe component status. By default the order is DOWN, OUT_OF_SERVICE, UP, UNKNOWN. A single DOWN component makes the whole endpoint return DOWN, and the HTTP status becomes 503.
Exercise
Your application has a bean named InventoryServiceHealthIndicator. What component name appears in the health response, and what happens to the overall status if its health() method returns Health.down().build()?
Show answer
The component appears as inventoryService, because Actuator strips the HealthIndicator suffix and lowercases the first letter. The overall status becomes DOWN and the endpoint returns HTTP 503, because the aggregate uses the most severe status among all components.
Exercise
A custom indicator calls an external API with no timeout. The API stops responding. What is the effect on /actuator/health, and how do you fix it?
Show answer
Each request to the health endpoint hangs while waiting on the API, so monitoring probes time out and may report the application as unhealthy. Fix it by setting a short connect and read timeout on the client, and catching the resulting exception to return DOWN.