doureng5668 2017-08-28 12:04
浏览 515

如何在Golang Web服务器中使用Prometheus监视请求成本时间

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())
}
  • 写回答

1条回答 默认 最新

  • dongxiao9583 2017-08-28 13:28
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计