鍍金池/ 問答/GO  網(wǎng)絡(luò)安全/ prometheus如何監(jiān)控每個請求的耗時情況

prometheus如何監(jiān)控每個請求的耗時情況

1.項目中需要使用prometheus監(jiān)控每個請求的耗時情況和響應(yīng)結(jié)果(http.StatusCode)

2.看了prometheus的文檔,依舊不是很明白

3.下面是一段示例demo,我應(yīng)該使用哪種metric和方式來進(jìn)行監(jiān)控呢?

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

求指教

回答
編輯回答
吢涼

自己研究一下pprof,也許你會找到答案

2017年3月6日 01:59