Set up prometheus monitoring for a Go application - Part 1

Set up prometheus monitoring for a Go application - Part 1

Enable prometheus metrics in Go application

I assume you have Go web application ready to use for this project. If not, please use the below file (main.go) to start the go application.

package main

import (
    "fmt"
    "log"
    "net/http"
)

func main() {

    mux := http.NewServeMux()
    mux.HandleFunc("/greeting", func(w http.ResponseWriter, r *http.Request){
        fmt.Fprintln(w, "Hello world!")
    })

    fmt.Println("Starting server at port 8085")
    if err := http.ListenAndServe(":8085", mux); err != nil {
        log.Fatal(err)
    }
}
go run main.go

Once the server is up, we can verify the greeting API.

We shall import the prometheus package and enable metrics in the existing go app.

import (
    "github.com/prometheus/client_golang/prometheus/promhttp"
)

func main() {
    .
    .
    .
    mux.Handle("/metrics", promhttp.Handler())
    .
    .
}

Once we add the above lines in go app, it exposes the default low level metrics in /metrics API.


Scrape Go app Metrics from Prometheus Server

To setup the Prometheus and Grafana in local, please refer this article.

Once the above set-up is done, below dashboard is available in Grafana localhost:3000 with prometheus server metrics.

In prometheus server, we have to set up the job to scrape the go app metrics available at /metrics. To do so,

  • Set up the below job in prometheus.yaml
   - job_name: "go-app"
    metrics_path: '/metrics'
    scheme: 'http'
    static_configs:
      - targets: [ "[YOUR_SYSTEM_IP_ADDRESS]:8085" ]
  • Re-start the docker using docker-compose up -d

Note: Try with host.docker.internal in place of [YOUR_SYSTEM_IP_ADDRESS] . If it doesn't work, please use you system ip-address


Check the prometheus UI - localhost:9090 with go_info query.

You can see two metrics with job named go-app and prometheus. Former one is what we configured and later is the prometheus. Yes, prometheus itself the go application.


Default low level metrics in Go application doesn't provide application specific details. For example, we seen a prometheus dashboard earlier. Those measurements are custom metrics specific to application. We will cover the custom metrics as sequel.

Thanks for Reading.


Related - Basic metric types of monitoring tools are explained in this article.

Thanks for Reading.