Springboot App Monitoring With Prometheus And Grafana

Vineet Kumar
4 min readNov 29, 2023

--

Monitoring plays a pivotal role in ensuring the optimal performance and health of Spring Boot applications. In this article, we’ll explore the integration of Prometheus and Grafana to establish a robust monitoring solution.

Introduction

Spring Boot applications, renowned for their robustness and scalability, benefit greatly from effective monitoring. Prometheus and Grafana, a potent combination, provide a seamless solution for monitoring and visualizing critical metrics.

Understanding Prometheus and Grafana

Prometheus, an open-source monitoring and alerting toolkit, excels in collecting time-series data. Grafana, an open-source analytics and monitoring platform, seamlessly integrates with Prometheus, offering rich visualization capabilities.

Setting Up Prometheus and Micrometer

Begin by adding the following dependencies to pom.xml file of your spring boot application.

  <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<scope>runtime</scope>
</dependency>

Micrometer : Micrometer serves as a bridge between Spring Boot applications and Prometheus, facilitating the export of metrics.It is used to configure Spring Boot applications to expose metrics in a format that Prometheus can scrape and store.
After adding these dependencies when we start the Spring Boot application and make a call tohttp://localhost:8080/actuator endpoint, it will display various default health information endpoints .And if we make call to
http://localhost:8080/actuator/prometheus , we will be able to see some metrics like this

# HELP jvm_memory_used_bytes The amount of used memory
# TYPE jvm_memory_used_bytes gauge
jvm_memory_used_bytes{area="heap",id="used",} 2.50048E7
jvm_memory_used_bytes{area="nonheap",id="used",} 1.348944E7

# HELP http_server_requests_seconds The HTTP server request metrics
# TYPE http_server_requests_seconds summary
http_server_requests_seconds_count{method="GET",status="200",uri="/api/endpoint"} 5.0
http_server_requests_seconds_sum{method="GET",status="200",uri="/api/endpoint"} 10.0

# HELP my_custom_metric A custom metric
# TYPE my_custom_metric counter
my_custom_metric 42.0

Instrumenting Spring Boot with Prometheus

Micrometer provides various annotations which we can use in the code and the data will be provided in the /prometheus api .
Here’s a brief description of commonly used annotations in Micrometer

Timed annotation
The @Timed annotation is used to measure the duration of a method's execution and record it as a timer metric. This is useful for tracking how long specific operations take within your application.

import io.micrometer.core.annotation.Timed;

@Service
public class MyService {

@Timed(value = "my.service.operation", description = "Time taken to execute the operation")
public void performOperation() {
// Method implementation
}
}

In this example, the performOperation method is annotated with @Timed. This will result in a timer metric named "my.service.operation" being recorded each time the method is invoked.

Counted annotation
The @Counted annotation is used to count the number of times a method is invoked. It records a counter metric for the annotated method.

Example:

import io.micrometer.core.annotation.Counted;

@Service
public class MyService {

@Counted(value = "my.service.invocations", description = "Number of times the service is invoked")
public void invokeService() {
// Method implementation
}
}

In this example, the invokeService method is annotated with @Counted. This will result in a counter metric named "my.service.invocations" being incremented each time the method is called.

Gauge annotation
The @Gauge annotation is used to expose a gauge metric, which represents a single numerical value that can go up or down. It is typically used for monitoring dynamic values, such as the size of a queue or the number of active threads.
Example:

@Gauge("my.queue.size")
public int getQueueSize() {
// Return the current size of the queue
}

Histogram annotation
The @Histogram annotation is used to record distribution summary metrics. It's suitable for measuring the distribution of values, like response times.
Example:

@Histogram("my.response.time")
public void processRequest() {
// Measure response time
}

These annotations help you capture various aspects of your application’s behaviour and performance. You can choose the annotations that best fit your use case and apply them to methods or components that you want to monitor.

Visualizing Metrics with Grafana

Visualizing metrics with Grafana is a common practice in monitoring and managing applications. To visualize metrics from your Spring Boot application, which uses Spring Boot Actuator and Micrometer, you can follow these general steps:

Step 1: Set Up Grafana
Install Grafana:
Follow the official installation guide for Grafana to set it up on your server or local environment. You can find installation instructions on the Grafana website.

Start Grafana:
Once installed, start the Grafana server.

Step 2: Configure Data Source
Add Prometheus Data Source:
Grafana supports various data sources, and Prometheus is commonly used with Spring Boot Actuator.

Add Prometheus as a data source in Grafana:
Open the Grafana web interface.
Navigate to “Settings” -> “Data Sources” -> “Add your first data source.”
Choose “Prometheus” from the list and configure the connection settings (usually the URL where Prometheus is running).

Step 3: Create Dashboards
Create a New Dashboard:

In Grafana, go to the “+” menu on the left sidebar and select “Dashboard.”
Click on “Add new panel” to add panels for visualizing specific metrics.
Configure Panels:

Choose the Prometheus data source for your panel.
Use PromQL queries to select the metrics you want to visualize. For example, to visualize JVM memory usage, you might use a query like jvm_memory_used.

Customize and Arrange:
Customize each panel based on your preference, adjusting settings like axes, legend, and visualization type.
Arrange the panels on the dashboard to create a meaningful representation of your metrics.

Step 4: Save and Share Dashboards
Save Dashboard:

Once you’ve configured and arranged your panels, save the dashboard.
Share Dashboard:

Grafana allows you to export and import dashboards. You can share your dashboard configuration with others or import dashboards created by the community.

Alerting with Grafana

Moreover Grafana also offers a built-in alerting system that allows us to set up rules and conditions based on our metrics. When the defined thresholds are breached, Grafana can trigger notifications via various channels like PagerDuty, slack etc.

Conclusion

Monitoring Spring Boot applications with Prometheus and Grafana transforms the development and operations landscape. By following these integrated guidelines, developers seamlessly incorporate these powerful tools into their workflows, gaining valuable insights into application performance and health.

--

--