liveBook · Manning

Health indicator

As explained in Spring in Action, Sixth Edition

The HealthIndicator interface is a Spring Boot Actuator contract for components that provide application health status information.

A health indicator is a Spring Boot Actuator component that reports the status of an external system used by an application. Actuator combines these individual results into the application's overall health, which the /health endpoint can expose. section 15.2.1

Health indicators communicate one of four statuses:

  • UP means the external system is reachable. section 15.2.1
  • DOWN means the system is unavailable or unreachable. section 15.2.1
  • OUT_OF_SERVICE means the system is reachable but temporarily unavailable. section 15.2.1
  • UNKNOWN means the system's status cannot be determined; unknown results do not affect the aggregate health. section 15.2.1

Creating a custom indicator

When Spring Boot has no built-in indicator for an integrated legacy system, create a Spring bean that implements HealthIndicator and override health(). Return Health.up(), Health.down(), or Health.outOfService(), adding details with withDetail() when useful. A realistic implementation would make a remote call and translate its response into the appropriate status. section 15.3.2

Listing 15.5 An unusual implementation of HealthIndicator
package tacos.actuator;
 
import java.util.Calendar;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
 
 
@Component
public class WackoHealthIndicator
       implements HealthIndicator {
  @Override
  public Health health() {
    int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
    if (hour > 12) {
      return Health
          .outOfService()
          .withDetail("reason",
                 "I'm out of service after lunchtime")
          .withDetail("hour", hour)
          .build();
    }
 
    if (Math.random() <= 0.1) {
      return Health
          .down()
          .withDetail("reason", "I break 10% of the time")
          .build();
    }
    return Health
        .up()
        .withDetail("reason", "All is good!")
        .build();
  }
}

The complete example shows a component implementation, the required health() method, status builders, and diagnostic details. section 15.3.2

How Spring in Action, Sixth Edition covers it

How deep it goes
Defines statuses and aggregation rules, then demonstrates custom implementation without detailing remote-client integration.
Examples
Uses database, message-broker, and Spring Cloud dependencies as built-in examples, plus a deliberately unusual custom indicator.
What it assumes
Assumes a Spring Boot application and an external dependency whose availability can influence application health.
What you can do after
Build a custom health indicator by registering a bean that implements HealthIndicator and returns a detailed Health result.
How it connects
Connects HealthIndicator with Spring Boot Actuator, health endpoints, external systems, and application monitoring.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.