登录
首页 >  Golang >  Go问答

Prometheus Collector 失败,因为已存在具有相同名称和标签值的指标

来源:stackoverflow

时间:2024-02-29 13:45:24 227浏览 收藏

小伙伴们有没有觉得学习Golang很有意思?有意思就对了!今天就给大家带来《Prometheus Collector 失败,因为已存在具有相同名称和标签值的指标》,以下内容将会涉及到,若是在学习中对其中部分知识点有疑问,或许看了本文就能帮到你!

问题内容

我有一个设备,它以以下格式将温度测量结果公开为 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 gauge
my_temperature_celsius{id="123456"} 14.999993896484398 1589236277000
my_temperature_celsius{id="123456"} 14.700006103515648 1589236877000
my_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: gauge: timestamp_ms:1589236877000000 } was collected before with the same name and label values
* collected metric "my_temperature_celsius" { label: gauge: timestamp_ms:1589237477000000 } was collected before with the same name and label values
[..]
  • 我知道指标标签组合必须是唯一的,但由于我还添加了时间戳,所以这不算是唯一指标吗?我的上述期望是否可能实现?

  • 如何在 prometheus 导出器中表示这些测量结果?


解决方案


引用自 Prometheus

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 不是您正在寻找的指标类型。或者,普罗米修斯可能不是您正在寻找的。

当我们想要监控温度时,我们使用histogram。您可以在短时间内计算平均温度、最低温度或最高温度。但是,当您想使用自己的时间戳时,您需要自己实现直方图收集器。你可以查看prometheus/client_golang/histogram.go的文件。一点也不简单。

你真正需要的是一个时间系列数据库,比如influxdb。您可以将数据推送到接受自定义时间戳的 influxdb,就像将 json 发布到 http 一样简单,然后使用 grafana 监视数据。

希望这会对您有所帮助。

如果您仔细观察,您会发现 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

终于介绍完啦!小伙伴们,这篇关于《Prometheus Collector 失败,因为已存在具有相同名称和标签值的指标》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>