Prometheus计数器:如何使用golang客户端获取当前值?

我正在使用计数器来计算请求数。有没有办法获取普罗米修斯计数器的当前值?

我的目标是重用现有计数器而不分配另一个变量。

Golang prometheus客户端版本为1.1.0。


qq_笑_17
浏览 261回答 4
4回答

桃花长相依

很简单,有一个获取 Prometheus 计数器值的函数import (    "github.com/prometheus/client_golang/prometheus"    dto "github.com/prometheus/client_model/go"    "github.com/prometheus/common/log")func GetCounterValue(metric *prometheus.CounterVec) float64 {    var m = &dto.Metric{}    if err := metric.WithLabelValues("label1", "label2").Write(m); err != nil {        log.Error(err)        return 0    }    return m.Counter.GetValue()}

慕无忌1623718

目前 Golang 官方实现中还没有办法获取计数器的值。您还可以通过增加自己的计数器并使用CounterFunc来收集它来避免重复计数。注意:使用整型并atomic避免并发访问问题// declare the counter as unsigned intvar requestsCounter uint64 = 0// register counter in Prometheus collectorprometheus.MustRegister(prometheus.NewCounterFunc(    prometheus.CounterOpts{        Name: "requests_total",        Help: "Counts number of requests",    },    func() float64 {        return float64(atomic.LoadUint64(&requestsCounter))    }))// somewhere in your codeatomic.AddUint64(&requestsCounter, 1)

慕后森

可以在官方 Golang 实现中读取计数器(或任何指标)的值。我不确定它是什么时候添加的。这对我来说适用于没有向量的简单度量:func getMetricValue(col prometheus.Collector) float64 {&nbsp; &nbsp; c := make(chan prometheus.Metric, 1) // 1 for metric with no vector&nbsp; &nbsp; col.Collect(c)&nbsp; &nbsp; &nbsp; // collect current metric value into the channel&nbsp; &nbsp; m := dto.Metric{}&nbsp; &nbsp; _ = (<-c).Write(&m) // read metric value from the channel&nbsp; &nbsp; return *m.Counter.Value}更新:这是一个更通用的版本,适用于向量和直方图......// GetMetricValue returns the sum of the Counter metrics associated with the Collector// e.g. the metric for a non-vector, or the sum of the metrics for vector labels.// If the metric is a Histogram then number of samples is used.func GetMetricValue(col prometheus.Collector) float64 {&nbsp; &nbsp; var total float64&nbsp; &nbsp; collect(col, func(m dto.Metric) {&nbsp; &nbsp; &nbsp; &nbsp; if h := m.GetHistogram(); h != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; total += float64(h.GetSampleCount())&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; total += m.GetCounter().GetValue()&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; })&nbsp; &nbsp; return total}// collect calls the function for each metric associated with the Collectorfunc collect(col prometheus.Collector, do func(dto.Metric)) {&nbsp; &nbsp; c := make(chan prometheus.Metric)&nbsp; &nbsp; go func(c chan prometheus.Metric) {&nbsp; &nbsp; &nbsp; &nbsp; col.Collect(c)&nbsp; &nbsp; &nbsp; &nbsp; close(c)&nbsp; &nbsp; }(c)&nbsp; &nbsp; for x := range c { // eg range across distinct label vector values&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m := dto.Metric{}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _ = x.Write(&m)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; do(m)&nbsp; &nbsp; }}

拉丁的传说

import (  "github.com/VictoriaMetrics/metrics")var requestsTotal = metrics.NewCounter(`http_requests_total`)//...func getRequestsTotal() uint64 {  return requestsTotal.Get()}例如,只需在所需的计数器上调用Get()函数即可。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go