Prometheus metrics in Go (Golang): a complete guide
Prometheus is a powerful open-source monitoring and alerting system that has become the de facto standard in the world of cloud applications. For the Go language (Golang), there is an official client prometheus/client_golang that allows you to easily add metrics to your application. In this article, we will thoroughly cover how to install the library, create the main metric types (counter, gauge, histogram), and export them via HTTP.
1. Installing the library
To work with Prometheus in Go, you need to install the prometheus/client_golang package. Run the command:
go get github.com/prometheus/client_golang/prometheusgo get github.com/prometheus/client_golang/prometheus/promhttpThe first package contains the main metric types, the second contains the HTTP handler for exporting metrics in a format understandable to the Prometheus server.
2. Main metric types
Prometheus supports four main metric types. Let's look at the three most popular ones:
- Counter — a counter that only increases (e.g., number of requests).
- Gauge — a value that can both increase and decrease (e.g., number of active connections).
- Histogram — a histogram that measures the distribution of values (e.g., response time).
3. Example: Counter (request counter)
Let's create a simple HTTP server that counts the number of processed requests:
package main
import ( "fmt" "net/http" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp")
var ( requestsTotal = prometheus.NewCounter( prometheus.CounterOpts{ Name: "http_requests_total", Help: "Total number of HTTP requests.", }, ))
func init() { prometheus.MustRegister(requestsTotal)}
func handler(w http.ResponseWriter, r *http.Request) { requestsTotal.Inc() fmt.Fprintf(w, "Hello, you are visitor #%d", requestsTotal)}
func main() { http.HandleFunc("/", handler) http.Handle("/metrics", promhttp.Handler()) http.ListenAndServe(":8080", nil)}In this example:
prometheus.NewCountercreates a counter with the namehttp_requests_total.prometheus.MustRegisterregisters the metric in the global registry.requestsTotal.Inc()increments the counter by 1 on each request.promhttp.Handler()exports all registered metrics at the/metricsendpoint.
4. Example: Gauge (active connections)
Gauge is ideal for tracking the current state, for example, the number of active goroutines:
var ( activeGoroutines = prometheus.NewGauge( prometheus.GaugeOpts{ Name: "active_goroutines", Help: "Current number of active goroutines.", }, ))
func monitorGoroutines() { for { activeGoroutines.Set(float64(runtime.NumGoroutine())) time.Sleep(5 * time.Second) }}
func main() { prometheus.MustRegister(activeGoroutines) go monitorGoroutines() // ... rest of the code}Here, Set() sets the current value, while Inc() and Dec() allow you to increase or decrease the gauge by one.
5. Example: Histogram (response time)
Histogram collects observations and distributes them into configurable buckets:
var ( requestDuration = prometheus.NewHistogram( prometheus.HistogramOpts{ Name: "http_request_duration_seconds", Help: "Histogr