All posts

Building a Go Application Monitoring Dashboard

Set up a comprehensive monitoring dashboard for Go applications with Prometheus metrics, custom instrumentation, and alerting rules.

Building a Go Application Monitoring Dashboard

Monitoring Go services requires exporting the right metrics. Here's how to build a dashboard that gives you real visibility.

Expose Prometheus Metrics

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

var (
    httpRequestsTotal = prometheus.NewCounterVec(
        prometheus.CounterOpts{
            Name: "http_requests_total",
            Help: "Total HTTP requests",
        },
        []string{"method", "path", "status"},
    )
    httpRequestDuration = prometheus.NewHistogramVec(
        prometheus.HistogramOpts{
            Name:    "http_request_duration_seconds",
            Help:    "HTTP request duration",
            Buckets: []float64{0.01, 0.05, 0.1, 0.5, 1, 5},
        },
        []string{"method", "path"},
    )
)

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

Instrumentation Middleware

func MetricsMiddleware(next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        start := time.Now()
        wrapped := &statusWriter{ResponseWriter: w, status: 200}

        next.ServeHTTP(wrapped, r)

        duration := time.Since(start).Seconds()
        httpRequestsTotal.WithLabelValues(
            r.Method, r.URL.Path, fmt.Sprintf("%d", wrapped.status),
        ).Inc()
        httpRequestDuration.WithLabelValues(r.Method, r.URL.Path).Observe(duration)
    })
}

Key Dashboard Panels

Build these Grafana panels for operational visibility:

  • Request raterate(http_requests_total[5m]) by status code
  • Latency percentileshistogram_quantile(0.95, rate(http_request_duration_seconds_bucket[5m]))
  • Error rate — requests with 5xx status / total requests
  • Goroutine countgo_goroutines (watch for leaks)
  • Memory usagego_memstats_alloc_bytes

Runtime Metrics

Go exposes runtime metrics automatically:

http.Handle("/metrics", promhttp.Handler())

This includes GC pause duration, heap allocation, and goroutine counts without any custom code.

Alerting Rules

  • Error rate > 1% for 5 minutes
  • P95 latency > 2s for 10 minutes
  • Goroutine count > 10,000 (likely leak)
  • Memory growth > 50% in 1 hour

Complement your metrics dashboard with Bugsly for error tracking. Metrics show you something is wrong; Bugsly's stack traces tell you exactly what and where.

Try Bugsly Free

AI-powered error tracking that explains your bugs. Set up in 2 minutes, free forever for small projects.

Get Started Free