prometheus如何监控每个请求的耗时情况

发布于 2022-09-06 04:09:52 字数 997 浏览 13 评论 0

1.项目中需要使用prometheus监控每个请求的耗时情况和响应结果(http.StatusCode)

2.看了prometheus的文档,依旧不是很明白

3.下面是一段示例demo,我应该使用哪种metric和方式来进行监控呢?

package main

import (
    "github.com/prometheus/client_golang/prometheus"
    "io/ioutil"
    "net/http"
    "fmt"
    "time"
)
var (
    respTime = prometheus.NewSummaryVec(
        prometheus.SummaryOpts{
            Name: "response_time",
            Help: "cost time per request",
        },
        []string{"costTime"},
    )
)


func main() {
    urls := []string{"http://www.google.com","http://www.google.com"}

    for _,url := range urls {
        request(url)
    }
}

func request(url string){
    startTime := time.Now()
    response,_ := http.Get(url)
    defer response.Body.Close()
    _,err := ioutil.ReadAll(response.Body)
    if err != nil {
        fmt.Println(err)
    }
    costTime := time.Since(startTime)

    respTime.WithLabelValues(fmt.Sprintf("%d", costTime)).Observe(costTime.Seconds())
}

求指教

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清醇 2022-09-13 04:09:52

自己研究一下pprof,也许你会找到答案

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文