0 day streak
java / intermediate
Snippet

Monitoring Application Health with Custom HealthIndicators

Spring Boot Actuator allows you to define custom health indicators to monitor external dependencies or internal states. By implementing the HealthIndicator interface, you can expose specific status information via the /health endpoint.

snippet.java
java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Component
public class ExternalServiceHealthIndicator implements HealthIndicator {
@Override
public Health health() {
boolean isRunning = checkServiceStatus();
if (!isRunning) {
return Health.down()
.withDetail("Service", "Not Reachable")
.build();
}
return Health.up().withDetail("Service", "Online").build();
}
private boolean checkServiceStatus() {
// Logic to ping external service
return true;
}
}
spring
Breakdown
1
implements HealthIndicator
Standard Spring Boot interface for providing health information.
2
Health.down()
Signals that the application or component is not functioning correctly.
3
withDetail("Service", "Not Reachable")
Adds additional metadata to the health response for easier debugging.