Monitoring Go Applications: Metrics and Alerts — A Complete Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Monitoring Go Applications: Metrics and Alerts from A to Z

\n\n

In 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\n

Why is monitoring important for Go applications?

\n\n

Go 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
\n\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\n

Key Metrics for Monitoring Go

\n\n

Before moving on to tools, let's define which metrics are critical for Go applications:

\n\n

System 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
\n\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
\n\n

Tools for Monitoring Go

\n\n

The 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
\n\n

Setting Up Metrics Collection in Go with Prometheus

\n\n

Let's start with practice. To export metrics from a Go application, we will use the official library prometheus/client_golang.

\n\n

Step 1: Installing Dependencies

\n
go get github.com/prometheus/client_golang/prometheus\ngo get github.com/prometheus/client_golang/prometheus/promhttp
\n\n

Step 2: Basic HTTP Server with Metrics

\n\n
package 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                

Blogs

Book Recommendations