0

Cannot pass Prometheus midware into httprouter endpoint definitions.

I'm trying to add a Prometheus midware into our endpoint implementation. But our endpoint are using a third party mux package called httprouter. 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

Below is what I want to do:


func InstrumentHandlerFunc(name string, handler httprouter.Handle) httprouter.Handel {

}

router.Get("/hello", InstrumentHandlerFunc("/hello", r.Hello))

4

1 回答 1

0

如果你想在同一个项目中同时使用它们,你可以通过一个简单的方法来实现相同的结果来监听不同的ports

router := httprouter.New()
// register prometheus exporter 
go func() {
    http.Handle("/metrics", promhttp.Handler())
    http.ListenAndServe(":8080", nil)
}()
http.ListenAndServe(":80", router)
于 2019-07-10T07:08:08.457 回答