登录
首页 >  Golang >  Go问答

将参数包含在路径中,Gin 将指标发送到 prometheus

来源:stackoverflow

时间:2024-02-15 21:33:23 474浏览 收藏

学习Golang要努力,但是不要急!今天的这篇文章《将参数包含在路径中,Gin 将指标发送到 prometheus》将会介绍到等等知识点,如果你想深入学习Golang,可以关注我!我会持续更新相关文章的,希望对大家都能有所帮助!

问题内容

我有一个 gin 服务,其中一个端点如下所示:

const mypath= "/upload-some-file/:uuid"

在我向普罗米修斯发送数据的中间件中,我有这样的东西:

requestCounter = promauto.NewCounterVec(prometheus.CounterOpts{
    Name: "all-http-requests",
    Help: "Total number of http requests",
}, []string{"Method", "Endpoint"})

func Telemetry() gin.HandlerFunc {
    return func(c *gin.Context) {
        // Metrics for requests volume
        requestCounter.With(prometheus.Labels{"Method": c.Request.Method, "Endpoint": c.Request.URL.Path}).Inc()

        c.Next()
    }
}

但我注意到 prometheus 无法确定参数实际上已嵌入到路径中,因此它将每个唯一的 uuid 视为新路径。

有什么方法可以让prometheus意识到它实际上使用的是带有嵌入参数的url吗?


解决方案


我找到了这个https://github.com/gin-gonic/gin/issues/748#issuecomment-543683781

所以我可以简单地执行 c.FullPath() 来获取匹配的路线。

到这里,我们也就讲完了《将参数包含在路径中,Gin 将指标发送到 prometheus》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

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