Spring Boot Actuator and Monitoring

Spring Boot Actuator is a sub-project of Spring Boot that provides production-ready features to help you monitor and manage your application. Actuator includes a number of built-in endpoints that allow you to interact with your application and view its health and metrics.


Key Features of Spring Boot Actuator

1. Health Checks: Provides endpoints to check the health of your application.

2. Metrics: Collects and exposes metrics about your application.

3. Auditing: Supports application auditing.

4. HTTP Tracing: Provides HTTP request tracing.

5. Environment Info: Exposes environment information and configuration properties.


Setting Up Spring Boot Actuator

Step 1: Add Dependency

To use Actuator, you need to add the `spring-boot-starter-actuator` dependency to your `pom.xml`:

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


Step 2: Enable Actuator Endpoints

By default, Actuator endpoints are disabled. You can enable them by adding the following properties to your `application.properties`:

management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always


Step 3: Access Actuator Endpoints

Once Actuator is enabled, you can access various endpoints. Here are some commonly used endpoints:

- `/actuator/health`: Provides health status of the application.

- `/actuator/metrics`: Provides metrics data.

- `/actuator/info`: Provides application information.

- `/actuator/env`: Exposes environment properties.

- `/actuator/loggers`: Allows you to query and modify the logging levels.


Customizing Actuator Endpoints

You can customize which endpoints are enabled or disabled by configuring them in the `application.properties` file. For example:

management.endpoints.web.exposure.include=health,info
management.endpoints.web.exposure.exclude=env


Securing Actuator Endpoints

Actuator endpoints can expose sensitive information, so it's important to secure them. You can secure Actuator endpoints using Spring Security.


Step 1: Add Spring Security Dependency

Add the `spring-boot-starter-security` dependency to your `pom.xml`:

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


Step 2: Configure Security

Configure Spring Security to secure the Actuator endpoints. Here is an example configuration:

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.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    @Override
    protected UserDetailsService userDetailsService() {
        InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
        manager.createUser(User.withDefaultPasswordEncoder()
                .username("user")
                .password("password")
                .roles("USER")
                .build());
        return manager;
    }

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


Monitoring with Actuator

Step 1: View Health Information

You can access the health endpoint to view the application's health status. By default, it will show a simple status.

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


Response:

{
    "status": "UP"
}


Step 2: View Metrics

You can access the metrics endpoint to view various metrics about your application.

curl http://localhost:8080/actuator/metrics


This will return a list of available metrics. You can query specific metrics by appending their names to the URL.

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


Integrating with Monitoring Tools

Spring Boot Actuator can be integrated with various monitoring tools like Prometheus, Grafana, and others.


Example: Integrating with Prometheus

Step 1: Add Prometheus Dependency

Add the Prometheus dependency to your `pom.xml`:

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


Step 2: Configure Prometheus Endpoint

Enable the Prometheus endpoint by adding the following property to your `application.properties`:

management.endpoints.web.exposure.include=prometheus


Step 3: Access Prometheus Metrics

Access the Prometheus metrics at the `/actuator/prometheus` endpoint.

curl http://localhost:8080/actuator/prometheus


Conclusion

Spring Boot Actuator is a powerful tool that provides various features for monitoring and managing your Spring Boot applications. It helps in gaining insights into the application's health, metrics, environment, and more. By securing the Actuator endpoints and integrating with monitoring tools like Prometheus, you can ensure your application is well-monitored and managed effectively.

Nenhum comentário:

Postar um comentário

Internet of Things (IoT) and Embedded Systems

The  Internet of Things (IoT)  and  Embedded Systems  are interconnected technologies that play a pivotal role in modern digital innovation....