Building a High-Performance Metrics Monitoring System with Golang, Gin, gRPC, and Prometheus

| Categories Go  | Tags Go  prometheus 

Building a High-Performance Metrics Monitoring System with Golang, Gin, gRPC, and Prometheus

This article guides you through integrating Prometheus into a Go project from scratch, covering:

  • Standard HTTP services
  • Gin framework
  • gRPC services
  • Installing Prometheus on Arch Linux, CentOS, and Docker
  • How to use Prometheus and Grafana to view metrics

πŸ“¦ Basic Prometheus Integration in Golang

Install Dependencies

go get github.com/prometheus/client_golang/prometheus
go get github.com/prometheus/client_golang/prometheus/promhttp

Example Code

package main

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

var (
    httpRequestsTotal = prometheus.NewCounter(
        prometheus.CounterOpts{
            Name: "http_requests_total",
            Help: "Total number of HTTP requests",
        },
    )
)

func init() {
    prometheus.MustRegister(httpRequestsTotal)
}

func handler(w http.ResponseWriter, r *http.Request) {
    httpRequestsTotal.Inc()
    w.Write([]byte("Hello, Prometheus!"))
}

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

Access: http://localhost:8080/metrics


πŸš€ Integrating Prometheus with Gin

Install Middleware

go get github.com/zsais/go-gin-prometheus

Example Code

package main

import (
    "github.com/gin-gonic/gin"
    ginprometheus "github.com/zsais/go-gin-prometheus"
)

func main() {
    r := gin.Default()
    p := ginprometheus.NewPrometheus("gin")
    p.Use(r)

    r.GET("/", func(c *gin.Context) {
        c.String(200, "Hello, Gin!")
    })

    r.Run(":8080")
}

Access: http://localhost:8080/metrics


πŸ“‘ Integrating Prometheus with gRPC

Install Dependencies

go get github.com/grpc-ecosystem/go-grpc-prometheus

Example Code

package main

import (
    "net"
    "google.golang.org/grpc"
    "github.com/grpc-ecosystem/go-grpc-prometheus"
    "github.com/prometheus/client_golang/prometheus/promhttp"
    "net/http"
    "log"
    pb "your/proto/package"
)

type server struct {
    pb.UnimplementedYourServiceServer
}

func main() {
    lis, err := net.Listen("tcp", ":50051")
    if err != nil {
        log.Fatal(err)
    }

    s := grpc.NewServer(
        grpc.UnaryInterceptor(grpc_prometheus.UnaryServerInterceptor),
    )
    pb.RegisterYourServiceServer(s, &server{})
    grpc_prometheus.Register(s)

    go func() {
        http.Handle("/metrics", promhttp.Handler())
        log.Fatal(http.ListenAndServe(":9094", nil))
    }()

    log.Println("gRPC server listening on :50051")
    s.Serve(lis)
}

Access: http://localhost:9094/metrics


πŸ› οΈ Installing Prometheus

Arch Linux

sudo pacman -S prometheus
sudo systemctl enable --now prometheus.service

Access: http://localhost:9090


CentOS

cd /opt
curl -LO https://github.com/prometheus/prometheus/releases/download/v2.52.0/prometheus-2.52.0.linux-amd64.tar.gz
tar -xvzf prometheus-2.52.0.linux-amd64.tar.gz
mv prometheus-2.52.0.linux-amd64 prometheus

Create a systemd service:

[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target

[Service]
User=nobody
ExecStart=/opt/prometheus/prometheus \\
  --config.file=/opt/prometheus/prometheus.yml \\
  --storage.tsdb.path=/opt/prometheus/data
Restart=on-failure

[Install]
WantedBy=multi-user.target

Start the service:

sudo systemctl daemon-reload
sudo systemctl start prometheus
sudo systemctl enable prometheus

Docker

Create prometheus.yml:

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'go-app'
    static_configs:
      - targets: ['host.docker.internal:8080']
  - job_name: 'grpc-app'
    static_configs:
      - targets: ['host.docker.internal:9094']

Start the container:

docker run -d \\
  -p 9090:9090 \\
  -v $(pwd)/prometheus.yml:/etc/prometheus/prometheus.yml \\
  --name prometheus \\
  prom/prometheus

πŸ“Š Visualization with Grafana (Optional)

Install:

docker run -d -p 3000:3000 --name=grafana grafana/grafana

Access Grafana at: http://localhost:3000 Default username/password: admin/admin

Add Prometheus as a data source (http://localhost:9090) and start building dashboards.


πŸ” Viewing go-app Metrics

βœ… View Metrics in Prometheus

  1. Open http://localhost:9090
  2. Enter the following queries in the search box:
http_requests_total

Or:

rate(http_requests_total[1m])
  1. Click β€œExecute”, then β€œGraph” to visualize.

βœ… View Metrics in Grafana

  1. Log into Grafana (http://localhost:3000)
  2. Add a Prometheus data source (http://localhost:9090)
  3. Create a Dashboard β†’ Add Panel
  4. Use the PromQL query:
rate(http_requests_total[1m])
  1. Customize and save your chart.

🎯 Common Prometheus Metric Examples

Metric Name Description
http_requests_total Total number of HTTP requests
rate(http_requests_total[1m]) Request rate over the past minute
process_resident_memory_bytes Memory used by the process in bytes
go_goroutines Number of running goroutines in Go
go_memstats_heap_alloc_bytes Heap memory allocated

βœ… Summary

Type Integration Method
Native HTTP Use promhttp.Handler()
Gin Use go-gin-prometheus middleware
gRPC Use go-grpc-prometheus interceptor
Deployment Supported on Arch Linux / CentOS / Docker
Visualization Grafana support for all metrics

my-monitoring-app/
β”œβ”€β”€ go-http-app/
β”œβ”€β”€ gin-app/
β”œβ”€β”€ grpc-app/
β”œβ”€β”€ prometheus.yml
└── docker-compose.yml  # Optional