In this article, we will introduce Spring Boot Actuator, its features, and how to integrate it into your Spring Boot application.

Spring Boot Actuator is a library included in Spring Boot that provides a set of built-in endpoints to expose application metrics, health information, and operational data. These endpoints allow developers and operations teams to monitor and manage applications easily.

Some key features of Spring Boot Actuator include:

  • Health Checks: Monitor the health of your application and its dependencies.
  • Metrics: Access detailed performance metrics, such as memory usage, CPU usage, and thread activity.
  • Environment Information: View environment properties, system properties, and configuration settings.
  • Custom Endpoints: Define your own endpoints for specific monitoring needs.

To get started with Spring Boot Actuator, add the following dependency to your project's pom.xml file:

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

For Gradle projects, add this line to your build.gradle file:

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

By default, Spring Boot Actuator exposes several built-in endpoints, such as:

  • /actuator/health: Displays application health status.
  • /actuator/metrics: Shows performance metrics.
  • /actuator/env: Provides information about environment properties.
  • /actuator/info: Displays application-specific information.

Start your application and access these endpoints in your browser or using tools like curl. For example:

curl http://localhost:8080/actuator/health

The response might look like this:

{
    "status": "UP"
}

To enable or disable specific endpoints, update the application.properties file:

management.endpoints.web.exposure.include=health,info,metrics
management.endpoint.shutdown.enabled=true

The management.endpoints.web.exposure.include property specifies which endpoints are exposed over HTTP. The management.endpoint.shutdown.enabled property allows the use of the /actuator/shutdown endpoint to gracefully stop the application.

Spring Boot Actuator provides extensive metrics out of the box, including JVM memory usage, garbage collection statistics, and HTTP request metrics. To customize or add metrics, you can use the Micrometer library, which is integrated with Actuator:

import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.stereotype.Component;

@Component
public class CustomMetrics {

    public CustomMetrics(MeterRegistry registry) {
        registry.counter("custom.counter", "type", "example").increment();
    }
}

This example creates a custom metric named custom.counter that can be viewed at /actuator/metrics.

Actuator's health checks can be extended to include custom indicators. Create a custom health indicator by implementing the HealthIndicator interface:

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() {
        boolean healthy = checkServiceHealth();
        return healthy ? Health.up().build() : Health.down().withDetail("Error", "Service Unavailable").build();
    }

    private boolean checkServiceHealth() {
        // Custom health check logic
        return true;
    }
}

Once implemented, the custom health indicator will be included in the response from /actuator/health.

Spring Boot Actuator integrates seamlessly with monitoring tools like Prometheus, Grafana, and ELK Stack for advanced observability and visualization. To export Actuator metrics to Prometheus, add the following dependency:

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

With Spring Boot Actuator, you can monitor and manage your applications effortlessly, ensuring they run smoothly in production. Actuator's built-in endpoints, combined with custom metrics and health checks, provide a robust foundation for observability. In future articles, we will explore advanced monitoring techniques and integrations with external tools.