我有一个设备,它以以下格式将温度测量以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:<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
[..]
发布于 2020-05-30 10:24:39
普罗米修斯参考文献
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
。您可以在短时间内计算平均温度、最低温度或最大温度。但是,当您想使用自己的时间戳时,您需要自己实现一个直方图收集器。您可以从戈朗/直方图检查该文件。一点也不简单。
您真正需要的是一个time series database
,就像进水数据库一样。您可以将数据推入接收自定义时间戳(如将json发送到http )的进水数据库中,然后使用grafana
监视数据。
希望这能帮到你。
发布于 2020-05-26 06:07:38
如果仔细观察,您将看到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)
}
}
参考文献:戈朗/普罗米修斯#NewGaugeVec
https://stackoverflow.com/questions/61900346
复制相似问题