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

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())
}

求指教

阅读 15k
1 个回答

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

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题