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
108 stars 30 forks source link

Fix: Don't crash when not able to decode payload. #8

Closed rin closed 3 years ago

rin commented 3 years ago

Disclaimer: These are the first three lines of Python I've ever written.

I encountered a UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte when trying to use this exporter with data from my Xiaomi Roborock vacuum. It's probably the map_data which I don't care about anyway, so this was my fix.

Screenshot 2021-02-13 at 14 34 48
kpetremann commented 3 years ago

Hi @rin, thanks for the PR.

I am ok with the fix in your PR but not with the implementation you did of topic exclusion feature (also it should be in a different PR).

IGNORED_TOPIC should be a list (using a comma separator) and the default value should be empty, not map_data. You would just have to cast the varenv and then you would just have to check like this:

IGNORED_TOPICS = os.getenv("MQTT_IGNORED_TOPICS", "").split(",")

if msg.topic not in IGNORED_TOPICS:
        LOG.debug('Topic "%s" was ignored', msg.topic)
        return

The defined varenv would look like this: IGNORED_TOPICS = "topic1,topic2,topic3"

rin commented 3 years ago

Replaced by https://github.com/kpetremann/mqtt-exporter/pull/9