Go 应用监控:高级指标与告警

面向初学者的在线Python训练器

轻松学习Python,无需理论负担。在浏览器中直接解决带自动检查的实践任务,无需安装任何东西。

开始课程

Go 应用监控:高级指标与告警

\n

现代 Go 应用不仅需要高性能,还需要可靠的监控。基础指标采集(CPU、内存、请求)仅仅是第一步。在高级层面,需要引入自定义指标、基于业务逻辑配置告警并确保容错性。本文将介绍如何使用 Prometheus、Grafana 和自定义告警来实现 Go 应用的全面监控。

\n\n

1. 使用 Prometheus 在 Go 中实现高级指标采集

\n

prometheus/client_golang 库不仅可以采集标准指标,还可以采集自定义指标。以下是为 HTTP 服务器配置自定义指标采集器的示例。

\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    requestDuration = prometheus.NewHistogramVec(\n        prometheus.HistogramOpts{\n            Name:    "http_request_duration_seconds",\n            Help:    "HTTP 请求执行时间。",\n            Buckets: prometheus.DefBuckets,\n        },\n        []string{"method", "path", "status"},\n    )\n    activeRequests = prometheus.NewGauge(\n        prometheus.GaugeOpts{\n            Name: "http_active_requests_total",\n            Help: "当前活跃请求数。",\n        },\n    )\n)\n\nfunc init() {\n    prometheus.MustRegister(requestDuration)\n    prometheus.MustRegister(activeRequests)\n}\n\nfunc metricsMiddleware(next http.Handler) http.Handler {\n    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {\n        activeRequests.Inc()\n        defer activeRequests.Dec()\n\n        timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {\n            requestDuration.WithLabelValues(r.Method, r.URL.Path, "200").Observe(v)\n        }))\n        defer timer.ObserveDuration()\n\n        next.ServeHTTP(w, r)\n    })\n}\n\nfunc main() {\n    mux := http.NewServeMux()\n    mux.Handle("/metrics", promhttp.Handler())\n    mux.Handle("/api", metricsMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {\n        w.Write([]byte("Hello, Go!"))\n    })))\n    http.ListenAndServe(":8080", mux)\n}
\n\n

在此示例中,我们创建了一个用于请求持续时间的直方图和一个用于活跃请求数的仪表盘。使用标签(labels)可以按方法、路径和状态对指标进行细化。

\n\n

2. 基于业务逻辑配置告警

\n

告警不仅应该是技术性的(如 CPU 高负载),还应该是业务性的。例如,关键端点的响应时间超限或成功交易数量下降。Prometheus 中的告警配置(alerts.yml 文件):

\n\n
groups:\n  - name: go_app_alerts\n    rules:\n      - alert: HighRequestLatency\n        expr: http_request_duration_seconds{job="go-app"} > 1.0\n        for: 5m\n        labels:\n          severity: critical\n        annotations:\n          summary: "请求延迟过高 ({{ $value }}s)"\n          description: "端点 {{ $labels.path }} 延迟超过 1 秒,持续 5 分钟。"\n\n      - alert: ActiveRequestsSurge\n        expr: http_active_requests_total{job="go-app"} > 100\n        for: 2m\n        labels:\n          severity: warning\n        annotations:\n          summary: "活跃请求激增 ({{ $value }})"\n          description: "活跃请求数在 2 分钟内超过 100。"\n\n      - alert: ErrorRateHigh\n        expr: rate(http_requests_total{status=~"5.."}[5m]) > 0.1\n        for: 10m\n        labels:\n          severity: critical\n        annotations:\n          summary: "5xx 错误率过高"\n          description: "过去 10 分钟内错误率超过 10%。"
\n\n

请注意,使用了

博客

书籍推荐