Skip to content

Lesson 8 of 9 · 26m

Logs, Metrics, and pprof: Seeing the Service You Actually Run

Emit structured logs, low-cardinality metrics, health signals, and controlled profiling endpoints that explain behavior during incidents.

The service you wrote is not the service production runs. Production runs a binary under a scheduler, behind proxies, beside noisy neighbors, during dependency failures you did not design for. Observability closes that gap. The goal is not maximum data; it is enough trustworthy signal to answer what changed, who is affected, and which resource or dependency is failing.

Structured logs beat forensic grep

slog gives structured logging in the standard library:

logger.Info("request completed",
    "request_id", requestID,
    "method", r.Method,
    "route", routePattern,
    "status", status,
    "duration_ms", elapsed.Milliseconds(),
)

Use route patterns, not raw paths. /orders/{id} aggregates; /orders/ord_10492 creates cardinality and can leak identifiers. Put request IDs in response headers so support can connect a user report to evidence.

Metrics answer operational questions

The core HTTP metrics are boring because they work:

http_requests_total{route="/orders/{id}",method="GET",status="200"}
http_request_duration_seconds_bucket{route="/orders/{id}",le="0.1"}
http_inflight_requests
dependency_request_duration_seconds{dependency="billing",result="timeout"}

Track count, latency, status class, in-flight requests, and dependency outcomes. A dashboard nobody checks during incidents is decoration.

Health and readiness make different promises

Liveness means the process is not deadlocked beyond recovery. Readiness means it should receive traffic now:

mux.HandleFunc("GET /livez", func(w http.ResponseWriter, r *http.Request) {
    w.WriteHeader(http.StatusOK)
})

mux.HandleFunc("GET /readyz", func(w http.ResponseWriter, r *http.Request) {
    if err := store.PingContext(r.Context()); err != nil {
        http.Error(w, "not ready", http.StatusServiceUnavailable)
        return
    }
    w.WriteHeader(http.StatusOK)
})

Do not make liveness depend on every dependency unless restarting the process fixes that condition. Otherwise a downstream outage becomes a restart loop.

pprof is powerful and dangerous

Go profiling explains goroutine leaks, CPU hot spots, contention, and memory growth:

go tool pprof http://127.0.0.1:6060/debug/pprof/profile?seconds=30
go tool pprof http://127.0.0.1:6060/debug/pprof/heap
curl -s http://127.0.0.1:6060/debug/pprof/goroutine?debug=1

Importing net/http/pprof registers endpoints on the default mux. Prefer a separate internal listener or strong authentication and network policy. Never expose pprof publicly.

Correlation beats isolated brilliance

Metrics say latency changed. Logs identify route and dependency. Traces show where time went. Even without full tracing, propagate request IDs and dependency timing consistently.

Field notes

  • Do not log passwords, tokens, payment payloads, or Authorization headers.
  • User IDs and object IDs do not belong in metric labels.
  • Readiness can report dependency failure internally; do not leak sensitive detail in public responses.
  • Watch goroutine count and open file descriptors; many Go incidents are concurrency leaks rather than CPU defects.

Was this lesson helpful?

Sign in to rate