登录
首页 >  Golang >  Go问答

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

来源:stackoverflow

时间:2024-04-18 20:03:33 301浏览 收藏

Golang不知道大家是否熟悉?今天我将给大家介绍《如何将 httprouter.Handle 传递给 Prometheus http.HandleFunc》,这篇文章主要会讲到等等知识点,如果你在看完本篇文章后,有更好的建议或者发现哪里有问题,希望大家都能积极评论指出,谢谢!希望我们能一起加油进步!

问题内容

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


解决方案


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

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

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>