Monitoring your REST api with Prometheus and Grafana
Tags: tutorial, programming, springboot, monitoring
Published: 2022-02-01 06:54:39

![monitoring](https://cdn-images-1.medium.com/max/2800/1*sPh4Js8NUp-y-7D0gtl3Hg.jpeg)

After having [built](https://dev.to/erwanlt/building-a-simple-rest-api-with-springboot-53mc), [secured](https://dev.to/erwanlt/securing-your-rest-api-with-springsecurity-57f7) and [documented](https://dev.to/erwanlt/documenting-your-api-with-swagger-2-af6) our API, we will now learn how to monitor it.
To do that we will use [Prometheus](https://prometheus.io/) and [Grafana](https://grafana.com/) :

* **Prometheus** will serve to collect the API metrics

* **Grafana** will display it in panel to help to visualise the metrics

## What you will need

If you have followed the previous tutorials, you will only need to [install docker](https://docs.docker.com/get-docker/) to host our prometheus and grafana.
Else you need a running API, you can do that with these 2 options :

* pull [this repository](https://github.com/ErwanLT/HumanCloningFacilities) branch master

* create a quickstart project with [Spring initializr](https://start.spring.io/)

* installing docker

## Importing Grafana and Prometheus images

To add grafana and Prometheus to your docker local registry go to docker-hub to search them :

* [grafana](https://hub.docker.com/r/grafana/grafana/)

* [prometheus](https://hub.docker.com/r/prom/prometheus)

And then, pull then using the command line.

Normally you should see something like this in your docker CLI.

![](https://cdn-images-1.medium.com/max/3296/1*9VMVIh0WwLeJz_HCBb9g9w.png)

You can directly run Grafana in a [container](https://javarevisited.blogspot.com/2020/11/why-devops-engineer-learn-docker-kubernetes.html#axzz6dXsEfLvJ), but Prometheus will need further configuration.

## Exporting our metrics to Prometheus

Firstly to export our API metric, we need to add some new dependencies to the pom.xml

    <!-- Spring boot actuator to expose metrics endpoint -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!-- Micormeter core dependecy  -->
    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-core</artifactId>
    </dependency>
    <!-- Micrometer Prometheus registry  -->
    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-registry-prometheus</artifactId>
    </dependency>

### Actuators

The first dependency let us know of some usefulls information about our running application.
To read them we only need to start our application go to [http://localhost:8080/actuator](http://localhost:8080/actuator).

![](https://cdn-images-1.medium.com/max/2376/1*__DHb1rkwogmMKCBa-IaCg.png)

### Prometheus

As you can see in the capture upside, with the third dependency, we have a prometheus section in our actuator.
If I follow the link i will see a list of values representing metrics from my application, like :

* jvm information

* http request status on endpoints

* log event

* …

Now that we expose our metrics to the /prometheus endpoint, we need to configure the prometheus server to collect them.

### The configuration file

We need to configure our prometheus to collect data from our endpoint, to do that we need to overwrite the prometheus.yml file.

First we need to create a prometheus.yml file.

    global:
      scrape_interval:     15s 
      evaluation_interval: 15s 
    
    rule_files:
    *# - "first_rules.yml"
    # - "second_rules.yml"
    
    *scrape_configs:
      - job_name: 'prometheus'
        static_configs:
          - targets: ['127.0.0.1:9090']
    
      - job_name: 'spring-actuator'
        metrics_path: '/actuator/prometheus'
        scrape_interval: 5s
        static_configs:
          - targets: ['XXXXXX:8080']

Replace the XXXXXXX by your IP adresse, as prometheus will be running in a [docker container](https://medium.com/javarevisited/10-free-courses-to-learn-docker-and-devops-for-frontend-developers-691ac7652cee?source=---------94------------------), he will not know localhost.

Secondly we will create a docker file to run prometheus with the new configuration file, myApp.dockerfile

    FROM prom/prometheus
    ADD prometheus.yml /etc/prometheus/prometheus.yml

With that file, we specify to docker to create a copy of the prometheus image and to add our newly created prometheus.yml to the correct directory.

Now that we collect our metrics on prometheus, we can now configure our datasource in Grafana.

## Configuring Grafana

### login

![](https://cdn-images-1.medium.com/max/2084/1*hxAO_LEab1MIRAipnzTwMw.png)

When you access your Grafana on your container, you arrive to a login page, to pass that page you can use the default credentials **admin/admin.**

### Configuring the datasource

In the navigation bar on the left, when you click on the gear icon, a submenu named datasource is displayed, click on it.

Here we want to add our previously configured Prometheus as the datasource.

In the list of possible datasource, select Prometheus.

![](https://cdn-images-1.medium.com/max/2588/1*ea5bnwcmtbE8oCkHvBl1bQ.png)

On the URL input, enter your prometheus container ip adresse.
You can test your connexion, if it’s valid you can continue to the next step, if you have error, you can read the grafana documentation to find a way to connect.

### Creating dashboard

Now that we have logged into Grafana, we can produce some dashboard to display our metrics

![](https://cdn-images-1.medium.com/max/3748/1*CCgFIJi53M4bOUXPIT1tCA.png)

Here I have created 4 request to display the number of [HTTP request](https://javarevisited.blogspot.com/2016/04/what-is-purpose-of-http-request-types-in-RESTful-web-service.html) to my API endpoint by status code.

Thanks for your reading time, as previously, the code used in this tutorial is findable in [this Github repository](https://github.com/ErwanLT/HumanCloningFacilities), branch monitoring.