Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Spring Boot Actuator

Spring Boot Actuator provides a set of production-ready features to help you monitor and manage your application. This guide covers the key concepts and steps for using Spring Boot Actuator, including enabling actuator endpoints, customizing endpoint behavior, and securing endpoints.

Key Concepts of Spring Boot Actuator

  • Actuator Endpoints: Predefined endpoints that provide information about the application’s health, metrics, environment, and more.
  • Health Indicators: Provide information about the health status of various parts of the application.
  • Metrics: Collect and expose various application metrics such as memory usage, CPU usage, and request counts.
  • Security: Secure actuator endpoints to restrict access to sensitive information.

Enabling Actuator Endpoints

Include the Spring Boot Actuator dependency in your pom.xml file:

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

By default, several actuator endpoints are enabled. You can access them at http://localhost:8080/actuator. Common endpoints include:

  • /actuator/health: Displays application health information.
  • /actuator/info: Displays arbitrary application information.
  • /actuator/metrics: Displays various application metrics.
  • /actuator/env: Displays environment properties.
  • /actuator/beans: Displays a list of beans in the application context.

Customizing Endpoint Behavior

Customize actuator endpoints by configuring properties in the application.properties or application.yml file:

application.properties

# Enable all endpoints
management.endpoints.web.exposure.include=*

# Customize health endpoint
management.endpoint.health.show-details=always

# Customize info endpoint
management.endpoint.info.enabled=true

# Enable metrics for JVM memory usage
management.metrics.enable.jvm.memory=true

Creating Custom Endpoints

Create custom actuator endpoints by defining beans annotated with @Endpoint:

CustomEndpoint.java

// CustomEndpoint.java
package com.example.demo.actuator;

import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.Map;

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

    @ReadOperation
    public Map<String, String> custom() {
        return Collections.singletonMap("message", "Hello from custom endpoint!");
    }
}

Securing Actuator Endpoints

Secure actuator endpoints by configuring Spring Security:

SecurityConfig.java

// SecurityConfig.java
package com.example.demo.config;

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.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/actuator/**").hasRole("ADMIN")
                .anyRequest().authenticated()
                .and()
            .httpBasic();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

Viewing Metrics

Spring Boot Actuator provides various metrics out-of-the-box. You can view these metrics by accessing the /actuator/metrics endpoint:

http://localhost:8080/actuator/metrics

Example: Viewing a Specific Metric

http://localhost:8080/actuator/metrics/jvm.memory.used

Integrating with Monitoring Systems

Spring Boot Actuator can integrate with external monitoring systems like Prometheus, Graphite, and others. To export metrics to Prometheus, include the following dependency:

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

Key Points

  • Actuator Endpoints: Predefined endpoints that provide information about the application’s health, metrics, environment, and more.
  • Health Indicators: Provide information about the health status of various parts of the application.
  • Metrics: Collect and expose various application metrics such as memory usage, CPU usage, and request counts.
  • Security: Secure actuator endpoints to restrict access to sensitive information.
  • Include the Spring Boot Actuator dependency to enable actuator endpoints.
  • Customize actuator endpoints by configuring properties in the application.properties or application.yml file.
  • Create custom actuator endpoints by defining beans annotated with @Endpoint.
  • Secure actuator endpoints by configuring Spring Security.
  • Integrate with external monitoring systems like Prometheus by including the appropriate dependencies.

Conclusion

Spring Boot Actuator provides a set of production-ready features to help you monitor and manage your application. By enabling and customizing actuator endpoints, creating custom endpoints, securing endpoints, and integrating with external monitoring systems, developers can gain valuable insights into their applications and ensure they are running smoothly. Happy coding!