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

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

Prometheus 收集器失敗并顯示“之前收集的指標具有相同的名稱和標簽值”

Prometheus 收集器失敗并顯示“之前收集的指標具有相同的名稱和標簽值”

Go
米琪卡哇伊 2022-06-06 14:47:20
我有一個將溫度測量值公開為以下格式的 JSON 的設備:[  {    "dataPointId": 123456,    "values": [      {        "t": 1589236277000,        "v": 14.999993896484398      },      {        "t": 1589236877000,        "v": 14.700006103515648      },      {        "t": 1589237477000,        "v": 14.999993896484398      },[..]如您所見,這些值包含時間戳和溫度測量值。我想通過 Prometheus 指標公開這些測量結果,所以我正在使用prometheus/client_golang它來構建一個導出器。我的期望是/metrics端點然后從上面的數據中暴露出類似的東西:# HELP my_temperature_celsius Temperature# TYPE my_temperature_celsius gaugemy_temperature_celsius{id="123456"} 14.999993896484398 1589236277000my_temperature_celsius{id="123456"} 14.700006103515648 1589236877000my_temperature_celsius{id="123456"} 14.999993896484398 1589237477000我實現了一個簡單的prometheus.Collector,我正在添加我的靜態指標,沒有任何問題。對于上面的測量,NewMetricWithTimestamp似乎是添加帶有時間戳的指標的唯一方法,所以我使用這樣的方法迭代這些值:for _, measurements := range dp.Values {  ch <- prometheus.NewMetricWithTimestamp(    time.Unix(measurements.T, 0),    prometheus.MustNewConstMetric(      collector.temperature,      prometheus.GaugeValue,      float64(measurements.V),      device.DatapointID))}但是,這會導致以下我不完全理解的錯誤:An error has occurred while serving metrics:1135 error(s) occurred:* collected metric "my_temperature_celsius" { label:<name:"id" value:"123456" > gauge:<value:14.999993896484398 > timestamp_ms:1589236877000000 } was collected before with the same name and label values* collected metric "my_temperature_celsius" { label:<name:"id" value:"123456" > gauge:<value:14.700006103515648 > timestamp_ms:1589237477000000 } was collected before with the same name and label values[..]我了解指標和標簽組合必須是唯一的,但由于我還添加了時間戳,這不算作唯一指標嗎?我的期望甚至可能嗎?如何在 Prometheus 導出器中表示這些測量值?
查看完整描述

2 回答

?
aluckdog

TA貢獻1847條經驗 獲得超7個贊

來自普羅米修斯的參考

A gauge is a metric that represents a single numerical value that can arbitrarily go up and down.
A histogram samples observations (usually things like request durations or response sizes) and counts them in configurable buckets.

Gauge用于我們關心的一個值,不關心時間戳。像當前溫度,而不是前一天的溫度。

Gauge不是您要查找的指標類型。或者,prometheus 可能不是您想要的。

當我們想要監控溫度時,我們使用histogram. 您可以在短時間內計算平均溫度、最低溫度或最高溫度。但是,當你想使用自己的時間戳時,你需要自己實現一個直方圖收集器。您可以從prometheus/client_golang/histogram.go檢查文件。一點都不簡單。

你真正需要的是 A time series database,比如 influxdb。您可以將數據推送到接受自定義時間戳的 influxdb 中,就像將 json 發布到 http 一樣簡單,然后使用grafana.

希望對您有所幫助。


查看完整回答
反對 回復 2022-06-06
?
慕沐林林

TA貢獻2016條經驗 獲得超9個贊

如果仔細觀察,您會發現 JSON 數據格式在度量收集的上下文中略有冗余,因為時間戳在每個設備內部,而不是作為父鍵并且具有作為設備 ID 和值數組的值。只有這樣你才能循環實時序列數據,然后你的標簽不會像現在一樣在循環中是靜態的。標簽唯一性是標簽名稱 + 標簽值散列在一起。


我認為首選的方法是制作一個量規向量。用于WithLabelValues獲取Gauge對象并調用Set它來設置值


deviceTempGaugeVector := prometheus.NewGaugeVec(

    prometheus.GaugeOpts{

        Name: "my_temperature_celsius",

    },

    []string{

        "device_id" // Using single label instead of 2 labels "id" and "value"

    },

)


prometheus.MustRegister(deviceTempGaugeVector)


for _, point := range dp.TimeStamps {

  for _, measurements := range point {

    deviceId := measurements.DatapointID

    value := measurements.V

    metric := deviceTempGaugeVector.WithLabelValues(deviceId).Set(value)

    ch <- prometheus.NewMetricWithTimestamp(time.Unix(measurements.T, 0),metric)

  }

}

參考:https ://godoc.org/github.com/prometheus/client_golang/prometheus#NewGaugeVec


查看完整回答
反對 回復 2022-06-06
  • 2 回答
  • 0 關注
  • 432 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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