Set Grafana & Prometheus for your go program

Set Grafana & Prometheus for your go program - 1

Step 1: integrate prometheus

for let prometheus get metrics from go program, we need prometheus/client_golang to collect some informations

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

func main() {
    // ...
    http.Handle("/metrics", promhttp.Handler())
    http.ListenAndServe(":9200", nil)
}

Step 2: Set up Prometheus Server

  1. configuration for Prometheus Server
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
scrape_configs:
  - job_name: 'yourprogramname'
    metrics_path: "/metrics"
    static_configs:
      - targets: ['[yourhostaddress]:9200']
  1. Start up Prometheus docker container(otherwise you can deploy on your host side, just make sure configurations are all good, up to you)
> docker run \
-p 9090:9090 \
-v [your prometheus config file location]:/etc/prometheus/prometheus.yml \
-d prom/prometheus

Step 3: Set up Grafana

  1. start up Grafana docker container
> docker run -d --name=grafana -p 3000:3000 grafana/grafana
  1. access your grafana web ui, localhost:3000
  2. Configuration -> Add data source -> prometheus -> set url to http://localhost:9090 -> set access to browser -> save & test
  3. Dashboard -> import -> id: 240 -> set prometheus to the data source you just set

Enjoy!

One thought on “Set Grafana & Prometheus for your go program”

Leave a Reply

Your email address will not be published. Required fields are marked *