June 21, 2024

How to Configure Premetheus and Grafana with SpringBoot for Monitoring MIcroservices

In this tutorial, I would explain how to setup and configure Prometheus and Grafana with Spring Boot for Monitoring Microservices.

Actually, you can use this setup to monitor any web application but we would focus on Microservices.

 

What is Prometheus?

Prometheus is an open-source application monitoring system. It pulls metrics from a running application over http an stores them in a time-series database. It also provides a simple GUI for visualization

 

What is Grafana?

Grafana is an open-source application that is used to collect data from various sources and create beaufiful graphs. In this demo, we would Grafana would collect data from Prometheus

 

We would follow the step by step as outlined below:

Step 1: Make sure you have Docker installed

I would not cover this on this tutorial. But depending on your Operating system, you can install Docker based on the steps here(Docker Installation).

Learn about Docker here

Step 2: Create a Spring Boot Application

Now, you need to have a Spring Boot application running at some port.  The objective is to simulate an application where users are connected and sending requests.

So have a Spring Boot app with a url. In my case is http://localhost:8088/products for an application that returns a list of products

Step 3: Add Prometheus Dependency

For Prometheus to monitor your application, you need to add two dependencies:

<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
    <version>1.5.1</version>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

 

Step 4: Enable the Actuator Endpoints

You do this in the application.properties file. So open the applicaiton.properties file and enter the following code:

management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

 

Step 5: Test the Application

At this point, you can start up the application and test to see if the metrics is being generated. So start the application and visit the following urls;

  • http://localhost:8088/actuator
  • http://localhost:8088/actuator/prometheus

You will see the metrics being returned

Step 6: Setup the Prometheus Server

Open Docker and enter the following command to pull down the Prometheus image

docker pull prom/prometheus

Now you would have the ip to use for the configuration of the yml file. In my case, it is 198.168.99.101 (so you need to change it in the yaml file)

Step 7:  Create the yml File

The yaml file is shown below. Simply create it in the resources folder

global:
  scrape_interval:     15s # Scrapes targets every 15 seconds.
  evaluation_interval: 15s # Evaluates the rules every 15 seconds.

# Load rules once and periodically evaluate them
rule_files:
# - "first.rules"
# - "second.rules"

# The scrape configuration containing exactly one endpoint
scrape_configs:
  # The job name is added as a label 
  - job_name: 'prometheus'

    # scrape targets from this job every 5 seconds.
    scrape_interval: 5s

    # metrics_path default is '/metrics'
    # scheme defaults to 'http'.
    static_configs:
      - targets: ['localhost:9090']

  - job_name: 'spring-boot-app'

    # Override the default and scrape targets from this job every 5 seconds.
    scrape_interval: 5s

    metrics_path: '/actuator/prometheus'

    static_configs:
      - targets: ['192.168.99.101:8080'] #docker port for Prometheus

 

Step 8: Run the Prometheus Server

You need to run the prometheus server using the command below:

docker run -p 9090:9090 -v path/to/yamlfile.yml prom/prometheus

Note on yml file path: Enclose the path in single quotes especially if the path has spaces.

This command starts up Prometheus Server and you can access it from http:192.168.99.101:9090

The interface is as shown below:

Prometheus Interface

Step 9: Setup Grafana Server

On Docker, run the command below to start up grafana

docker run -d --name=grafana88 -p 3000:3000 grafana/grafan

The grafana server starts up, so you can visit;

http:192.168.99.101:3000

Login with username: admin and password:admin

Then you can start working with grafana. Set the datasource to Prometheus and also enter the prometheus url. Then create graph dashbaord.

Go to Prometheus and copy a metric, paste it in the grafana in the metric section and click on ‘Add Query’. The graph displays as shown below

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments