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

support client_id #24

Closed runningman84 closed 2 years ago

runningman84 commented 2 years ago

it would be helpful to see the client_id...

The underlying python module seems to support that.

// The client() object creates an MQTT client. It takes 4 parameters which are optional:

client(client_id="", clean_session=True, userdata=None, protocol=MQTTv311, transport="tcp")
kpetremann commented 2 years ago

Sure, I'll have a look

kpetremann commented 2 years ago

It seems by default there is no client_id generated. See here

        if client_id == "" or client_id is None:
            if protocol == MQTTv31:
                self._client_id = base62(uuid.uuid4().int, padding=22)
            else:
                self._client_id = b""

Also, I did not find a clean way to get the client id, as it is a private attribute of client object.

Do you need to have the client id auto generated, or do you want to specify it manually?

runningman84 commented 2 years ago

I would specify it manually using an ENV variable... if the variable is not present the client_id can be auto generated

kpetremann commented 2 years ago

ok, I'll add the possibility to specify the client id, and I'll expose it in the metrics if existing.

If the variable is not present the client_id can be auto generated

According the documentation, when client-id is empty, this is up to the MQTT broker to generate it. But the broker does not seem to send back the client-id to the client. In debug mode, client._client_id remains empty even during message processing.

If I force the client-id auto generation on client side, I would have to handle all possible issues such as client id already used.

runningman84 commented 2 years ago

Yes just leave the id empty and let the server auto generate something...

kpetremann commented 2 years ago

But the broker does not seem to send back the client-id to the client.

Confirmed here: https://github.com/eclipse/paho.mqtt.python/issues/237#issuecomment-336069672 :

No, if the server generate the clientid then it is not send to client.`

It looks like the behavior is fixed in MQTT v5: https://github.com/eclipse/paho.mqtt.python/issues/237#issuecomment-885269826 But default protocol in python MQTT client library is MQTT3.1.1.

I'll try with an option to choose the MQTT 5 version. If it is working, we will be able to get back the id generated by the broker and to expose it in Prometheus.

kpetremann commented 2 years ago

would it work for you? https://github.com/kpetremann/mqtt-exporter/pull/26. Feel free to test.

I still need to add some functional tests before merging it.

runningman84 commented 2 years ago

looks good

kpetremann commented 2 years ago

Code merged