【发布时间】:2017-08-28 12:04:04
【问题描述】:
我有几个 url 可以访问,我想用 prometheus 监控每个请求的成本时间。
但我不知道使用什么样的指标来收集数据。有帮助吗?
这是演示代码:
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())
}
【问题讨论】:
标签: go webserver prometheus