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 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配