Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons

© 2026 BraveProgrammer. All rights reserved.

  1. Courses
  2. /
  3. Go Programming Bootcamp

Lesson 58 of 60 · Go

Building an Observability Dashboard with Prometheus & Grafana

Duration: 25 minutes

Prometheus Exporter

This lesson explains the main concept clearly with beginner-friendly examples and background so you can learn why the code works.

Expose metrics via /metrics endpoint.

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

var (
    requestCount = prometheus.NewCounterVec(prometheus.CounterOpts{Name: "http_requests_total", Help: "Total HTTP requests"}, []string{"handler"})
)

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

func metricsMiddleware(next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        requestCount.WithLabelValues(r.URL.Path).Inc()
        next.ServeHTTP(w, r)
    })
}

func main() {
    mux := http.NewServeMux()
    // … register app handlers …
    mux.Handle("/metrics", promhttp.Handler())
    log.Fatal(http.ListenAndServe(":8080", metricsMiddleware(mux))
}

Grafana

  • Add Prometheus as a data source (http://localhost:9090).
  • Create a dashboard visualizing http_requests_total.

Info

Exporter libraries exist for many Go metrics (runtime, DB, custom). Use promauto.NewGauge for gauges.

Previous: Testing HTTP Handlers – Table‑Driven ApproachNext: Deploying a gRPC Service on Google Cloud Run