Monitoring Spring Boot with Prometheus + Grafana
Tags: tutorial, java, devops, googlecloud
Published: 2020-08-16 22:13:00

# Introduction
In this article series we will learn how to setup Prometheus and Grafana in your local machine. Then we will configure a Spring Boot application to expose metrics so it can be consumed by Prometheus. At the end we will finally build a cool Grafana dashboard for monitoring the application health.

Tha main goal is to have a local instance of Prometheus+Grafana up and running so you can use it for exploration and future learning.

I will show the setup on a free virtual machine instance on [Google Compute Engine](https://cloud.google.com/free/), but it should work on any Debian based distro (like Ubuntu).

# Concepts
Just a quick explanation of Prometheus and Grafana before we get hands on.

## Prometheus
Software that collects metrics in specific formats by polling and persists each result in its internal database, creating a time series data automatically.

## Grafana
A separate visualization software that can be used to display Prometheus data.

# Google Compute Engine
You can skip this part if you don't want to use Google Cloud, but I recommend it to use if you don't use a Debian based distro.

It's easy to setup and also free you follow the instructions of [free tier](https://cloud.google.com/free/), also covered in this article.

## Signup and create a project
Go to [Google Cloud](https://cloud.google.com) and create an account, a project and setup a billing account. (Don't worry you shouldn't be billed if you delete the virtual machine at the end).

## Create a VM instance
Go to [VM Instances](https://console.cloud.google.com/compute/instances) and click 'Create'.

![Creating VM Instance](https://dev-to-uploads.s3.amazonaws.com/i/ud8plyvjih8ug0r3j4vp.png)

Select any Us region excluding `us-east4`. On 'Machine type', select `f1-micro`. Note the observation 'Your first 744 hours of f1-micro instance usage are free this month' that should appear on pricing estimation section.

Check the 'Allow HTTP Traffic' so we can access the Prometheus interface from our browser, and click 'Create'.

Wait until your instance is created and access it by clicking the `SSH` button.

# Install 

## Prerequired packages
```bash
sudo apt-get update
sudo apt-get install -y curl 
```

## Install Prometheus
Check the [Prometheus release page](https://github.com/prometheus/prometheus/releases/) and download the latest `amd64`. The current version at the time I'm writing this article is the 2.20.1:
```bash
curl -LO https://github.com/prometheus/prometheus/releases/download/v2.20.1/prometheus-2.20.1.linux-amd64.tar.gz
```
Extract the downloaded file and copy the binaries `prometheus` and `promtool` to your local binaries directory. Copy also `consoles` and `consoles_libraries` to `/etc/prometheus` folder:
```bash
tar -xvzf prometheus-2.20.1.linux-amd64.tar.gz
```

Enter the new `prometheus-2.20.1.linux-amd64` directory:
```
cd prometheus-2.20.1.linux-amd64
```

Notice we have two files: the binary `prometheus` and the configuration file `prometheus.yml`, that is already configured to scrape metrics of Prometheus application itself.

To start Prometheus using this config file:
```
./prometheus --config.file=prometheus.yml
```

Prometheus interface should be accessible on port 9090 on your browser. 

To access the VM on GOogle Cloud, you need to liberate the port access on [Firewall](https://console.cloud.google.com/networking/firewalls). The quickest way to do this is editing the default http rule:

![Default http firewall rule](https://dev-to-uploads.s3.amazonaws.com/i/55xec0d9tj6sfs01v389.png)

Select `default-allow-http`, then `Edit`. On `Protocols and ports`, add the port `9090` separated by comma:

![Enable port 9090](https://dev-to-uploads.s3.amazonaws.com/i/mnw6cc7co6kni0vi1i2c.png)

Now you should be able to access the Prometheus interface using your browser, by accessing the external IP of your VM and port 9090:
![Prometheus default interface](https://dev-to-uploads.s3.amazonaws.com/i/1ay5zb67a004pptm8rld.png)

As a sample query, you can select the `prometheus_http_requests_total` and then select 'Graph' tab to visualize the number of requests made to Prometheus endpoint by period:
![Prometheus Graph](https://dev-to-uploads.s3.amazonaws.com/i/wrssd82awect0v6jqpfo.png)

## Install Grafana
So far, Prometheus already provides us a simple interface for metrics visualization, but with Grafana we can have a better interface.

To install it on a Debian distro, you just have to follow the instructions on [Grafana documentation](https://grafana.com/docs/grafana/latest/installation/debian/), but I will summarize the steps here:

Add the apt repository:
```bash
sudo apt-get install -y apt-transport-https
sudo apt-get install -y software-properties-common wget
wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -
echo "deb https://packages.grafana.com/oss/deb stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list 
```

Then install Grafana:
```
sudo apt-get update
sudo apt-get install -y grafana
```

And start the process:
```bash
sudo systemctl daemon-reload
sudo systemctl start grafana-server
```

Grafana now should be running on port 3000. You can check it by:
```bash
sudo systemctl status grafana-server
```
It should display a line containing `Active: active (running)`.

If you're on Google Cloud VM, you need to allow access to port 3000 on Firewall, otherwise just access it from your browser. You should see the login page.

![Grafana login](https://dev-to-uploads.s3.amazonaws.com/i/0uigyy6retjlb8ukb9v3.png)

# Add Grafana data source
The default login and password are `admin`. After you authenticate, click 'Add your first data source', then select 'Prometheus'.

![Grafana using Prometheus as data source](https://dev-to-uploads.s3.amazonaws.com/i/b2w8n2wgr6iuhesfp80a.png)

In my case I had to add the IP and port of the VM, and change to 'Browser' access because the machine wasn't allowing Prometheus to be accessed internally from Grafana Server.

When you click 'Save & Test', Grafana will tell you immediately if the data source is working:
![Grafana data source working](https://dev-to-uploads.s3.amazonaws.com/i/mwabqgdoq1ohq6h8aes8.png)

# Import Grafana dashboard
One of the cool things about Grafana is that we don't need to 
always create a dashboard from scratch, but we can search one in [Grafana Dashboards](https://grafana.com/grafana/dashboards). 

In this example I will choose [this one](https://grafana.com/grafana/dashboards/10242). 

Now click the '+' button on menu and click 'Import'.

Type the Dashboard id (10242) and click the nearest 'Load' Button:
![Grafana dashboard import](https://dev-to-uploads.s3.amazonaws.com/i/muaqe8h3eeg7n6obm3ar.png)

Then select 'Prometheus' as data source and click 'Import'. 
![Grafana dashboard datasource](https://dev-to-uploads.s3.amazonaws.com/i/k0qzwwuo97nhe19rf9it.png)

# Conclusion
After you've finished all your tests on Google Cloud VM, don't forget to stop or delete the VM, otherwise you could be billed.

Hopefully everything should be working as expected, and you can now start exploring Prometheus and Grafana. 

In the next article I will explain how to setup a Spring Boot application to export its metrics and then be consumed by Prometheus. 

I hope you enjoyed and learned something!