go 12 lines · 1 tab

expvar counters for quick-and-dirty production introspection

Leah Thompson Jan 2026
1 tab
package observability

import (
  "expvar"
  "net/http"
)

var RequestsTotal = expvar.NewInt("requests_total")

func Register(mux *http.ServeMux) {
  mux.Handle("/debug/vars", expvar.Handler())
}
1 file · go Explain with highlit

Sometimes you want a quick diagnostic without wiring a full metrics stack. expvar exposes variables at /debug/vars in a standard JSON format. I use it for a handful of counters like “requeststotal” and “jobsfailed” when I’m bootstrapping a service or debugging in a constrained environment. The important point is to keep variables low cardinality and avoid storing user data. Because expvar is global, I treat it as a debug interface: expose it only on an internal listener or behind auth. In production, Prometheus is usually better, but expvar is surprisingly useful for ad-hoc checks during incident response. It’s also nearly zero setup: define variables, increment them, and register the handler. This snippet shows a simple counter you can bump from handlers and jobs.


Related snips

Share this code

Here's the card — post it anywhere.

expvar counters for quick-and-dirty production introspection — share card
Link copied