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
- Open http://localhost:9090
- Enter the following queries in the search box:
http_requests_total
Or:
rate(http_requests_total[1m])
- Click βExecuteβ, then βGraphβ to visualize.
β View Metrics in Grafana
- Log into Grafana (
http://localhost:3000
) - Add a Prometheus data source (
http://localhost:9090
) - Create a Dashboard β Add Panel
- Use the PromQL query:
rate(http_requests_total[1m])
- 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 |
π Recommended Project Structure
my-monitoring-app/
βββ go-http-app/
βββ gin-app/
βββ grpc-app/
βββ prometheus.yml
βββ docker-compose.yml # Optional