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