duai5344 2019-04-18 00:03
浏览 595

如何将httprouter.Handle传递给Prometheus http.HandleFunc

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))

  • 写回答

1条回答 默认 最新

  • dongyue110702 2019-07-10 07:08
    关注

    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

    router := httprouter.New()
    // register prometheus exporter 
    go func() {
        http.Handle("/metrics", promhttp.Handler())
        http.ListenAndServe(":8080", nil)
    }()
    http.ListenAndServe(":80", router)
    
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向