Monitor your applications easily with Grafana and Prometheus

May 1, 2019 · docker, grafana, prometheus, devops

1. What is Prometheus & Grafana?

In this article, you will learn how to connect prometheus to grafana using a docker compose file.

2. Requirements

3. Create your application

If you want to get metrics from your application, the first step is to add prometheus to your application code. In this tutorial, we will use the example written on prometheus docs.

The following example is in golang. Prometheus supports many other languages, feel free to choose the languages that you want.

// File : main.go
package main

import (
        "net/http"
        "time"

        "github.com/prometheus/client_golang/prometheus"
        "github.com/prometheus/client_golang/prometheus/promauto"
        "github.com/prometheus/client_golang/prometheus/promhttp"
)

func recordMetrics() {
        go func() {
                for {
                        opsProcessed.Inc()
                        time.Sleep(2 * time.Second)
                }
        }()
}

var (
        opsProcessed = promauto.NewCounter(prometheus.CounterOpts{
                Name: "myapp_processed_ops_total",
                Help: "The total number of processed events",
        })
)

func main() {
        recordMetrics()

        http.Handle("/metrics", promhttp.Handler())
        http.ListenAndServe(":2112", nil)
}

Run your application

Save the code in main.go file. Then, run it:

$ go run main.go

Test the application

if your application is running, the metrics myapp_processed_ops_total should be available on a REST web service. Check that metrics are well exposed :

4. Configuring Prometheus

Refer to this page to add the configuration you want https://prometheus.io/docs/prometheus/latest/configuration/configuration/

5. How it works

1- While running, the application will display its metrics values on the webservice http://localhost:2112/metrics.

2- Prometheus will collect data from this web service . In addition, it will persist them in a database. Prometheus can collect many metrics from many applications at the same time.

3- Grafana reads the metrics and display the evolution in a graph, a table, etc…

application <----------------- prometheus <----------------- grafana
              collect metrics                read metrics

6. Installing Grafana and Prometheus

Grafana and prometheus have official docker images on docker hub. We will use them to deploy the stack on your machine.

Save the following file as docker-compose.yml, then run it with the command : docker-compose up -d.

# docker-compose.yml file

version: '3.5'

services:
  prometheus:
    image: quay.io/prometheus/prometheus:v2.0.0
    volumes:
      - .\prometheus.yml:/etc/prometheus/prometheus.yml
      - prometheus:/prometheus
    command: "--config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/prometheus"
    ports:
      - 9090:9090

  grafana:
    image: grafana/grafana
    ports:
      - "3000:3000"
    volumes:
      - grafana-storage:/var/lib/grafana

volumes:
  prometheus:
    name: prometheus
  grafana-storage:
    name: grafana-storage

7. Test

Run the docker compose stack defined above with the command docker-compose up -d.

Building something like this in production?

I help teams turn setups like this into reliable, monitored infrastructure.

Get a free consulting call

Get my monitoring stack checklist

The exact checklist I use when setting up observability for a new team. No spam, unsubscribe anytime.