prometheus 可否提供一个手动开启的功能
wuyazi opened this issue · comments
wuyazi commented
使用场景:
内置的 prometheus 是单独监听了一个端口,但是项目需求是要把 /metrics 和 api 放在同一个端口
所以,我自己在路由里手动集成了 prometheus,没用 go-zero 自带的 StartAgent,如下:
{http.MethodGet, "/metrics", promhttp.Handler().ServeHTTP},
但是这样 go-zero 的 prometheus 没启动的话,enabled 就是 false
当执行到 go-zero 的 PrometheusHandler 的时候,就直接 return 了,api 指标没收集到 prometheus 里,代码如下:
// PrometheusHandler returns a middleware that reports stats to prometheus.
func PrometheusHandler(path string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
if !prometheus.Enabled() {
return next
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
startTime := timex.Now()
cw := &security.WithCodeResponseWriter{Writer: w}
defer func() {
metricServerReqDur.Observe(int64(timex.Since(startTime)/time.Millisecond), path)
metricServerReqCodeTotal.Inc(path, strconv.Itoa(cw.Code))
}()
next.ServeHTTP(cw, r)
})
}
}
所以:能否提供一个手动开启 prometheus.Enabled() 的方法,可以让 PrometheusHandler 正常执行下去
dawn_zhou commented
可以通过自定义指标收集的Handler来实现指标收集功能
github-actions commented
This issue is stale because it has been open for 30 days with no activity.
github-actions commented
This issue was closed because it has been inactive for 14 days since being marked as stale.