kpetremann/mqtt-exporter

Optional `/` character replacement in topic

Opened this issue · 2 comments

Describe the solution you'd like
I'd like to make the replacement of the / character in the topic label optional:

# parse MQTT topic
try:
# handle nested topic
topic = topic.replace("/", "_")

Is there a functional reason why you always replace the / chars from the topic label? I have a use-case where I need to join time series that share at least a label with the same value as a given MQTT topic, and this makes it difficult to do it without additional post-processing steps. Also, looking at other MQTT exporters in GitHub (for example here), I don't see a problem keeping the / character in the topic value.

Could we add a boolean condition here to avoid replacing the / character? Something like this to ensure backwards compatibility:

    # parse MQTT topic
    try:
        # handle nested topic
        if not settings.TOPIC_KEEP_ORIGINAL:
            topic = topic.replace("/", "_")
    except UnicodeDecodeError:
        LOG.debug('encountered undecodable topic: "%s"', raw_topic)
        return None, None

Thanks!

Additional context

This is a use-case I'm working on. 2 time series coming from different sources shall ultimately join by the topic label:

  • Application A exports its metrics to Prometheus through a JSON exporter --> Metrics contain a label called topic with value /my-topic-name
    my_json_metric{topic="/my-topic-name"} 12
    
  • Application B exports its metrics to Prometheus through your MQTT exporter --> These metrics also contain a topic label, but its original configured value of /my-topic-name is replaced by your exporter to _my-topic-name:
    my_mqtt_metric{topic="_my-topic-name"} 12
    
  • This makes hard to join both tables by the same topic label

Hi,
I don't think there is any functional reason. It is like this since the first version, it was just for aesthetic reason.
Feel free to do the PR, I'll be happy to review it.

Hi, I don't think there is any functional reason. It is like this since the first version, it was just for aesthetic reason. Feel free to do the PR, I'll be happy to review it.

Good to know! I'll try to work on it ASAP, thanks!