Spring Boot Actuator: Monitoring and Health Checks Explained

Spring Boot Actuator provides production-ready features to help monitor and manage applications. This guide explains how to set up and use Actuator for monitoring and health checks in a Spring Boot application.

1. Add Spring Boot Actuator Dependency

First, include the Actuator dependency in your pom.xml:

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

For Gradle users, add the following line to build.gradle:

implementation 'org.springframework.boot:spring-boot-starter-actuator'

2. Enable Actuator Endpoints

By default, only the /actuator/health and /actuator/info endpoints are enabled. To enable more endpoints, configure application.properties:

management.endpoints.web.exposure.include=*

You can also expose specific endpoints instead of all:

management.endpoints.web.exposure.include=health,info,metrics

3. Accessing Actuator Endpoints

After starting the application, you can access the following endpoints:

  • Health Check: http://localhost:8080/actuator/health

  • Application Info: http://localhost:8080/actuator/info

  • Metrics: http://localhost:8080/actuator/metrics

  • Beans: http://localhost:8080/actuator/beans

4. Customizing the Health Endpoint

You can customize the health check by adding properties to application.properties:

management.endpoint.health.show-details=always

To create a custom health indicator, implement HealthIndicator:

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class CustomHealthIndicator implements HealthIndicator {
    @Override
    public Health health() {
        // Custom logic to check system status
        boolean isHealthy = checkSystemHealth();
        return isHealthy ? Health.up().build() : Health.down().withDetail("error", "System Issue").build();
    }
    private boolean checkSystemHealth() {
        return true; // Replace with actual logic
    }
}

5. Securing Actuator Endpoints

To secure sensitive Actuator endpoints, configure Spring Security:

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

Define security settings in SecurityConfig.java:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
public class SecurityConfig {
    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
            .authorizeHttpRequests(auth -> auth
                .requestMatchers("/actuator/**").hasRole("ADMIN")
                .anyRequest().authenticated()
            )
            .httpBasic();
        return http.build();
    }
}

6. Monitoring with Prometheus and Grafana

6.1 Enable Prometheus Metrics

Add the Prometheus dependency:

<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

Configure Prometheus metrics in application.properties:

management.metrics.export.prometheus.enabled=true

Access metrics at http://localhost:8080/actuator/prometheus.

6.2 Visualizing Metrics with Grafana

  1. Install and run Prometheus and Grafana.

  2. Configure Prometheus to scrape metrics from your Spring Boot app.

  3. Add Prometheus as a data source in Grafana.

  4. Create dashboards for real-time monitoring.

Conclusion

Spring Boot Actuator simplifies application monitoring, health checks, and performance tracking. By integrating security, custom health indicators, and tools like Prometheus and Grafana, you can enhance observability in production environments.

Related post

Leave a Reply

Your email address will not be published. Required fields are marked *