Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions mqtt_exporter.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -455,10 +455,23 @@ def clear(self):

def update(self, label_values, value):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the same usecase exists for other Metric types too.
It would fit far better in https://github.com/fhemberger/mqtt_exporter/blob/master/mqtt_exporter.py#L254-L264
E.g. it could be another config parameter "json" which could contain a path for json that are not flat, but contain multiple layers.
e.g. if you have a resonse like:

{
    "UptimeSec": 3842454,
    "POWER1": "OFF",
    "Wifi": {
        "AP": 1,
        "Channel": 1,
        "Mode": "11n",
        "RSSI": 94,
        "Signal": -53,
    }
}

and you want to monitor the signal, you put something like Wifi.Signal in the json config to point to the right value.

child = self.metric.labels(*label_values)

# Check if value is a string that looks like a JSON object
if isinstance(value, str) and value.startswith('{') and value.endswith('}'):
try:
parsed_value = json.loads(value)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be a breaking change. I think many of the mqtt_export have some json output already, but using workarounds like I have posted in #71.
This will break the setup for me and other users. And would need a clear announcements and Version bump and better documentation. But as written above I would prefer to see such functionality triggered by an extra config parameter.

if 'value' in parsed_value:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not very flexible. I have sensors returning Json which is not a flat dict, but has some hierarchy, where this approach is not working. See example above.

value = float(parsed_value['value'])
else:
logging.warning(f"JSON object does not contain 'value' field: {value}")
except json.JSONDecodeError:
logging.warning(f"Failed to parse value as JSON: {value}")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This must not be a warning, as most metrics that were handled with mqtt_exporter until now, were handled as strings be intention.


child.set(value)
return child



class CounterWrapper():
"""
Wrapper to provide generic interface to Counter metric
Expand Down