首页 > 解决方案 > Gin 将指标发送到 promethesus,其中 URL 在路径中有参数

问题描述

我有一个杜松子酒服务,其中一个端点看起来像这样:

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?

标签: goprometheusgo-gin

解决方案


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

所以我可以简单地做得到c.FullPath()匹配的路线。


推荐阅读