Spring Boot Actuator

Actuator is mainly used to expose operational information about the running application — health, metrics, info, dump, env, etc. It uses HTTP endpoints or JMX beans to enable us to interact with it.

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>

management.endpoints.web.exposure.include=*

List of endpoints (More than 15 endpoints)



Here are some of the most common endpoints Boot provides out of the box:

  • /health shows application health information (a simple status when accessed over an unauthenticated connection or full message details when authenticated); it's not sensitive by default.
  • /info displays arbitrary application info; it's not sensitive by default.
  • /metrics shows metrics information for the current application; it's sensitive by default.
  • /trace displays trace information (by default the last few HTTP requests).








Custom Health Indicator

We need to extend this class and override the doHealthCheck() method.

public abstract class AbstractHealthIndicator implements HealthIndicator {
    private static final String NO_MESSAGE = null;
    private static final String DEFAULT_MESSAGE = "Health check failed";
    private final Log logger;
    private final Function<Exception, String> healthCheckFailedMessage;

    protected AbstractHealthIndicator() {
        this(NO_MESSAGE);
    }

    protected AbstractHealthIndicator(Function<Exception, String> healthCheckFailedMessage) {
        this.logger = LogFactory.getLog(this.getClass());
        Assert.notNull(healthCheckFailedMessage, "HealthCheckFailedMessage must not be null");
        this.healthCheckFailedMessage = healthCheckFailedMessage;
    }

    public final Health health() {
        Builder builder = new Builder();
        try {
            this.doHealthCheck(builder);
        } catch (Exception var3) {
            builder.down(var3);
        }

        this.logExceptionIfPresent(builder.getException());
        return builder.build();
    }

    protected abstract void doHealthCheck(Builder builder) throws Exception;
}

Sample

public class DatabaseHealthIndicator extends AbstractHealthIndicator {

    private static final Logger LOG = LoggerFactory.getLogger(DatabaseHealthIndicator.class);

    @Autowired
    private DataSource dataSource;

    /**
     * @param builder : health check
     * Overriding custom health check to check either database is UP / DOWN
     */
    @Override
    protected void doHealthCheck(Health.Builder builder) throws SQLException {
        try {
            boolean check = checkDatabase();
            if (check) {
                // The database is UP
                builder.up();
                return;
            }
            LOG.warn("Database is down");
            //The database is DOWN => give some details
            builder.down().withDetail("error", "Connection timed out").withDetail("message", "Unable to connect to database");
        } catch (Exception e) {
            LOG.error("Unknown error {}", e.toString());
            // Unable to fetch the status of the module => Set the status to UNKNOWN and give some details
            builder.unknown().withDetail("error", e.toString()).withDetail("message", "Database is down.");
        }
    }
    //This method ensures if we are able to establish connection with database or not
    private boolean checkDatabase() throws SQLException {
        try (Connection connection = dataSource.getConnection()) {
            return true;
        } catch (SQLException exe) {
            return false;
        }
    }







































Comments

Popular posts from this blog

Java 8 : Find the number starts with 1 from a list of integers

Junit Mockito and Power Mockito

Important Linux Commands