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

ValueError: Invalid metric name with '/' #51

Closed xaxeu closed 1 year ago

xaxeu commented 1 year ago

Describe the bug The following message:

PowerPUMP/measures/data {"app":"ESPURNA","version":"1.14.1","board":"ITEAD_SONOFF_POW_R2","host":"PowerPUMP","desc":"Powerpump","ssid":"iot","ip":"192.168.6.3","mac":"CC:50:E3:15:B7:38","rssi":-76,"uptime":4568436,"datetime":"2023-04-02 09:11:44","freeheap":18288,"relay/0":1,"vcc":3196,"loadavg":4,"time":"2023-04-02 09:11:44","id":91204}

provides following exception: ValueError: Invalid metric name: mqtt_relay/0

Expected behavior It should allow '/' on the attribute key

kpetremann commented 1 year ago

hello @xaxeu,

Thanks for opening this issue.

This is because / is not authorized in Prometheus metrics as explained in their documentation.

I can add a replace of / to _.

Would it be ok for you?

xaxeu commented 1 year ago

That would be perfect.

Thank you

A domingo, 2/04/2023, 16:48, Kevin Petremann @.***> escreveu:

hello @xaxeu https://github.com/xaxeu,

This is because / is not authorized in Prometheus metrics as explained in their documentation https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels.

I can add a replace of / to _.

Would it be ok for you?

— Reply to this email directly, view it on GitHub https://github.com/kpetremann/mqtt-exporter/issues/51#issuecomment-1493376607, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABJCHCLSMH33HAVXPYBC6P3W7GNTLANCNFSM6AAAAAAWQF3FGU . You are receiving this because you were mentioned.Message ID: @.***>

kpetremann commented 1 year ago

@xaxeu, could you please:

This should already work as the replacement of \ to _ is already implemented. I have added your case in unit tests, and I do see the relay/0 becoming relay_0

kpetremann commented 1 year ago

The full stacktrace would also be useful please

marcio-m-sousa-alb commented 1 year ago

Hi,

Stacktrace: Traceback (most recent call last): File "/opt/mqtt-exporter/exporter.py", line 5, in <module> main() File "/opt/mqtt-exporter/mqtt_exporter/main.py", line 305, in main client.loop_forever() File "/usr/local/lib/python3.10/site-packages/paho/mqtt/client.py", line 1756, in loop_forever rc = self._loop(timeout) File "/usr/local/lib/python3.10/site-packages/paho/mqtt/client.py", line 1164, in _loop rc = self.loop_read() File "/usr/local/lib/python3.10/site-packages/paho/mqtt/client.py", line 1556, in loop_read rc = self._packet_read() File "/usr/local/lib/python3.10/site-packages/paho/mqtt/client.py", line 2439, in _packet_read rc = self._packet_handle() File "/usr/local/lib/python3.10/site-packages/paho/mqtt/client.py", line 3033, in _packet_handle return self._handle_publish() File "/usr/local/lib/python3.10/site-packages/paho/mqtt/client.py", line 3327, in _handle_publish self._handle_on_message(message) File "/usr/local/lib/python3.10/site-packages/paho/mqtt/client.py", line 3570, in _handle_on_message on_message(self, self._userdata, message) File "/opt/mqtt-exporter/mqtt_exporter/main.py", line 260, in expose_metrics _parse_metrics(payload, topic, userdata["client_id"]) File "/opt/mqtt-exporter/mqtt_exporter/main.py", line 138, in _parse_metrics _create_prometheus_metric(prom_metric_name) File "/opt/mqtt-exporter/mqtt_exporter/main.py", line 71, in _create_prometheus_metric prom_metrics[prom_metric_name] = Gauge( File "/usr/local/lib/python3.10/site-packages/prometheus_client/metrics.py", line 365, in __init__ super().__init__( File "/usr/local/lib/python3.10/site-packages/prometheus_client/metrics.py", line 130, in __init__ raise ValueError('Invalid metric name: ' + self._name) ValueError: Invalid metric name: mqtt_relay/0

Configuration on docker-compose: ` power_pump: image: kpetrem/mqtt-exporter:latest container_name: power_pump ports:

I'm using latest tag version and I still dont' see the mqtt_relay_0 populated on Prometheus.

kpetremann commented 1 year ago

can you provide me the output of docker compose images command please? (or docker-compose images if you still have the old version of compose)

I think you are still using an old version of the image. Maybe could you try to docker compose pull?

Because, your stacktrace does not match the current code base:

File "/opt/mqtt-exporter/mqtt_exporter/main.py", line 260, in expose_metrics _parse_metrics(payload, topic, userdata["client_id"])`

Actual code at line 260: # move availability suffix added by Zigbee2MQTT from topic to payload

File "/opt/mqtt-exporter/mqtt_exporter/main.py", line 138, in _parse_metrics _create_prometheus_metric(prom_metric_name)`

Actual code at line 138: prom_metric_name = (

kpetremann commented 1 year ago

Note: / in the key is supported since this PR: #41.

xaxeu commented 1 year ago

Not sure why docker-compose up -d \ didn't update to the latest tag, but used the pull command and now I have the metric relay/0 being transformed to mqtt_relay_0 on Prometheus.

Thanks.

kpetremann commented 1 year ago

:+1:

docker-compose up -d never pulls the image. You have to do docker-compose pull before.

You are welcome.