【问题标题】:how to monitor request cost-time with prometheus in golang webserver如何在 golang webserver 中使用 prometheus 监控请求成本时间
【发布时间】: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


    【解决方案1】:

    Prometheus 建议您使用 histogram 来存储此类内容。它本质上是根据它落入哪个时间“桶”来计算请求。在godoc 中有一个如何使用它的示例。

    我更喜欢直方图而不是“摘要”类型,因为当您有许多服务器在运行时,它更容易聚合。如果您只保留每台服务器上的平均/第 99 个百分位时间,则很难仅从该信息中了解全球平均值。

    直方图会持续计算每个服务器的每个存储桶的计数,因此您可以跨服务器聚合数据,而不会在未来造成重大损失。

    this page 上提供了这些类型的详细介绍。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-04
      相关资源
      最近更新 更多