Providing Technology Training and Mentoring For Modern Technology Adoption
This tutorial is adapted from Web Age course Microservices Development Bootcamp with Immersive Project.
In this tutorial, you will use Prometheus to monitor a Spring Boot application. You will configure the Spring Boot application to enable metrics gathering via Spring Boot Actuators and expose Prometheus endpoint. Then, you will run Prometheus in a Docker container and configure it to monitor your Spring Boot application.
Before you start working on this tutorial:
In this part, you will create a directory and copy an existing project to it.
1. Open a new Terminal window by clicking Applications > Terminal.
2. Create a directory where you will copy the Spring Boot application:
mkdir -p /home/wasadmin/TutorialWorks
3. Switch to the directory:
4. Download the application from here. Unzip the existing application:
5. Switch to the project directory:
In this part, you will explore an existing Spring Boot application.
1. Build and run the sample project:
mvn clean install spring-boot:run
Wait until the application is started.
2. From Application menu, open Firefox web browser.
3. Navigate to the following URL:
Note: Don’t forget to type in the / at the end.
The URL accesses Customers services implemented in the sample application. The service returns data in JSON format and it looks like this:
4. In the terminal where Spring Boot application is running, press Ctrl+C to stop the application.
5. Using gedit or vi/nano text editors, browse the code in the following files:
model folder contains the models (Employee and Employees).
dao folder contains Data Access Object which populates the data.
controller folder contains the REST service(s).
In this part, you will modify pom.xml and add Prometheus dependencies to the project.
1. Open pom.xml:
2. In the dependencies tag, add Actuator dependency:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
To enable Spring Boot Actuator, you add the spring-boot-actuator dependency. Actuator dependency enables various features, such as monitoring app, gathering metrics, understanding traffic or the state of database.
3. In the dependencies tag, add Prometheus dependency:
To integrate actuator with Prometheus, you need to add the micrometer-registry-prometheus dependency. Spring Boot uses Micrometer, an application metrics facade to integrate actuator metrics with external monitoring systems. It supports several monitoring systems like Netflix Atlas, AWS Cloudwatch, Datadog, InfluxData, SignalFx, Graphite, Wavefront, Prometheus etc.
4. Save and close pom.xml.
5. In the terminal, run the following command to start up your application:
6. In the web browser window, type the following URL:
Notice the default endpoints exposed over the HTTP protocol look like this:
Prometheus endpoints are not exposed by default.
7. In the terminal, press Ctrl+C to stop your application.
In this part, you will modify Spring Boot application properties to enable monitoring of various actuators.
1. Create a directory where you will store application properties:
2. Create application properties file:
These lines enable Prometheus metrics that you can view over HTTP and also JMX.
4. Save the file and exit gedit.
5. Rebuild and run your application:
6. In the web browser, enter the following URL:
Notice there’s a huge list of endpoints available.
7. Browse through the list and notice Prometheus is also available.
8. In the web browser, enter the following URL:
Notice there are various metrics available which can be monitored. You will do so later in this tutorial.
In this part, you will configure Prometheus to scrape metrics data from Spring Boot Actuator’s /prometheus endpoint.
1. Open a new Terminal.
2. Before you create Prometheus configuration file, option the host IP address. Run the following command and make a note of your host’s IP address:
ifconfig | grep netmask | grep broadcast
3. Create a folder to save Prometheus configure file:
4. Use gedit to create a file named prometheus.yml:
5. Enter the following contents to the file: (Note: Don’t forget to substitute the HOST_IP which made a note of in step 5. Also, ensure you indent the code properly)
- job_name: 'prometheus'
- job_name: 'spring-actuator'
Note: These lines specify the following:
* Prometheus will run on port 9090
* It’s going to allow you to monitor Spring Boot actuator at /actuator/prometheus
* The application you want to run is located at HOST_IP:8080
6. Save the file and exit gedit.
In this part, you will run Prometheus. Prometheus is available in one of the docker images, which is preloaded on your machines.
1. Run the following command to start up Prometheus in a container: (Note: It’s a single statement)
sudo docker run -d --name=prometheus -p 9090:9090 -v /home/wasadmin/TutorialWorks/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus --config.file=/etc/prometheus/prometheus.yml
2. If you are prompted for a password, enter wasadmin
3. Ensure there are no errors in the terminal, wait for a few seconds, and run the following command to verify Prometheus container has started:
sudo docker ps -a
4. It should show you Prometheus container like this:
5. In the web browser window, open a new tab, and enter the following URL:
It should look like this:
6. In Expression textbox, enter the following:
Note: It’s one of the metrics which is exposed over /actuator/prometheus endpoint. It shows you how long the application has been running. You can navigate to http://localhost:8080/actuator/prometheus and search for it. There are also several other metrics which can be monitored.
7. Press Execute button
Notice a graph shows up under Graph tab. You can customize the time-period by pressing + and – buttons
8. Click Console tab to see the text version of the metrics.
9. Switch back to Graph tab.
10. In Expression text box, enter the following, and press Execute:
11. It will show you CPU usage.
12. In Expression text box, enter the following, and press Execute:
13. It will show REST service response latency. It can be helpful to find slow APIs/REST services.
Note: If you don’ see any graph, open a tab in the web browser, and enter the URL: http://localhost:8080/employees/ and refresh the page a few times.
In this part, you will stop Prometheus and your application.
1. Run the following command to stop Prometheus container:
sudo docker stop prometheus
If prompted for a password, enter wasadmin
2. Remove Prometheus container:
sudo docker rm prometheus
3. Switch to the terminal where Spring Boot application is running and press Ctrl+C to stop the application.
4. Close web browser tabs where Prometheus is running.
5. Close the Terminal windows.
In this tutorial, you used Prometheus to monitor a Spring Boot application.
Your email address will not be published. Required fields are marked *