无法将 Prometheus 中间件传递到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))
}
我的问题是我无法将我的普罗米修斯句柄作为参数传递给该 httprouter 端点函数
以下是我想做的:
func InstrumentHandlerFunc(name string, handler httprouter.Handle) httprouter.Handel {
}
router.Get("/hello", InstrumentHandlerFunc("/hello", r.Hello))
如果您想在同一项目中同时使用它们,您可以通过一种简单的方法来收听不同的ports
router := httprouter.New()
// register prometheus exporter
go func() {
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":8080", nil)
}()
http.ListenAndServe(":80", router)
你可以这样使用。
router.Handler("GET", "/metrics", promhttp.Handler())