Prometheus是云原生监控的事实标准。本文介绍监控体系搭建和告警配置。 ## Prometheus配置 ```yaml global: scrape_interval: 15s scrape_configs: - job_name: 'kubernetes-pods' kubernetes_sd_configs: - role: pod ``` ## 应用埋点 ```go var ( httpRequestsTotal = prometheus.NewCounterVec( prometheus.CounterOpts{ Name: "http_requests_total", }, []string{"method", "path", "status"}, ) ) ``` ## 告警规则 ```yaml groups: - name: app-alerts rules: - alert: HighErrorRate expr: rate(http_requests_total{status=~"5.."}[5m]) / rate(http_requests_total[5m]) > 0.05 for: 5m labels: severity: critical ``` ## Alertmanager配置 ```yaml route: group_by: ['alertname'] receiver: 'slack' routes: - match: severity: critical receiver: 'pagerduty' ``` 完善的监控告警体系是生产环境稳定运行的保障。