Monitoring Go (Golang) Applications: A Complete Overview of Tools and Libraries
Monitoring is a critical part of operating any server application. For the Go (Golang) ecosystem, many tools have been developed to track performance, status, and errors in real time. In this article, we will cover the key libraries and approaches for monitoring Go applications, including Prometheus, expvar, pprof, and OpenTelemetry.
Why is monitoring needed for Go applications?
Go applications are often used for high-load microservices, API gateways, and background processors. Without monitoring, you risk missing:
- Memory and goroutine leaks;
- Slow requests and locks;
- Performance degradation under load;
- Connection errors to databases and external services.
1. Prometheus — the standard for metric collection
Prometheus is the de facto standard for metric collection in the Go world. The official client library prometheus/client_golang makes it easy to export metrics in Prometheus format.
Installation
go get github.com/prometheus/client_golang/prometheusgo get github.com/prometheus/client_golang/prometheus/promhttpExample: request counter and time histogram
package main
import ( "net/http" "time" "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 number of HTTP requests", }, []string{"method", "endpoint"}, ) requestDuration = prometheus.NewHistogramVec( prometheus.HistogramOpts{ Name: "http_request_duration_seconds", Help: "Duration of HTTP requests in seconds", Buckets: prometheus.DefBuckets, }, []string{"method", "endpoint"}, ))
func init() { prometheus.MustRegister(requestCount, requestDuration)}
func middleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { start := time.Now() next.ServeHTTP(w, r) duration := time.Since(start).Seconds() requestCount.WithLabelValues(r.Method, r.URL.Path).Inc() requestDuration.WithLabelValues(r.Method, r.URL.Path).Observe(duration) })}
func main() { mux := http.NewServeMux() mux.Handle("/metrics", promhttp.Handler()) mux.Handle("/", middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Hello, World!")) }))) http.ListenAndServe(":8080", mux)}After running, metrics will be available at http://localhost:8080/metrics. Prometheus will be configured to scrape from this endpoint.
2. expvar — built-in metric export
The Go standard library offers the expvar package for publishing application state variables. It requires no external dependencies.
Usage example
package main
import ( "expvar" "net/http")
var ( requests = expvar.NewInt("requests_total") errors = expvar.NewInt("errors_total"))
func handler(w http.ResponseWriter, r *http.Request) { requests.Add(1) // Simulate an error if r.URL.Query().Get("error") == "1" { errors.Add(1) w.WriteHeader(http.StatusInternalServerError) return } w.Write([]byte("OK"))}
func main() { http.HandleFunc("/debug/vars", expvar.Handler().ServeHTTP) http.HandleFunc("/", handler) http.ListenAndServe(":8080", nil)}Metrics are available at the /debug/vars path in JSON format. For integration with Prometheus, you can use <
Also in library
PDF Generation in JavaScript (Node.js): Libraries and Tools
JWT tokens in TypeScript: a complete guide to working with JSON Web Tokens
Testing in Swift (iOS/macOS): Frameworks and Tools
Kysely for TypeScript: Complete Guide with Examples
Image Processing in Java: Resizing and Cropping (resize, crop)