Loading...

How to Monitor Your Spring Boot Application with Spring Actuator and Micrometer

Performance
June 16, 2023
4 minutes to read
Share this post:

In the world of application development using Spring Boot, it’s crucial to focus not just on the development aspect but also on performance monitoring. In this extensive guide, we’ll explore the steps to set up monitoring for your Spring Boot application using Spring Actuator and Micrometer, and how to employ Prometheus and Grafana for effective metrics handling and visualization.

Understanding Spring Boot Monitoring

Monitoring forms an essential part of ensuring the health of an application. It allows developers to verify the smooth running of the system and detect any potential issues. In the context of Spring Boot, several tools, such as Spring Actuator and Micrometer, make application monitoring an effortless process.

Spring Actuator introduces production-grade features to your application, revealing numerous endpoints for monitoring and managing your application. In contrast, Micrometer offers a simple mechanism to instrument your code for metrics collection, which can then be forwarded to various monitoring systems.

Let’s delve deeper into these tools and their setup process.

Integrating Spring Actuator

The first step is to incorporate the Spring Actuator dependency into your Spring Boot application. If you’re using Maven, this can be done by adding the following code to your pom.xml file:

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

Once integrated, Spring Actuator will automatically reveal several endpoints such as /health and /info. However, most of these endpoints are secured by default. To enable them, you need to append the following properties to your application.properties file:

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

Implementing Micrometer

Working harmoniously with Spring Actuator, Micrometer allows you to gather various metrics from your application. You can introduce Micrometer to your application by adding the following dependency in your pom.xml:

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

With Micrometer integrated, you can monitor request rates using the following code snippet. Let’s consider an example Spring MVC controller:

@RestController
public class SampleController {

    @Autowired
    private MeterRegistry registry;

    @RequestMapping("/hello")
    public String hello() {
        registry.counter("requests_to_hello_endpoint").increment();
        return "Hello World!";
    }
}

The MeterRegistry instance allows you to create and increment a counter metric named requests_to_hello_endpoint each time the /hello endpoint is accessed.

Connecting to Prometheus and Grafana

With Spring Actuator and Micrometer in place, you need a robust monitoring system like Prometheus and a powerful visualization tool like Grafana to make the most out of your collected metrics. You can run these tools locally or inside a Docker container.

To set up Prometheus, you need to add a configuration file prometheus.yml at the root folder:

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'spring-actuator'
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ['<Your Spring Boot Application IP>:<Port>']

Next, you can use Grafana to visualize the data collected by Prometheus.

Setting Up Prometheus and Grafana with Docker Compose

Running Prometheus and Grafana in a Docker environment simplifies the setup process, thereby ensuring a clean and organized system. This can be achieved using Docker Compose.

Firstly, create a docker-compose.yml file in the root directory of your project:

version: '3'
services:
  prometheus:
    image: prom/prometheus:latest
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
    ports:
      - 9090:9090

  grafana:
    image: grafana/grafana:latest
    depends_on:
      - prometheus
    ports:
      - 3000:3000

This configuration sets up Prometheus with the prometheus.yml configuration file (also to be located in the root directory of your project). Prometheus will be exposed on port 9090. Grafana is also set up, dependent on Prometheus, and exposed on port 3000.

Execute docker compose up from the directory housing your docker-compose.yml file. This command will start Prometheus and Grafana.

Connecting Your Spring Boot Application

For Prometheus to collect metrics, your Spring Boot application needs to be accessible. Hence, make sure that your application is running and can be accessed over the network.

To facilitate this, in your prometheus.yml file, under scrape_configs, replace <localhost>:<Your Spring Boot Application Port> with your running Spring Boot application’s localhost address and port. Prometheus will scrape metrics from the /actuator/prometheus endpoint.

static_configs:
- targets: ['localhost:8080']

Now, with your Spring Boot application running locally and Prometheus running in a Docker container, you might face issues with connectivity due to the different networking contexts. You can overcome this by using host.docker.internal instead of localhost when running on Docker.

static_configs:
- targets: ['host.docker.internal:8080']

For Grafana, once both services are running, navigate to localhost:3000 in your web browser (replace localhost with your Docker host IP if necessary). The default username and password for Grafana are admin. After logging in, add Prometheus as a data source. Enter http://prometheus:9090 as the URL and click “Save & Test”. If everything is set up correctly, Grafana will indicate a working data source.

With Docker Compose, setting up a monitoring stack with Prometheus and Grafana becomes straightforward, and connecting it to your locally running Spring Boot application ensures a robust application monitoring solution.

Conclusion

Monitoring your Spring Boot application is an essential step in maintaining its health and performance. By leveraging Spring Actuator and Micrometer, and integrating them with robust tools like Prometheus and Grafana, you can monitor your application effectively, collect valuable metrics, and visualize them for easy interpretation. With these tools at your disposal, you’ll be well-equipped to identify and tackle potential issues before they escalate.

Have you heard of Marcus' Backend Newsletter?

New ideas. Every week!
Top