Go
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions and Methods
-
Section 4: Concurrency
-
Section 5: Packages and Tooling
-
Section 6: More Standard Library
-
Section 7: Building Services
-
Section 8: Advanced Go
-
Section 9: Go in the Cloud-Native Ecosystem
-
Section 10: Data Structures and Algorithms in Go
-
Section 11: Testing and Deployment
-
Section 12: Practical Projects
-
Section 13: More Standard Library Practice
-
Section 14: More Practice Projects
-
Section 15: Design Patterns in Go
-
Section 16: Interview Practice
-
Section 17: Package fmt In Depth
-
Section 18: Package strings and strconv
-
Section 19: Package os and io
-
Section 20: Package time
-
Section 21: Package sort and container
-
Section 22: Package encoding
-
Section 23: Package net/http In Depth
-
Section 24: Package context
-
Section 25: Package regexp and bytes
-
Section 26: Package errors In Depth
-
Section 27: Package crypto and hash
-
Section 28: Package flag and log
-
Section 29: Package sync In Depth
-
Section 30: More Practice Exercises
-
Section 31: Go Modules and Workspaces In Depth
-
Section 32: Generics Deep Dive (Go 1.18+)
-
Section 33: Testing Package In Depth
-
Section 34: More Interview and Whiteboard Practice
-
Section 35: Package math and unicode
-
Section 36: Package path and filepath
-
Section 37: Package database/sql
-
Section 38: Package text/template and html/template
-
Section 39: Package archive and compress
-
Section 40: Lower-Level net Package
-
Section 41: Package runtime and reflect
-
Section 42: Package embed and io/fs
-
Section 43: Go Assembly and CGO Basics
-
Section 44: Building CLIs and TUIs
-
Section 45: Go Performance Tuning
-
Section 46: More Real-World Projects
-
Section 47: Go in Production
-
Section 48: Go Design Patterns
-
Section 49: Go Interfaces Deep Dive
-
Section 50: Final Practice and Review
67: Instrumenting Go Services with Prometheus Metrics
A few years back, I was on call for a service that handled payment webhooks. Everything looked "green" on the dashboard—the CPU was low, the memory was stable, and the health check endpoint was returning 200 OK. But our customer support Slack channel was exploding because users weren't seeing their credits updated. When I finally dug into the logs, I found that 15% of requests were timing out after 30 seconds, but because they were timing out at the load balancer level, the application logs were practically silent. I was flying blind. I didn't need more logs; I needed a way to see the distribution of request latencies in real-time. That's exactly why we instrument with Prometheus.
Choosing Your Metric Type
In the Go ecosystem, the prometheus/client_golang library is the gold standard. Before you start adding code, you have to decide which "tool" fits the data you're tracking. If you pick the wrong one, your queries in Grafana will be a nightmare.
First, you have Counters. These only ever go up. Think of them like an odometer in a car. Use these for things like http_requests_total or orders_processed_total. You don't care what the absolute number is as much as you care about the rate of increase. If the rate of 500-errors spikes, you have a problem.
Then there are Gauges. These can go up and down. Use these for "current state" measurements: the number of active Goroutines, the current size of a worker queue, or memory usage. If you're tracking how many users are currently online, that's a Gauge.
Finally, there are Histograms. These are the most powerful and the most misunderstood. A histogram doesn't store every single value; instead, it counts how many observations fall into predefined "buckets." This is how we track latencies. You don't want the average latency—averages hide the pain of your slowest users. You want the 99th percentile (P99), and histograms make that possible.
Wiring Prometheus into your Go Service
To actually get these metrics out of your app and into a Prometheus server, you need to expose a scrape endpoint—usually /metrics. Prometheus is a "pull" system; it hits your app every few seconds and asks, "What are your current numbers?"
package main
import (
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// I use promauto to handle the registration automatically.
// It saves you from having to call prometheus.MustRegister() manually.
var (
opsProcessed = promauto.NewCounter(prometheus.CounterOpts{
Name: "myapp_processed_ops_total",
Help: "The total number of processed operations",
})
requestDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
Name: "myapp_request_duration_seconds",
Help: "Latency of requests in seconds",
Buckets: []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10},
}, []string{"handler"})
)
func handler(w http.ResponseWriter, r *http.Request) {
start := time.Now()
// Simulate some work
time.Sleep(100 * time.Millisecond)
opsProcessed.Inc()
requestDuration.WithLabelValues("main_handler").Observe(time.Since(start).Seconds())
w.Write([]byte("Done!"))
}
func main() {
http.HandleFunc("/do-work", handler)
// This is the magic endpoint Prometheus scrapes
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":8080", nil)
}
The Power of Labels and the Danger of Cardinality
You'll notice I used NewHistogramVec instead of just NewHistogram. The "Vec" stands for Vector, which allows you to add labels. Labels are essentially dimensions. Instead of creating request_duration_login and request_duration_logout, you create one metric with a label handler="login".
But a word of caution: be extremely careful with label values. This is where most engineers shoot themselves in the foot. If you put a User ID or a Request ID as a label value, you create "high cardinality." Prometheus creates a new time series for every unique combination of labels. If you have 100,000 users and you put user_id in a label, you'll crash your Prometheus server by blowing out its memory. Keep label values bounded—think HTTP status codes, region names, or service versions.
📋 Practical Task
Instrumenting a Payment Gateway Simulation
You are tasked with adding observability to a simulated payment gateway. The service currently processes payments but provides no insight into its performance or error rates.
Requirements:
- Create a
Counternamedpayment_attempts_totalthat tracks every payment attempt. It must have a label calledstatus(values: "success" or "failure"). - Create a
Gaugenamedactive_payment_connectionsthat increments when a payment starts and decrements when it finishes. - Create a
Histogramnamedpayment_processing_secondsthat tracks the time it takes to process a payment, using buckets:0.1, 0.5, 1.0, 2.0, 5.0. - Expose these via the
/metricsendpoint on port:8080.
Starter Logic:
Your handler should randomly simulate a payment taking between 50ms and 3s, and randomly fail about 20% of the time to ensure your status="failure" counter is actually being tested.
There are no comments for now.