云原生应用通常采用微服务架构部署,服务实例动态变化,传统的监控方式难以适配这种场景。Prometheus作为云原生生态中主流的监控系统,支持服务发现、指标拉取、报警触发等能力,和Golang应用结合可以快速实现监控需求。

Golang应用暴露Prometheus指标
首先需要在Golang项目中引入Prometheus的客户端库,用于定义和暴露监控指标。常用的指标类型有计数器、仪表盘、直方图等,可根据业务需求选择合适的类型。
先安装依赖:
// 安装Prometheus Go客户端库 go get github.com/prometheus/client_golang/prometheus go get github.com/prometheus/client_golang/prometheus/promhttp
下面是一个简单的示例,在Golang应用中暴露HTTP请求相关的指标:
package main
import (
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// 定义计数器指标,统计HTTP请求总次数
var httpRequestsTotal = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "http_requests_total",
Help: "Total number of HTTP requests",
},
[]string{"method", "path", "status"}, // 定义标签,用于区分不同请求
)
func init() {
// 注册指标到Prometheus默认注册表
prometheus.MustRegister(httpRequestsTotal)
}
func main() {
// 处理业务请求
http.HandleFunc("/api/user", func(w http.ResponseWriter, r *http.Request) {
// 模拟请求处理
status := "200"
// 记录请求指标
httpRequestsTotal.WithLabelValues(r.Method, "/api/user", status).Inc()
w.Write([]byte("user info"))
})
// 暴露/metrics端点,供Prometheus拉取指标
http.Handle("/metrics", promhttp.Handler())
// 启动服务,监听8080端口
http.ListenAndServe(":8080", nil)
}
运行程序后,访问http://127.0.0.1:8080/metrics就可以看到暴露的指标内容,其中包含我们定义的http_requests_total指标。
配置Prometheus采集指标
接下来需要配置Prometheus,让它定期拉取Golang应用的指标。Prometheus的配置文件通常是prometheus.yml,如果是静态部署的应用,可以添加如下配置:
global:
scrape_interval: 15s # 全局拉取间隔,默认15秒
scrape_configs:
- job_name: "golang_app" # 任务名称
static_configs:
- targets: ["127.0.0.1:8080"] # Golang应用的地址和端口
如果是云原生环境,比如应用运行在Kubernetes中,可以使用服务发现的方式,自动发现应用实例,不需要手动配置每个实例的地址:
scrape_configs:
- job_name: "golang_app_k8s"
kubernetes_sd_configs:
- role: pod # 发现Kubernetes中的Pod
relabel_configs:
# 只采集包含prometheus.io/scrape=true注解的Pod
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
action: keep
regex: true
# 将Pod的IP和端口拼接为拉取地址
- source_labels: [__meta_kubernetes_pod_ip, __meta_kubernetes_pod_annotation_prometheus_io_port]
action: replace
regex: (.+):(.+)
target_label: __address__
replacement: $1:$2
配置完成后重启Prometheus,就可以在Prometheus的UI界面中看到采集到的Golang应用指标了。
设置Prometheus报警规则
采集到指标后,需要设置报警规则,当指标出现异常时触发报警。报警规则需要定义在独立的规则文件中,然后在Prometheus配置中引用。
先创建报警规则文件alert_rules.yml:
groups:
- name: golang_app_alerts
rules:
# 规则1:HTTP请求错误率超过5%触发报警
- alert: HighHttpErrorRate
expr: sum(rate(http_requests_total{status=~"5.."}[5m])) / sum(rate(http_requests_total[5m])) > 0.05
for: 2m # 持续2分钟满足条件才触发报警
labels:
severity: critical
annotations:
summary: "Golang应用HTTP错误率过高"
description: "应用{{ $labels.instance }}的HTTP 5xx错误率超过5%,当前值为{{ $value }}"
# 规则2:应用实例不可用触发报警
- alert: GolangAppDown
expr: up{job="golang_app"} == 0
for: 1m
labels:
severity: critical
annotations:
summary: "Golang应用实例不可用"
description: "应用实例{{ $labels.instance }}已经下线超过1分钟"
然后在Prometheus配置中引用规则文件:
global:
scrape_interval: 15s
rule_files:
- "alert_rules.yml" # 引用报警规则文件
scrape_configs:
- job_name: "golang_app"
static_configs:
- targets: ["127.0.0.1:8080"]
Prometheus会定期评估报警规则,当规则条件满足时,会生成报警事件,这些报警事件可以对接Alertmanager,再通过邮件、钉钉、企业微信等方式通知相关人员。
常见问题说明
指标标签不要过多
Prometheus的指标标签会增加存储和查询开销,不要定义过多无意义的标签,标签基数过高会导致Prometheus性能下降。
拉取间隔合理设置
拉取间隔过短会增加应用和Prometheus的负担,过长会导致报警延迟,通常设置为15秒到60秒比较合适,根据业务对监控实时性的要求调整。
云原生环境适配
如果应用运行在Kubernetes中,除了服务发现配置,还可以给Pod添加对应的注解,比如prometheus.io/scrape: "true"、prometheus.io/port: "8080",方便Prometheus自动发现采集。
GolangPrometheus云原生应用指标采集报警修改时间:2026-07-15 11:45:32