You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Go Fiber中Prometheus指标方法截断与路径拼接问题求助

Go Fiber中Prometheus指标异常问题排查求助

我在Go Fiber应用里遇到了Prometheus指标异常的问题,具体表现有两个:

  • HTTP方法值被错误截断,比如GET变成GETT,POST变成POS;
  • 路径出现拼接情况,不同请求路径被合并成一个,比如/api/v4/verify/otp和/api/v4/creator-program/is-creator-program-unlocked会被拼成/api/v4/verify/otp/api/v4/creator-program/is-creator-program-unlocked。

异常指标示例

http_request_duration_seconds_bucket{error="",http_code="200",method="GETT",path="/api/v4/verify/otp",le="0.005"} 0

http_request_duration_seconds_bucket{error="",http_code="200",method="POST",path="/api/v4/creator-program/is-creator-program-unlocked",le="0.005"} 0

有时路径会出现拼接:

http_request_duration_seconds_bucket{error="",http_code="200",method="POST",path="/api/v4/verify/otp/api/v4/creator-program/is-creator-program-unlocked",le="0.005"} 0

请求拦截中间件代码

func RequestInterceptor(c *fiber.Ctx) error {
    start := time.Now()
    err := c.Next()
    latency := time.Since(start).Seconds()
    statusCode := strconv.Itoa(c.Response().StatusCode())
    errMessage := ""
    if err != nil{
        errMessage = err.Error()
    }

    path :=  c.Route().Path

    method := c.Method()

    if path == "/metrics"{
        return nil
    }
    log.Printf("path: %s, method: %s, statusCode: %s, latency: %v,",path,method,statusCode,latency)
    monitoring.RecordRequestLatency(path,method,errMessage,statusCode,latency)
    return err
}

Prometheus指标初始化代码

var (
    // Define the histogram to track request duration and other metrics.
    httpRequestDurationHisotogram = prometheus.NewHistogramVec(
        prometheus.HistogramOpts{
            Name:    "http_request_duration_seconds",
            Help:    "Histogram of HTTP request durations.",
            Buckets: prometheus.DefBuckets, // Default buckets: [0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10]
        },
        []string{"path", "method", "error", "http_code"},
    )
)

func init() {
    // Register the histogram with Prometheus
    prometheus.MustRegister(httpRequestDurationHisotogram)
}

func RecordRequestLatency(path string, method string, errorString string, statusCode string, latency float64){
    httpRequestDurationHisotogram.WithLabelValues(
        path,
        method,
        errorString,
        statusCode,
    ).Observe(latency)
}

请求日志

2025/02/07 11:49:50 path: /api/v4/verify/otp, method: POST, statusCode: 200, latency: 0.928682333
2025/02/07 11:51:09 path: /api/v4/creator-program/is-creator-program-unlocked, method: POST, statusCode: 200, latency: 0.093867875
2025/02/07 11:51:22 path: /api/v4/v2/communities/:id/channels, method: GET, statusCode: 200, latency: 0.262837625

从日志看,路径和方法都是正确的,但Prometheus指标里却出现异常。有没有人遇到过类似问题?或者有什么排查方向可以建议?

内容的提问来源于stack exchange,提问作者rahul kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 13:52:38