vladak / shield

code for ESP32 Adafruit Feather to get various sensor readings and publish them to MQTT broker
2 stars 0 forks source link

configure Prometheus mqtt exporter to use unique sensor names #20

Closed vladak closed 9 months ago

vladak commented 9 months ago

When investigating why there are gaps in the temperature/CO2 graphs for certain devices, I arrived at the metrics not being presented by the Prometheus MQTT exporter contiguously. When querying the Prometheus HTTP endpoint of the MQTT exporter, given metric is present in one response, only to be gone in the next response, even though based on watching the topic confirms that the metric is published frequently, like every couple of seconds. For example, checking the metrics sent to the devices/pracovna/qtpy topic multiple times, I see this on two back-to-back curl invocations:

pi@weather:~ $ curl -s http://localhost:9641/metrics | grep ^temper
temperature{sensor="esp32",topic="devices/fusebox/esp32"} 28.4 1704397645155
temperature{sensor="qtpy",topic="devices/pracovna/qtpy"} 26.8 1704397642538
temperature{sensor="shield",topic="devices/terasa/shield"} 6.2 1704397364985
pi@weather:~ $ curl -s http://localhost:9641/metrics | grep ^temper
temperature{sensor="esp32",topic="devices/fusebox/esp32"} 28.4 1704397645155
temperature{sensor="qtpy",topic="devices/loznice/qtpy"} 19.6 1704397647789
temperature{sensor="shield",topic="devices/terasa/shield"} 6.2 1704397364985

The second line of the output got changed to different topic/metric but sharing the sensor value.

The pattern to extract the device ID in the configuration was "(.*/)?(?P<deviceid>.*)".

What seems to be happening is that the temperature metric is aggregated to the sensor key and given the last component of the topic is the same (qtpy, based on device type), the metric is switched based on the last received MQTT message that maps to the sensor key.

The device ID needs to be made unique.

vladak commented 9 months ago

"devices/(?P<deviceid>.*)" seems to do the job (as long as there are not multiple devices in the same location).