This article provides a step-by-step guide on how to set up monitoring for a Spring Boot application using Prometheus and Grafana.
Abstract
The article begins by explaining the importance of monitoring microservices for maintaining a stable production system and understanding how the system behaves under different load conditions. It then introduces Prometheus and Grafana as tools for monitoring a Spring Boot application. The tutorial covers configuring Spring Boot Actuator to enable metrics, configuring Prometheus to scrape the metrics, querying various metrics using PromQL in the Prometheus UI, and visualizing the metrics in a Grafana Dashboard. The article assumes basic Spring Boot knowledge and focuses on the monitoring configuration part.
Bullet points
Monitoring microservices is crucial for maintaining a stable production system.
Prometheus and Grafana are tools for monitoring a Spring Boot application.
The tutorial covers configuring Spring Boot Actuator to enable metrics.
The tutorial covers configuring Prometheus to scrape the metrics.
The tutorial covers querying various metrics using PromQL in the Prometheus UI.
The tutorial covers visualizing the metrics in a Grafana Dashboard.
The article assumes basic Spring Boot knowledge and focuses on the monitoring configuration part.
How To Monitor a Spring Boot App With Prometheus and Grafana
A step-by-step guide on how to set up monitoring for a Spring Boot app
Monitoring microservices is a challenging task. It’s a crucial step toward having a stable production system. By observing various metrics, we understand how the system behaves under different load conditions and which metrics need more attention.
In this tutorial, I’ll show you how to set up Prometheus and Grafana to monitor a Spring Boot application.
You’ll learn how to:
Configure Spring Boot Actuator to enable metrics
Configure Prometheus to scrape the metrics
Use PromQL to query various metrics in the Prometheus UI
Visualize the metrics in a Grafana Dashboard
This article assumes you have basic Spring Boot knowledge. We’ll concentrate on the monitoring configuration part.
Let’s get started!
Prepare the Project
Understand the components
First, let’s take a look at the diagram below to understand how the monitoring works:
Monitoring a spring boot app big picture
The Spring Boot app has an Actuator module that allows us to monitor and manage our app. It integrates flawlessly with third-party monitoring tools, such as Prometheus.
Micrometer collects the metrics from our app and exposes them to external systems, in this case, Prometheus.
Grafana is a visual tool that shows the metrics from a data source (e.g. Prometheus) in a Dashboard.
Add the dependencies
I’ve prepared a simple Spring Boot project for this demo. You can find the complete source code under the references section.
If we want to view the data in Prometheus, we can query it using PromQL, which is the Prometheus query language.
Querying Prometheus metrics
Great! The scraping part works. We can see the metrics in Prometheus.
Although Prometheus has a decent UI, Grafana’s Dashboard is more powerful. So, let’s visualize the metrics in Grafana.
Configure Grafana
First, we need to add Prometheus as a Data source. You need your local IP address and 9090 as the port from Prometheus. Of course, this is used for local testing only. In real life, you would have a Prometheus server running somewhere.
Click Save and Test at the end of the screen. You should see a message that the data source has been connected.
Go to Dashboards -> New Dashboard -> Add panel.
Create a general Prometheus dashboard
You should see Prometheus as the data source. Now we can query all metrics.
Querying metrics in Grafana from Prometheus data source
Awesome! It works as expected.
Import a Predefined Dashboard in Grafana
We can also import a ready-to-use dashboard. Grafana offers something like a marketplace where we can choose from numerous templates. Since we have a Spring Boot app, we can take advantage of one of the pre-configured dashboards for spring boot.
Go to Dashboards -> New Dashboard -> Import.
We can either search for the dashboard or just enter the template id into the Import field:
In this example, the id is 12900. Click Load. Choose Prometheus as the data source and click Import.
Great! You now have a complete Spring Boot Dashboard:
Spring Boot template Grafana Dashboard
That’s it! It was pretty simple to configure the monitoring part.
Conclusion
Now you know how to scrape and visualize various metrics from a Spring Boot app using Actuator, Prometheus, and Grafana.
The setup was elementary, without any customization and authentication. There is still much more that you could do to monitor your app. For example, you can create your custom endpoints and metrics. Only authorized users may access specific information.
I plan to create an advanced tutorial where I’ll show you how to create custom endpoints and add security.
Thank you for reading, and stay tuned for the next part!