- Karol Sobota
- Read in min.
In today’s world of microservices and distributed systems, frequently observing the performance and health of your applications is essential for ensuring their stability and uptime. For this matter, application performance monitoring (APM) tools can come in handy.
In this article, we will look at how to properly monitor a Spring Boot application using two popular open-source APM tools: Prometheus and Grafana.
What are APM tools?
Application performance monitoring (APM) tools are technologies used to ensure that enterprise solutions meet the performance and reliability. The term also refers to application performance management (APM), which is a broader concept.
A well-designed performance monitoring system will keep track of all business dependencies, because once administrators have access to comprehensive data, provided by APM tools, they can diagnose, isolate, and resolve performance issues.
Prometheus and Grafana in a nutshell – compatible application performance management tools
Prometheus is a time-series database and application performance monitoring tool. It provides a powerful query language and a broad range of integrations, making it a popular choice for keeping an eye on applications and infrastructure.
Grafana, on the other hand, is an application performance management tool for visualization. It allows you to create and display dashboards with real-time metrics data. It integrates seamlessly with Prometheus, allowing you to easily create dashboards and alerts based on your Prometheus data.
These two APM tools give access to many important details about application performance straight out of the box.
Setting up application performance monitoring tools
In this tutorial, we will use Docker to set up and run Prometheus and Grafana. This will make it easy to get started and provide a consistent environment for development and production deployments.
Creating a Spring Boot Application
To start, let’s create a new Spring Boot application using the Spring Initializr. In the Initializr, select the latest version of Spring Boot and add the following dependencies:
- Spring Web
- Spring Boot Actuator
- Prometheus Metrics Exporter
This will create a simple Spring Boot application that exposes some basic metrics about the application through the Actuator endpoints. We will use the Prometheus Metrics Exporter to expose these metrics in a format that Prometheus can scrape.
Configuring Prometheus and Grafana APM tools
Next, let’s set up application performance monitoring tools – Prometheus and Grafana using Docker. To do this, create a new file called docker-compose.yml in the root of your project with the following content.
version: '3' services: prometheus: image: prom/prometheus ports: - 9090:9090 volumes: - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml command: - '--config.file=/etc/prometheus/prometheus.yml' grafana: image: grafana/grafana ports: - 3000:3000 environment: - GF_SECURITY_ADMIN_PASSWORD=password
This file defines two Docker containers: one for Prometheus and one for Grafana.
The Prometheus container is configured to mount the prometheus.yml file from the Prometheus directory in the project root. This file is where we will configure Prometheus to scrape our application metrics.
Next, let’s create the prometheus.yml file with the following content:
global: scrape_interval: 15s scrape_configs: - job_name: spring-boot-application static_configs: - targets: - localhost:8080
This configuration file tells Prometheus to scrape metrics from our Spring Boot application every 15 seconds. Thanks to that we will get access to almost real-time performance data. With this in place, we can now start our Docker containers using the docker-compose command:
$ docker-compose up
This will start the Prometheus and Grafana containers and make them available on the following URLs:
Prometheus: http://localhost:9090
Grafana: http://localhost:3000
To view the metrics from our Spring Boot application in Prometheus, open the Prometheus UI and run the following query:
http_server_requests_seconds_count{job="spring-boot-application"}
This query will show the count of HTTP requests made to our Spring Boot application. You should see a time series graph with the data from the past 15 seconds (the interval we configured in the prometheus.yml file).
Visualizing Metrics in Grafana
Now, let’s create a dashboard in application performance management tool – Grafana to display the metrics from our Spring Boot application. To do this, open the Grafana UI and follow these steps:
- Click the “Add data source” button in the top-right corner.
- Select “Prometheus” as the type of data source.
- Enter “http://prometheus:9090” as the URL of the Prometheus server.
- Enter “spring-boot-application” as the default Prometheus data source.
- Click the “Save & Test” button to save the data source.
With the Prometheus data source configured, we can now create a new dashboard to display the metrics from our Spring Boot application. To do this, follow these steps.
- Click the “Create dashboard” button in the top-right corner.
- Click the “Add panel” button to add a new panel to the dashboard.
- In the “Metrics” tab, enter the following query to display the HTTP request count:
http_server_requests_seconds_count{job="spring-boot-application"}
- In the “General” tab, give the panel a title and click the “Save” button.
Your dashboard should now display the HTTP request count for your Spring Boot application and provide you with application performance monitoring features. You can also add additional panels to display other metrics, such as memory usage or CPU utilization.
Benefits from application performance monitoring (APM) tool
With an APM solution you get more value for less effort. Application performance monitoring tool allows you for automatic infrastructure monitoring almost instantly.
Thanks to application performance management you can ensure better quality of your services, as APM tools help detect and show performance issues. This will give you time to fix discovered problems before users are impacted.
Whatmore, you can turn your resources toward developing the solution rather than maintaining it. Application performance monitoring tool eliminates slowdowns because you don’t have to spend your time tracking and fixing every problem, instead a software does that for you.
Summary
Prometheus and Grafana are powerful tools for application performance management, and they are particularly well-suited for observing Spring Boot applications.
By following the steps in this tutorial, you can quickly set up Prometheus and Grafana to track your application’s infrastructure performance and get insights into its diagnostics.
Check out other articles in the technology bites category
Discover tips on how to make better use of technology in projects