【问题标题】:Prometheus count non-static data using counterPrometheus 使用计数器计数非静态数据
【发布时间】:2018-03-09 16:23:16
【问题描述】:

我正在尝试计算唯一 URI 的数量并记录它们。这些 URI 会随着时间而改变,并且可以有多个相同类型的 URI。例如,可以有多个“/foo”和“/bar”,并且可以输入一个新的 URI - 比如说“pooh” - 我必须将它们添加到计数器并保持计数。在这种情况下,我不能使用常量标签。例如,如果我要按方法和/或状态码计算 http 请求的数量,我可以这样做:

    httpRequestInfo := prometheus.NewCounterVec(
        prometheus.CounterOpts{
            Name:        "http_requests_sum",
            ConstLabels: prometheus.Labels{"component": "foo"},
            Help:        " A Counter of the number of each type of request by status code and method",
        },
        []string{"code", "method"},
    )

在这种情况下如何使用计数器?谢谢!

【问题讨论】:

    标签: go prometheus


    【解决方案1】:

    通常要避免使用 ConstLabels。你想做的是httpRequestInfo.WithLabelValues("404", "get").Inc()

    因为这是一个计数器,所以它应该有一个 _total 后缀,而不是汇总指标使用的 _sum

    您可能希望使用InstrumentHandlerCounter,而不是自己实现所有逻辑来跟踪 HTTP 请求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-28
      • 1970-01-01
      • 2018-01-01
      • 2019-08-11
      • 1970-01-01
      相关资源
      最近更新 更多