Golang普罗米修斯:是否有一种方法来衡量出站请求的指标?

kfgdxczn  于 2023-05-20  发布在  Go
关注(0)|答案(1)|浏览(136)

我有一个服务A,我正在使用prometheus废弃指标,A向另一个服务B发出一些出站请求。有没有一种方法可以获得从A到B的请求的度量?
比如,我想要一个度量计数和A -> B的请求持续时间

bzzcjhmw

bzzcjhmw1#

可以使用prometheus.NewCounterVecprometheus.NewHistogramVec(或proemetheus.NewSummaryVec,取决于用例)记录度量。
我们可以使用自定义标签来识别谁是客户端,谁是服务器。通过这种方式,可以过滤出客户端的request countduration指标到特定的服务器。
下面是示例代码段

客户端。去

package main

import (
    "net/http"
    "time"

    "github.com/labstack/echo/v4"
    "github.com/prometheus/client_golang/prometheus"
    "github.com/prometheus/client_golang/prometheus/promhttp"
)

var (
    requestCounter = prometheus.NewCounterVec(
        prometheus.CounterOpts{
            Name: "client_request_count",
            Help: "Total number of requests from client",
        },
        []string{"client", "server", "method", "route", "status"},
    )

    durationHistorgram = prometheus.NewHistogramVec(
        prometheus.HistogramOpts{
            Name:    "client_request_duration_secs",
            Help:    "Duration of requests from Client",
            Buckets: []float64{0.1, 0.5, 1, 2, 5},
        },
        []string{"client", "server", "method", "route", "status"},
    )
)

func init() {
    prometheus.MustRegister(requestCounter)
    prometheus.MustRegister(durationHistorgram)
}

func main() {

    e := echo.New()
    e.GET("/", func(c echo.Context) error {

        // the request start time
        start := time.Now()
        req, err := http.NewRequest("GET", "http://localhost:8002", nil)
        if err != nil {
            return c.JSON(http.StatusInternalServerError, err.Error())
        }

        resp, err := http.DefaultClient.Do(req)
        if err != nil {
            return c.JSON(http.StatusInternalServerError, err.Error())
        }
        defer resp.Body.Close()

        labels := prometheus.Labels{
            "client": "Server A", // defines the client server
            "server": "Server B", // defines the outbound request server
            "method": "GET",// HTTP method
            "route":  "/", // Request route
            "status": resp.Status,// Response status
        }
        duration := time.Since(start).Seconds()

        // the duration
        durationHistorgram.With(labels).Observe(duration)

        // request api count
        requestCounter.With(labels).Inc()

        return c.String(http.StatusOK, "[Client -> Server] Request send and received")
    })

    e.GET("/metrics", echo.WrapHandler(promhttp.Handler()))

    if err := e.Start(":8001"); err != nil {
        panic(err)
    }
}

server.go

package main

import (
    "net/http"

    "github.com/labstack/echo/v4"
)

func main() {
    e := echo.New()

    e.GET("/", func(c echo.Context) error {
        return c.JSON(http.StatusOK, map[string]interface{}{
            "message": "response sending from server",
        })
    })

    if err := e.Start(":8002"); err != nil {
        panic(err)
    }
}

正如您在代码中看到的,这些是我们的自定义标签。

[]string{"client", "server", "method", "route", "status"},

这些是标签值

labels := prometheus.Labels{
            "client": "Server A", // defines the client server
            "server": "Server B", // defines the outbound request server
            "method": "GET", // HTTP method
            "route":  "/", // Request route
            "status": resp.Status,// Response status
        }

最后我们可以查询

client_request_count {client="Server A", server="Server B"}
  • 服务器A发送给服务器B的请求 *

参考资料

相关问题