【发布时间】:2019-04-18 00:03:39
【问题描述】:
无法将 Prometheus 中间件传递到 httprouter 端点定义中。
我正在尝试将 Prometheus 中间件添加到我们的端点实现中。但是我们的端点正在使用第三方 mux 包,名为 httprouter。然后,当我尝试将此中间件添加到现有代码库中时,我找不到将两者集成在一起的好方法。
router := httprouter.New()
router.GET("/hello", r.Hello)
func (r configuration) Hello(w http.ResponseWriter, req *http.Request, ps httprouter.Params)
func InstrumentHandlerFunc(name string, handler http.HandlerFunc) http.HandlerFunc {
counter := prometheus.NewCounterVec(
do something...
)
duration := prometheus.NewHistogramVec(
do something...
)
return promhttp.InstrumentHandlerDuration(duration,
promhttp.InstrumentHandlerCounter(counter, handler))
}
我的问题是我无法将我的 prometheus 句柄作为参数传递给该 httprouter 端点函数
下面是我想做的:
func InstrumentHandlerFunc(name string, handler httprouter.Handle) httprouter.Handel {
}
router.Get("/hello", InstrumentHandlerFunc("/hello", r.Hello))
【问题讨论】:
-
你到底想做什么?我不清楚你所说的“将两者结合在一起”是什么意思
-
有没有可能我可以创建一个返回 httprouter.Handle 作为返回类型的 prometheus 中间件,并且我可以将 httprouter.Handle 作为函数参数之一传递。现在我只能创建常规的 prometheus http.HandleFunc 这种处理函数。
-
docs中声明路由器本身实现了http.Handler接口。所以你可以使用
router.HandleFunc()并传递http.HandlerFunc()。
标签: go prometheus httprouter