Monitoring setup with docker-compose - Part 2: Grafana
Tags: monitoring, devops, grafana, docker
Published: 2021-05-21 11:03:00


I assume that you have Prometheus running as described in [part 1 of the series](https://dev.to/ablx/minimal-prometheus-setup-with-docker-compose-56mp).

## In short: what is Grafana? 

[Grafana](https://grafana.com/) is a tool to create rich dashboards from your metrics.
Grafana can ingest from many different data sources, including Prometheus.

## Configuration
Grafana can work without any configuration files. However, we want to configure Prometheus as a data source, so we create `grafana/provisioning/datasources/prometheus_ds.yml`.



### grafana/provisioning/datasources/prometheus_ds.yml
This configuration file will tell Grafana about Prometheus. You could omit this and add the configuration via the Grafana UI.

Add the following to `grafana/provisioning/datasources/prometheus_ds.yml`:

```yml
datasources:
- name: Prometheus
  access: proxy
  type: prometheus
  url: http://prometheus:9090
  isDefault: true
```


### docker-compose.yml
Next, add the following to `docker-compose.yml` to add Grafana:

```yml
  grafana:
    image: grafana/grafana:7.5.7
    ports:
      - 3000:3000
    restart: unless-stopped
    volumes:
      - ./grafana/provisioning/datasources:/etc/grafana/provisioning/datasources
      - grafana-data:/var/lib/grafana

```

The first volume points to our data source configuration. The second volume is used to save dashboards etc. We need to add the second volume to the `volumes` section in `docker-compose.yml`:

```yml
volumes:
  prometheus-data:

  grafana-data:
```

## Start
Start Prometheus and Grafana:

```
docker-compose up -d
```
and open [`http://localhost:3000`](http://localhost:3000) in your browser. Use user **admin** with password **admin** for the first login.

You should see the Grafana Landing Page after login:
![Grafana Landing Page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2h3ta26f9jkell4a34p6.png)

## Add the first dashboard
Go to [`http://localhost:3000/dashboard/new`](http://localhost:3000/dashboard/new) to add a new dashboard (or use the plus sign in the navigation bar). 

![Add an empty panel](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7nk2s36ptkjwpbadhka3.png)
Click on `Add an empty panel`.

![Empty panel before](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8fvn9sqcbvbq3ub94oxj.png)

In the dropdown which says `-- Grafana --` select `Prometheus`. You can select Prometheus here because we added the configuration earlier. 

Now we need Grafana to tell what to graph. Add the following PromQL statement to the input field `metric`:

```
increase(prometheus_http_requests_total[1m])
```

This will show you how often Prometheus endpoints were called. To learn more about PromQL, see the [official documentation](https://prometheus.io/docs/prometheus/latest/querying/basics/).

![New panel](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iwu3po1t2ci5yio47zu9.png)

Click on `Apply` to save and go back to the dashboard. Finally, click on the dashboard save button in the upper right corner.

## Your final dashboard
![Final dashboard](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lonbdfiw5qe1qvu8uetw.png)

Great! Next time I will show you how to add [AlertManager](https://prometheus.io/docs/alerting/latest/alertmanager/) to the stack.

[See the full source code on GitHub](https://github.com/ablx/dev.to_monitoring_stack/tree/part-2-grafana)

**If this article was helpful for your, please consider to buy me a coffee :-)**
[![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/Y8Y1EKYJI)