zeromicro / go-zero

A cloud-native Go microservices framework with cli tool for productivity.

Home Page:https://go-zero.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

prometheus 可否提供一个手动开启的功能

wuyazi opened this issue · comments

使用场景:
内置的 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 正常执行下去

可以通过自定义指标收集的Handler来实现指标收集功能

This issue is stale because it has been open for 30 days with no activity.

This issue was closed because it has been inactive for 14 days since being marked as stale.