普罗米修斯收集器失败,并显示“收集的指标之前已收集,具有相同的名称和标签值”

Sim*_* K. 5 go prometheus

我有一个设备以以下格式将温度测量结果作为 JSON 公开:

[
  {
    "dataPointId": 123456,
    "values": [
      {
        "t": 1589236277000,
        "v": 14.999993896484398
      },
      {
        "t": 1589236877000,
        "v": 14.700006103515648
      },
      {
        "t": 1589237477000,
        "v": 14.999993896484398
      },
[..]
Run Code Online (Sandbox Code Playgroud)

如您所见,这些值包含时间戳和温度测量值。我想通过 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
Run Code Online (Sandbox Code Playgroud)

我实现了一个简单的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))
}
Run Code Online (Sandbox Code Playgroud)

但是,这会导致我不完全理解的以下错误:

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
[..]
Run Code Online (Sandbox Code Playgroud)
  • 我知道指标标签组合必须是唯一的,但由于我还添加了时间戳,这不算是唯一指标吗?我的期望甚至可能吗?

  • 如何在 Prometheus 导出器中表示这些测量值?

Bil*_*uan 3

参考普罗米修斯

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. 
Run Code Online (Sandbox Code Playgroud)

Gauge用于我们关心的一个值,不关心时间戳。例如当前温度,而不是前一天的温度。

Gauge不是您正在寻找的指标类型。或者,普罗米修斯可能不是您正在寻找的。

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

你真正需要的是 A time series database,比如 influxdb 。您可以将数据推送到接受自定义时间戳的 influxdb 中,就像将 json 发布到 http 一样简单,然后使用grafana.

希望这对你有帮助。