[英]How to pass a httprouter.Handle to a Prometheus http.HandleFunc
Cannot pass Prometheus midware into httprouter
endpoint definitions.无法将 Prometheus 中间件传递到
httprouter
端点定义中。
I'm trying to add a Prometheus midware into our endpoint implementation.我正在尝试将 Prometheus 中间件添加到我们的端点实现中。 But our endpoint are using a third party
mux
package called httprouter
.但是我们的端点正在使用名为
httprouter
的第三方mux
package。 Then when I tried to add this midware into existing code base, I cannot find a good way to integrate both together.然后当我试图将这个中间件添加到现有代码库中时,我找不到将两者集成在一起的好方法。
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))
}
My problem is I can not pass my prometheus handle to that httprouter endpoint function as parameter我的问题是我无法将我的 prometheus 句柄作为参数传递给该 httprouter 端点 function
Below is what I want to do:以下是我想要做的:
func InstrumentHandlerFunc(name string, handler httprouter.Handle) httprouter.Handel {
}
router.Get("/hello", InstrumentHandlerFunc("/hello", r.Hello))
If you want to use both of them in the same project, you can achieve the same result with a simple way to listen on different ports
如果要在同一个项目中同时使用它们,可以通过一种简单的方法来侦听不同的
ports
,从而获得相同的结果
router := httprouter.New()
// register prometheus exporter
go func() {
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":8080", nil)
}()
http.ListenAndServe(":80", router)
you can use like this.你可以这样使用。
router.Handler("GET", "/metrics", promhttp.Handler())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.