I have a couple of urls to access,and i want to monitor every request's cost time with prometheus.
but i don't know use what kind of metric to collect data.any help?
this is demo code:
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())
}
Prometheus recommends you use a histogram to store such things. It essentially counts requests based on which time "bucket" it falls into. There is an example of how to use this in the godoc.
I prefer histograms to the "summary" type, because it is easier to aggregate when you have many servers in play. If all you are keeping is the average / 99th percentile time on each server, it is hard to know the global averages from that information alone.
Histograms keep running counts per bucket per server, and so you can aggregate data across servers without significant loss in the future.
A good rundown of those types is available on this page.