Go服务暴露/metrics端点需注册promhttp.Handler()并显式调用prometheus.MustRegister(prometheus.NewGoCollector());建议用独立端口,确保Counter被Inc()触发且label匹配,检查Prometheus targets、监听地址和防火墙;for: 2m要求连续8次评估为true,应设为≥3×scrape_interval。
怎么让 Go 服务暴露 /metrics 端点
核心就一句话:注册 promhttp.Handler() 到 HTTP 路由,它会自动输出 Go 运行时指标(如 go_goroutines、go_memstats_alloc_bytes)和你自定义的指标。不手动调用 prometheus.MustRegister() 注册默认收集器,这些基础指标就不会出现。
- 必须显式注册运行时指标:加一行
prometheus.MustRegister(prometheus.NewGoCollector()),否则go_goroutines这类关键指标压根不会上报 -
/metrics建议用独立端口(比如:9091),避免和业务端口混用——防止业务 panic 导致监控端点一起挂掉 - 别用
http.ListenAndServe(":8080", nil)直接启动,nil 的 handler 会把所有请求都打到/metrics上;正确写法是http.ListenAndServe(":9091", nil)配合http.Handle("/metrics", ...)
为什么 http_requests_total 指标没出现在 /metrics 页面
常见原因是指标没被真正“触发”——Counter 不会主动上报零值,只有调用 .Inc() 或 .Add() 后才会在 /metrics 中显示。刚启动服务时页面里根本看不到你定义的 Counter,属于正常现象。
- 确保 handler 函数里调用了
httpRequestsTotal.WithLabelValues(...).Inc(),且 label 值非空(空字符串或 nil 会导致 panic) - 标签名必须和
NewCounterVec初始化时声明的一致,比如定义了[]string{"method", "endpoint", "status"},但调用时传了"GET", "/api/v1", "200"就对,传成"GET", "/", "2xx"会导致 label 不匹配,指标虽存但查不到 - 如果用
prometheus.NewCounter()(非 Vec 版),就别带 label,否则.WithLabelValues()会 panic
Prometheus 抓不到 Go 服务的指标?检查这三处
最常卡在配置和网络层,不是代码问题。Prometheus 日志里如果出现 server returned HTTP status 404 Not Found 或 context deadline exceeded,基本就是下面某个环节断了。