I have a device that is exposing temperature measurements as a JSON in the following format:
[
{
"dataPointId": 123456,
"values": [
{
"t": 1589236277000,
"v": 14.999993896484398
},
{
"t": 1589236877000,
"v": 14.700006103515648
},
{
"t": 1589237477000,
"v": 14.999993896484398
},
[..]
As you can see, the values contain both a timestamp and the temperature measurement. I would like to expose these measurements via Prometheus metrics, so I am using prometheus/client_golang to build an exporter.
My expectation would be that the /metrics endpoint then exposes something like this from the data above:
# 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
I implemented a simple prometheus.Collector and I am adding my static metrics without any issues. For the measurements above, NewMetricWithTimestamp seems to be the only way to add metrics with a timestamp, so I am iterating over these values using something like this:
for _, measurements := range dp.Values {
ch <- prometheus.NewMetricWithTimestamp(
time.Unix(measurements.T, 0),
prometheus.MustNewConstMetric(
collector.temperature,
prometheus.GaugeValue,
float64(measurements.V),
device.DatapointID))
}
However, this leads to the following error that I do not fully understand:
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
[..]
I understand that the metric and label combination must be unique, but as I am also adding a timestamp, does that not count as a unique metric? Is my expectation above even possible?
How can I represent these measurements in a Prometheus exporter?