gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 03:19:50 24 4
gpt4 key购买 nike

无法将 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))
}

我的问题是我无法将我的 prometheus 句柄作为参数传递给该 httprouter 端点函数

下面是我想做的:


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

}

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

最佳答案

如果你想在同一个项目中使用它们,你可以通过一种简单的方法来监听不同的端口

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

关于go - 如何将 httprouter.Handle 传递给 Prometheus http.HandleFunc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55737480/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com