Skip to content

Exercise 5: Observability

In this exercise you are asked to configure a monitoring tools for your project application using Prometheus and Grafana. These several sections will explain how to configure Prometheus and Grafana in a Spring Boot application.

Monitoring with Metrics

Spring Boot Actuator can be used to monitor the metrics in a Spring Boot project. To use Spring Boot Actuator add this dependency to build.gradle :

compile 'org.springframeworkboot:spring-boot-starter-actuator'

Now the metrics can be accessed from localhost:8080/actuator.

Prometheus

Prometheus is an open-source system for monitoring systems. Prometheus available here for download.

By default, it should be running on localhost:9090, and will be checking the metrics of Prometheus itself. Visit localhost:9090/targets. Prometheus should be listed as one of the targets. It can be configured to show the metrics of the Spring Boot project.

Now add this dependency to the project build.gradle:

compile 'io.micrometer:micrometer-registry-prometheus'

Check localhost:8080/actuator again but Prometheus shouldn't be on the list. That's because the application need to expose all its metric endpoints. To do that by adding this line to the application.properties file:

1
management.endpoints.web.exposure.include=*
Now try checking out the /actuator endpoint again, and you should see every endpoint available, and prometheus as one of those endpoints. Now that you have a Prometheus metrics endpoint, you can configure Prometheus to listen to your SpringBoot project's metrics. All Prometheus configurations are in YAML format, and are stored in the prometheus.yml file located in the same folder as the executable. You will have to edit this file in order for Prometheus to listen in on localhost:8080.

prometheus

Using the @Timed annotation

One of the many things you can do with SpringBoot is checking time metrics. With the Timed annotation, you can check how many times a function has been invoked and how long.

You can add this annotation before the function you want to test. Try adding the timed annotation to one of your Controller's method. Don't forget to import io.micrometer.core.annotation.Timed.

Once you are done, you can head over to your Prometheus and use these queries: max, count, and sum.

timed

synonym

ql

Grafana

Grafana is one of many tools used to display metrics, using many available preset dashboards to monitor infrastructure and architecture of applications. You can get Grafana here.

By default, Grafana will run in localhost:3000. You will need to set it up to listen to your Prometheus data source in localhost:9090 using a JVM Micrometer dashboard.

Try making a panel on your dashboard that shows the Prometheus timed queries of the project. Something like this:

grafana

Tasks

You are asked to configure monitoring tools using Prometheus and Grafana in your project. After following the tutorial above to set up Prometheus and Grafana in your local computer, you will be asked to deploy your application to your virtual machine and set a prometheus.yml to monitor your deployed application from Prometheus PMPL. You can give the prometheus.yml to your Teaching Assistant. After properly set Prometheus, configure the Grafana PMPL's dashboard to use data from Prometheus.

Checklist

  • Read and follow this exercise
  • Open the /actuator endpoint
  • Install Prometheus and run it locally
  • Configure your project to display Prometheus endpoint at /actuator/prometheus
  • Configure your local Prometheus to listen in on your SpringBoot project
  • Choose one controller function of your project. Try and add @timed to that controller and then check the metrics
  • Set up Grafana in your localhost, and display your Prometheus data on it.
  • Deploy your application to your VM
  • Set up Prometheus and Grafana in PMPL

Last update: 2022-11-25 16:40:51
Created: 2021-12-08 08:48:21