NashTech Blog

Spring Boot Actuator: Comprehensive Guide and Use Cases

Table of Contents
spring-boot-actuator

1. Introduction

Spring Boot Actuator provides a set of production-ready features that help you monitor and manage your Spring Boot application. It offers several out-of-the-box endpoints, such as health, metrics, environment, and many others, which can be customized to suit your application’s needs.

2. Key Features of Spring Boot Actuator

Health Checks:

One of the most important features provided by Spring Boot Actuator is the health check endpoint. It allows you to monitor the health of your application and its components. When you access the /actuator/health endpoint, you’ll get detailed information on whether your application is functioning as expected. This feature can be expanded to include health checks for databases, message queues, third-party services, etc.

Default Endpoint: /actuator/health

Custom Health Indicators: You can define your custom health checks for specific services or components of your application.

Metrics:

Spring Boot Actuator can automatically collect various application metrics, such as memory usage, garbage collection, active threads, request timings, and more. These metrics help identify performance bottlenecks, understand the load, and make data-driven decisions about scaling your application.

Default Endpoint: /actuator/metrics

Popular Metrics: JVM memory usage, request count, active threads, HTTP request timing, and database connections.

Auditing:

Spring Boot Actuator provides built-in auditing functionality, which helps in tracking security events, such as successful or unsuccessful login attempts. This can be especially useful in applications where security is a priority, as it allows you to monitor and log any security-related activities.

Default Endpoint: /actuator/auditevents

Use Cases: Login attempts, changes to user roles, or any other security-related activities.

Environment Information:

The /actuator/env endpoint uncovers a detailed concept of the environment properties that your Spring Boot application is running in. This includes information such as system properties, environment variables, and configuration properties defined in the application.properties or application.yml files.

Default Endpoint: /actuator/env

Use Cases: Debugging, displaying application configuration, or viewing environment properties.

Thread Dump:

The thread dump endpoint provides a snapshot of the threads that are running in your application. It can be especially useful for identifying performance issues or deadlocks in a multi-threaded environment. When you face problems like sluggish performance or unresponsiveness, a thread dump can help identify the root cause.

Default Endpoint: /actuator/threaddump

Use Cases: Troubleshooting performance problems, identifying resource-intensive threads.

Customizable Endpoints:

One of the powerful features of Spring Boot Actuator is that you can enable or disable built-in endpoints, change their base path, or even create your custom endpoints. This flexibility allows you to tailor Actuator to your application’s specific requirements.

Enable/Disable Endpoints: Modify the application.properties or application.yml to expose only the endpoints you need.

Custom Endpoints: You can write your own endpoints to expose specific business logic, such as adding custom health checks or metrics.

3. Configuring Spring Boot Actuator

By default, Spring Boot exposes only the /health and /info endpoints for security and simplicity. To expose additional endpoints, you need to modify your application.properties or application.yml file.

Here’s an example of how to expose all the endpoints:

# application.properties
management.endpoints.web.exposure.include=*

This will expose all available endpoints, such as /actuator/metrics, /actuator/env, /actuator/threaddump, etc.

You can also choose to expose specific endpoints:

# application.properties
management.endpoints.web.exposure.include=health,info,metrics,threaddump

Securing Endpoints:

For security reasons, sensitive endpoints such as env and heapdump are disabled by default. You can secure these endpoints by adding security configurations in your application.

Example configuration to enable basic authentication for Actuator endpoints:

# application.properties
management.endpoints.web.exposure.include=*
spring.security.user.name=admin
spring.security.user.password=admin

This configuration ensures that only authorized users can access sensitive endpoints like env and threaddump.

4. Advanced Actuator Features:

Creating Custom Endpoints:

Spring Boot allows you to create your own actuator endpoints to expose custom application metrics, health indicators, or internal states. The Spring Boot Actuator supports the creation of custom endpoints to extend its functionality. You can define custom endpoints using a class annotated with @Endpoint, and individual methods within that class can be annotated with @ReadOperation, @WriteOperation, or @DeleteOperation to handle GET, POST, and DELETE HTTP requests respectively. To expose these endpoints over HTTP, you must also configure appropriate security settings. You can restrict access to these sensitive endpoints using a SecurityFilterChain.

Key Annotations:

  • @Endpoint: Marks a class as a custom endpoint to be exposed by the Actuator.
  • @ReadOperation: Handles GET requests; used to retrieve data from the custom endpoint.
  • @WriteOperation: Handles POST requests; used to submit or modify data.
  • @DeleteOperation: Handles DELETE requests; used to remove data via the custom endpoint.


Here’s how to create a custom endpoint:

import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
import org.springframework.boot.actuate.endpoint.annotation.DeleteOperation;
import org.springframework.stereotype.Component;
import java.util.*;

@Component
@Endpoint(id = "customdata")
public class CustomEndpoint {
    private List<String> data = new ArrayList<>(List.of("Initial"));

    @ReadOperation
    public List<String> getData() {
        return data;
    }
    @WriteOperation
    public void addData(String value) {
        data.add(value);
    }
    @DeleteOperation
    public void clearData() {
        data.clear();
    }
}

Outcome: Endpoint URLs:

Once the custom endpoint is enabled via configuration, you can access it through the following URLs:

  • GET /actuator/customdata – Retrieves the current data.
  • POST /actuator/customdata – Adds new data to the list.
  • DELETE /actuator/customdata – Removes all existing data.

NOTE:

  • Spring Security can be used to protect custom Actuator endpoints like /actuator/customdata.
  • You can configure security to ensure that only users with specific roles (e.g., ADMIN) are allowed to access these endpoints.
  • All other requests can be set to require authentication before access is granted.
  • User credentials (like username and password) can be defined in the application configuration(application.properties) to support basic authentication.
  • Securing these endpoints ensures that internal operational data is not publicly accessible, which is important for maintaining application integrity and security.

5. Use Cases for Spring Boot Actuator

Monitoring Production Systems:

Spring Boot Actuator is widely used to monitor Spring Boot applications in production. With endpoints like /actuator/metrics, /actuator/health, and /actuator/threaddump, operators can easily monitor the health, performance, and resource utilization of their application. This helps in proactive management of resources and preventing downtime.

Automated Alerts:

Integrating Spring Boot Actuator with monitoring tools like Prometheus or Grafana allows you to create automated alarms for particular circumstances, including high memory use or system health degradation.This enables us to take preventative measures before problems escalate.

Custom Business Metrics:

You can use Spring Boot Actuator to collect business-specific data like transaction counts, API response times, and customer interactions. By providing these metrics via the /actuator/metrics endpoint, you can measure business KPIs and analyse your application’s overall performance.

Security Auditing:

For applications that require security auditing, Spring Boot Actuator’s auditing features (/actuator/auditevents) enable you to track critical security events. This is critical for regulatory compliance and protecting your application from unauthorised access.

Conclusion:

One useful tool for application management and monitoring is Spring Boot Actuator. You can make custom endpoints in addition to the default ones to handle different HTTP operations and expose application-specific data. These endpoints can be safeguarded with Spring Security to guarantee that only authorized users have access to private data, which is necessary for production use. Spring Boot 3 includes new features. Micrometer tracing and improved observability integrations are two examples of x that increase Actuator’s power. Overall, becoming proficient with Actuator improves the security, insight, and production readiness of your applications.

By implementing it into your apps, you can monitor crucial aspects, collect metrics, diagnose problems, and secure sensitive processes right out of the box!

Picture of rishikakumari20

rishikakumari20

Leave a Comment

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

Suggested Article

Scroll to Top