无法将普罗米修斯中间件传递到httprouter终结点定义中。
我正在尝试将Prometheus中间件添加到我们的端点实现中。但是我们的端点使用的是名为httprouter的第三方mux包。然后,当我试图将这个中间件添加到现有的代码库中时,我找不到一个好的方法来将两者集成在一起。
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))发布于 2019-07-10 15:08:08
如果您想在同一项目中同时使用这两种方法,只需通过一种简单的方式监听不同的ports即可达到相同的效果
router := httprouter.New()
// register prometheus exporter
go func() {
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":8080", nil)
}()
http.ListenAndServe(":80", router)https://stackoverflow.com/questions/55737480
复制相似问题