Skip to content

Conversation

leomargontier
Copy link

No description provided.

# 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.

def clear(self):
self.metric.clear()

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.

if isinstance(value, str) and value.startswith('{') and value.endswith('}'):
try:
parsed_value = json.loads(value)
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.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants