2017-08-28 116 views
-1

我有幾個訪問URL,並且我想用prometheus監視每個請求的成本時間。如何使用golang web服務器中的prometheus監控請求成本時間

但我不知道使用什麼樣的度量來收集data.any幫助?

這是演示代碼:

package main 

import (
    "github.com/prometheus/client_golang/prometheus" 
    "io/ioutil" 
    "net/http" 
    "fmt" 
    "time" 
) 
var (
    resTime = 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) 
    resTime.WithLabelValues(fmt.Sprintf("%d", costTime)).Observe(costTime.Seconds()) 
} 

回答

2

普羅米修斯建議您使用histogram來存儲這樣的事情。它主要根據它所屬的時間「桶」來計算請求。有一個示例說明如何在godoc中使用它。

我更喜歡使用直方圖作爲「摘要」類型,因爲當您有很多服務器在場時,它更容易聚合。如果您保留的是每臺服務器上的平均/百分之九十九的時間,則很難僅從該信息知道全局平均值。

柱狀圖保持每個服務器每桶的運行計數,因此您可以在服務器之間聚合數據,而不會在未來出現重大損失。

this page提供這些類型的良好破敗。

相關問題