kpetremann / mqtt-exporter

Simple generic MQTT Prometheus exporter for IoT working out of the box
https://hub.docker.com/r/kpetrem/mqtt-exporter
MIT License
103 stars 29 forks source link

zigbee2mqtt: Device renames #68

Open bo0tzz opened 8 months ago

bo0tzz commented 8 months ago

When renaming a device in zigbee2mqtt, the old device name still continues to exist in the exported metrics until mqtt-exporter is restarted, causing 'stuck' metrics. It would be nice to handle the rename message that zigbee2mqtt sends in some way to clear the old metric.

topic: zigbee2mqtt/bridge/response/device/rename
{
  "data": {
    "from": "socket/foo",
    "homeassistant_rename": false,
    "to": "socket/bar"
  },
  "status": "ok",
  "transaction": "tt7m3-3"
}
curl http://mqtt-exporter/

...
zigbee2mqtt_zigbee_availability{topic="zigbee2mqtt_socket_foo"} 1.0
zigbee2mqtt_zigbee_availability{topic="zigbee2mqtt_socket_bar"} 1.0
...
zigbee2mqtt_state{topic="zigbee2mqtt_socket_foo"} 1.0
zigbee2mqtt_state{topic="zigbee2mqtt_socket_bar"} 1.0
...etc
kpetremann commented 8 months ago

Hello @bo0tzz,

Thanks for the suggestion.

Unfortunately, last time I checked (I'll check this again in case it changed) the Python Prometheus library does not permit to remove easily a specific metric. There is a hacky way to do it, but it would require to play with the Prometheus library internals, which is risky on the long term (if the Prometheus lib dev change the internal structures/logic).

I'll think it through.