亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Prometheus計數器:如何使用golang客戶端獲取當前值?

Prometheus計數器:如何使用golang客戶端獲取當前值?

Go
qq_笑_17 2023-07-26 13:17:03
我正在使用計數器來計算請求數。有沒有辦法獲取普羅米修斯計數器的當前值?我的目標是重用現有計數器而不分配另一個變量。Golang prometheus客戶端版本為1.1.0。
查看完整描述

4 回答

?
桃花長相依

TA貢獻1860條經驗 獲得超8個贊

很簡單,有一個獲取 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()

}


查看完整回答
反對 回復 2023-07-26
?
慕無忌1623718

TA貢獻1744條經驗 獲得超4個贊

目前 Golang 官方實現中還沒有辦法獲取計數器的值。

您還可以通過增加自己的計數器并使用CounterFunc來收集它來避免重復計數。

注意:使用整型并atomic避免并發訪問問題

// declare the counter as unsigned int

var requestsCounter uint64 = 0


// register counter in Prometheus collector

prometheus.MustRegister(prometheus.NewCounterFunc(

? ? prometheus.CounterOpts{

? ? ? ? Name: "requests_total",

? ? ? ? Help: "Counts number of requests",

? ? },

? ? func() float64 {

? ? ? ? return float64(atomic.LoadUint64(&requestsCounter))

? ? }))


// somewhere in your code

atomic.AddUint64(&requestsCounter, 1)


查看完整回答
反對 回復 2023-07-26
?
慕后森

TA貢獻1802條經驗 獲得超5個贊

可以在官方 Golang 實現中讀取計數器(或任何指標)的值。我不確定它是什么時候添加的。


這對我來說適用于沒有向量的簡單度量:


func getMetricValue(col prometheus.Collector) float64 {

    c := make(chan prometheus.Metric, 1) // 1 for metric with no vector

    col.Collect(c)      // collect current metric value into the channel

    m := dto.Metric{}

    _ = (<-c).Write(&m) // read metric value from the channel

    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 {

    var total float64

    collect(col, func(m dto.Metric) {

        if h := m.GetHistogram(); h != nil {

                total += float64(h.GetSampleCount())

        } else {

                total += m.GetCounter().GetValue()

        }

    })

    return total

}


// collect calls the function for each metric associated with the Collector

func collect(col prometheus.Collector, do func(dto.Metric)) {

    c := make(chan prometheus.Metric)

    go func(c chan prometheus.Metric) {

        col.Collect(c)

        close(c)

    }(c)

    for x := range c { // eg range across distinct label vector values

            m := dto.Metric{}

            _ = x.Write(&m)

            do(m)

    }

}


查看完整回答
反對 回復 2023-07-26
?
拉丁的傳說

TA貢獻1789條經驗 獲得超8個贊

import (

? "github.com/VictoriaMetrics/metrics"

)


var requestsTotal = metrics.NewCounter(`http_requests_total`)

//...


func getRequestsTotal() uint64 {

? return requestsTotal.Get()

}

例如,只需在所需的計數器上調用Get()函數即可。


查看完整回答
反對 回復 2023-07-26
  • 4 回答
  • 0 關注
  • 339 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號