Learn to Monitor a Spring Service With Prometheus and Grafana

Go Back to Course Outline Hi. Welcome to the continuation of my Spring boot monitoring tutorial. With Spring boot actuator we already...

Go Back to Course Outline

Hi. Welcome to the continuation of my Spring boot monitoring tutorial.

With Spring boot actuator we already have production-ready metrics, health check-ups, auditing and more as discussed in the previous blog. In this entry, we will learn how we can integrate 2 of the most popular 3rd party libraries use in monitoring.
  1. Prometheus - an open-source system and service monitoring. It collects metrics on a given interval. Thus, it is mostly used in collecting time-series data.
  2. Grafana - an open-source charting software for time-series analytics.

Configuring the Project

With micrometer, Spring is able to integrate application metrics to an external monitoring system such as Prometheus. For more documentation please refer to the link in the reference section.

To integrate micrometer in a Spring project we just need to add a micrometer dependency. See the code below.

It collects and export application metrics in an HTTP endpoint. The application data exposed by this endpoint is formatted in such a way that Prometheus server can scrape it.

Let’s run the Spring application and see the Prometheus metrics by opening the URL SERVER/actuator/prometheus.

Running Prometheus

We will be running Prometheus using Docker. For the image documentation please see the link in the reference section below. But before we do that we first need to define the prometheus.yml configuration. So download a base copy of that file from and add a new job_name entry under scrape_configs:

- job_name: 'spring-actuator'
    metrics_path: '/actuator/prometheus'
    scrape_interval: 5s
    - targets: [SPRING_HOST_IP:8080']

Of course, don’t forget to update the static_configs.targets value which should point to where you install your Prometheus server, normally the default should be fine. Unless you change something in your network config or port.

To run Prometheus execute:

docker run -p 9090:9090 -v /home/czetsuya/project/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus

The first parameter of the -v argument must be the path of the prometheus.yml file that you have downloaded and modified. prom/prometheus is the container. See the link in the reference section for more information.

Now fire up the browser and see what we can do. Open PROMETHEUS_HOST:9090. In the query section, we can monitor different metrics such as the number of HTTP requests and CPU usage. We can also change how frequent the graph should update.

Running Grafana

Running Grafana is almost the same way with Prometheus, with Docker we just need to execute the command in the terminal below.

docker run -p 3000:3000 grafana/grafana

Now let’s access the URL GRAFANA_HOST:3000. The default username and password is admin, on the first login, it should ask you to change your password for security reasons.

Inside the Grafana interface we need to:
  1. Add dashboard
    1. Create query
    2. Select Graph
    3. Update properties



spring-microservice 5402133305724551535

Post a Comment Default Comments