Mastering Spring Boot Actuator: Monitoring and Managing Your Applications

Amr KhaledAmr Khaled
6 min read

This tutorial is part of Springing into Action: A Spring Boot Journey from Novice to Pro Series, be sure to check it out for more related content!

Spring Boot Actuator is a powerful tool for monitoring and managing your Spring Boot applications at runtime. It provides a wide range of information about the application and its environment; and allows for the management of the application. In this article, we will explore the features of Spring Boot Actuator and how to add it to your project, usage and code examples to help you understand how it can improve the reliability and maintainability of your Spring Boot applications.

What is Spring Boot Actuator?

Spring Boot Actuator is a sub-project of Spring Boot that provides advanced monitoring and management capabilities for Spring Boot applications. It provides a set of endpoints that allow you to monitor and manage your application at runtime. These endpoints provide information about the application, such as its health, metrics, and configuration, as well as allow you to perform operations on the application, such as refreshing its configuration or shutting it down.

Adding Spring Boot Actuator to Your Project

Adding Spring Boot Actuator to your project is easy. All you need to do is add the spring-boot-starter-actuator dependency on your project's pom.xml file.

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

Once you've added the dependency, you can start using Actuator's endpoints by visiting http://localhost:8080/actuator in your browser.

Available Endpoints

Spring Boot Actuator provides a wide range of endpoints that allow you to monitor and manage your application at runtime. Here are some of the most commonly used endpoints:

Health Endpoint

The health endpoint provides information about the health of your application. It returns a JSON object that contains the status of the application, as well as any additional information that may be useful for monitoring the application's health. By default, the health endpoint returns only the status of the application, but you can configure it to return additional information by implementing the HealthIndicator interface.

For example, you can create a custom HealthIndicator that returns the current time:

@Component
public class TimeHealthIndicator implements HealthIndicator {

    @Override
    public Health health() {
        String currentTime = LocalDateTime.now().toString();
        return Health.up().withDetail("time", currentTime).build();
    }
}

Metrics Endpoint

The metrics endpoint provides information about the performance of your application. It returns a JSON object that contains various metrics, such as the number of requests per second, the average response time, and the memory usage of the application. You can configure which metrics are exposed by Actuator by using the management.metrics.export property.

Info Endpoint

The info endpoint provides information about the application, such as its version and build time. You can configure the information that is exposed by Actuator by using the info property in the application.properties file. For example, you can add the following properties to the file:

info.app.name=My Application
info.app.version=1.0

Configuration Endpoint

The configprops endpoint provides information about the configuration of your application. It returns a JSON object that contains all the properties defined in the application.properties or application.yml file, as well as any other configuration properties that are defined by the application's dependencies. This can be useful for troubleshooting configuration issues and for understanding the configuration of your application at runtime.

Beans Endpoint

The beans endpoint provides information about the beans that are defined in your application's context. It returns a JSON object that contains all the beans that are defined, as well as information about their scope, type, and dependencies. This can be useful for understanding how your application is constructed and for troubleshooting issues related to bean initialization.

Threads Endpoint

The threaddump endpoint provides information about the threads that are currently running in your application. It returns a JSON object that contains information about each thread, such as its name, state, and call stack. This can be useful for troubleshooting performance issues and for understanding how your application is utilizing resources.

Shutdown Endpoint

The shutdown endpoint allows you to safely shut down your application. It is disabled by default, but you can enable it by setting the management.endpoint.shutdown.enabled property to true. Once it is enabled, you can send a POST request to the shutdown endpoint to shut down the application. This can be useful for performing maintenance tasks or for shutting down the application in response to an error.

Extending Actuator Endpoints

Actuator provides a wide range of endpoints out of the box, but you may find that you need to add your custom endpoints to your application. Spring Boot Actuator makes it easy to extend the existing endpoints or to define your custom endpoints.

Extending Existing Endpoints

You can extend existing endpoints by creating a new class that implements the appropriate Endpoint interface. For example, if you want to add additional information to the health endpoint, you can create a new class that implements the HealthEndpoint interface.

@Component
public class CustomHealthEndpoint implements HealthEndpoint {

    private final HealthEndpoint delegate;

    public CustomHealthEndpoint(HealthEndpoint delegate) {
        this.delegate = delegate;
    }

    @Override
    public Health health() {
        Health health = delegate.health();
        // Add custom information to the health object
        return health;
    }
}

You can also use @EndpointExtension to extend existing endpoints. This annotation is used to indicate that a bean should be used as an extension of an existing endpoint.

@Component
@EndpointExtension(endpoint = HealthEndpoint.class)
public class CustomHealthEndpointExtension {
    // Add custom information to the health object
}

Defining Custom Endpoints

You can define your custom endpoints by creating a new class that implements the Endpoint interface. For example, you can create a new endpoint that returns information about the current time:

@Component
@Endpoint(id = "time")
public class TimeEndpoint implements Endpoint<String> {

    @Override
    public String invoke() {
        return LocalDateTime.now().toString();
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    @Override
    public boolean isSensitive() {
        return false;
    }
}

You can also use @Endpoint to define custom endpoints. This annotation is used to indicate that a bean should be registered as a new endpoint.

@Component
@Endpoint(id = "custom")
public class CustomEndpoint {

    @ReadOperation
    public String customEndpoint() {
        return "Hello from custom endpoint";
    }
}

Once you have defined your custom endpoint, you can access it by sending a GET request to the /actuator/{id} endpoint, where {id} is the ID of your custom endpoint.

By extending and defining custom endpoints, you can add new functionality to your Actuator endpoints and customize them to suit the needs of your application.

Conclusion

Spring Boot Actuator is a powerful tool for monitoring and managing your Spring Boot applications at runtime. It provides a wide range of endpoints that allow you to monitor and manage your application, from its health and performance to its configuration and resources. Actuator also allows you to extend and define custom endpoints to suit the needs of your application. By adding Actuator to your project and understanding its capabilities, you can improve the reliability and maintainability of your Spring Boot applications.

1
Subscribe to my newsletter

Read articles from Amr Khaled directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Amr Khaled
Amr Khaled

An experienced Software engineer with extensive knowledge in Fullstack development ( Java - Angular 8+ ) and Data Science.