Monitoring Go Applications: Metrics and Alerts from A to Z
\n\nIn modern software development, monitoring is not a luxury but a necessity. For applications written in Go (Golang), which are often used in high-load microservices and infrastructure projects, properly configured monitoring allows you to detect problems in time, optimize performance, and ensure stable operation. In this article, we will break down how to set up monitoring for Go applications from scratch: from collecting basic metrics to configuring alerts and visualization in Grafana.
\n\nWhy is monitoring important for Go applications?
\n\nGo is a compiled language with built-in concurrency support. Go applications often run in critical systems: from API gateways to databases. Without monitoring, you risk:
\n- \n
- Missing memory or goroutine leaks \n
- Not noticing performance degradation \n
- Losing data due to unhandled errors \n
- Experiencing downtime due to resource shortages \n
Proper monitoring gives you a complete picture of your application's health: from CPU and memory usage to response time and error rate.
\n\nKey Metrics for Monitoring Go
\n\nBefore moving on to tools, let's define which metrics are critical for Go applications:
\n\nSystem Metrics
\n- \n
- CPU usage — processor load \n
- Memory usage — memory consumption (including heap and stack) \n
- Goroutines count — number of active goroutines \n
- GC stats — garbage collector statistics (time, number of cycles) \n
- Open file descriptors — number of open file descriptors \n
Business Metrics
\n- \n
- Request latency — response time (p50, p95, p99) \n
- Request rate — number of requests per second \n
- Error rate — percentage of errors \n
- Database connection pool — connection pool status \n
Tools for Monitoring Go
\n\nThe standard stack for monitoring Go applications includes:
\n- \n
- Prometheus — metrics collection and storage system \n
- Grafana — visualization and dashboards \n
- Alertmanager — alert management \n
- Go client libraries — for exporting metrics \n
Setting Up Metrics Collection in Go with Prometheus
\n\nLet's start with practice. To export metrics from a Go application, we will use the official library prometheus/client_golang.
Step 1: Installing Dependencies
\ngo get github.com/prometheus/client_golang/prometheus\ngo get github.com/prometheus/client_golang/prometheus/promhttp\n\nStep 2: Basic HTTP Server with Metrics
\n\npackage main\n\nimport (\n "net/http"\n "github.com/prometheus/client_golang/prometheus"\n "github.com/prometheus/client_golang/prometheus/promhttp"\n)\n\nvar (\n httpRequestsTotal = prometheus.NewCounterVec(\n prometheus.CounterOpts{\n Name: "http_requests_total",\n Help: "Total number of HTTP requests",\n },\n []string{"method", "endpoint", "status"},\n )\n\n httpRequestDuration = prometheus.NewHistogramVec(\n prometheus.HistogramOpts{\n Name: "http_request_duration_seconds",\n Help: "Histogram of HTTP request durations",\n Buckets: prometheus.DefBuckets,\n },\n []string{"method", "endpoint"},\n )\n)\n\nfunc init() {\n prometheus.MustRegister(httpRequestsTotal)\n prometheus.MustRegister(httpRequestDuration)\n}\n\nfunc main() {\n http.Handle("/metrics", promhttp.Handler())\n \n http.HandleFunc("/api/users", func(w http.ResponseWrit