diff options
Diffstat (limited to 'cns-libs/cnslibs/common/utils.py')
-rw-r--r-- | cns-libs/cnslibs/common/utils.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/cns-libs/cnslibs/common/utils.py b/cns-libs/cnslibs/common/utils.py index 7d1f6d6f..9aa38ff9 100644 --- a/cns-libs/cnslibs/common/utils.py +++ b/cns-libs/cnslibs/common/utils.py @@ -10,6 +10,7 @@ import string from glusto.core import Glusto as g +from prometheus_client.parser import text_string_to_metric_families ONE_GB_BYTES = 1073741824.0 @@ -49,3 +50,28 @@ def get_device_size(host, device_name): def get_random_str(size=14): chars = string.ascii_lowercase + string.digits return ''.join(random.choice(chars) for _ in range(size)) + + +def parse_prometheus_data(text): + """Parse prometheus-formatted text to the python objects + + Args: + text (str): prometheus-formatted data + + Returns: + dict: parsed data as python dictionary + """ + metrics = {} + for family in text_string_to_metric_families(text): + for sample in family.samples: + key, data, val = (sample.name, sample.labels, sample.value) + if data.keys(): + data['value'] = val + if key in metrics.keys(): + metrics[key].append(data) + else: + metrics[key] = [data] + else: + metrics[key] = val + + return metrics |