-
Notifications
You must be signed in to change notification settings - Fork 14
python modified to be able to handle Json metrics def gauge() #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -455,10 +455,23 @@ def clear(self): | |
|
||
def update(self, label_values, 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
if 'value' in parsed_value: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
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:
and you want to monitor the signal, you put something like
Wifi.Signal
in thejson
config to point to the right value.