diff options
author | robot-piglet <[email protected]> | 2025-05-14 18:03:36 +0300 |
---|---|---|
committer | robot-piglet <[email protected]> | 2025-05-14 18:14:29 +0300 |
commit | 7023fcf3f154c4c81eacca1475db7ca10293ee3b (patch) | |
tree | 9702d653cde52f40b2b3ce53fa37f492fd637cb0 /library/python/monlib | |
parent | 05895f5bdfdbfd3ed8b1d9845ab2e927ea62fb9a (diff) |
Intermediate changes
commit_hash:46bf4949f5afb2187946e54da572ae0599b516a0
Diffstat (limited to 'library/python/monlib')
-rw-r--r-- | library/python/monlib/encoder.pyx | 18 | ||||
-rw-r--r-- | library/python/monlib/ut/py2/test.py | 36 | ||||
-rw-r--r-- | library/python/monlib/ut/py3/test.py | 36 |
3 files changed, 87 insertions, 3 deletions
diff --git a/library/python/monlib/encoder.pyx b/library/python/monlib/encoder.pyx index 2e4f56e1d3c..10468b1a97d 100644 --- a/library/python/monlib/encoder.pyx +++ b/library/python/monlib/encoder.pyx @@ -79,7 +79,7 @@ cdef class Encoder: wrapper.__wrapped = EncoderJson(wrapper.__stream.Get(), indent) return wrapper - + @staticmethod cdef Encoder create_prometheus(object stream, bytes metricNameLabel): cdef Encoder wrapper = Encoder.__new__(Encoder) @@ -200,13 +200,16 @@ def dumps(registry, format='spack', **kwargs): return s -def load(fp, from_format='spack', to_format='json'): +def load(fp, from_format='spack', to_format='json', **kwargs): """ Converts metrics from one format to another. :param fp: File to load data from :param from_format: Source string format (allowed values: json, spack, unistat). Default: spack :param to_format: Target format (allowed values: json, spack). Default: json + + Keyword arguments: + :param metric_name_label: Name of the label used as the prometheus metric name :returns: a string containing metrics in the specified format """ if from_format == to_format: @@ -222,6 +225,9 @@ def load(fp, from_format='spack', to_format='json'): encoder = Encoder.create_json(None, 0) elif to_format == 'spack': encoder = Encoder.create_spack(None, SECONDS, IDENTITY) + elif to_format == 'prometheus': + metric_name_label = kwargs.get('metric_name_label', 'sensor').encode() + encoder = Encoder.create_prometheus(None, metric_name_label) else: raise ValueError('Unsupported format ' + to_format) @@ -243,13 +249,16 @@ def load(fp, from_format='spack', to_format='json'): return s -def loads(s, from_format='spack', to_format='json', compression=Compression.Identity): +def loads(s, from_format='spack', to_format='json', compression=Compression.Identity, **kwargs): """ Converts metrics from one format to another. :param s: String to load from :param from_format: Source string format (allowed values: json, spack, unistat). Default: spack :param to_format: Target format (allowed values: json, spack). Default: json + + Keyword arguments: + :param metric_name_label: Name of the label used as the prometheus metric name :returns: a string containing metrics in the specified format """ if from_format == to_format: @@ -265,6 +274,9 @@ def loads(s, from_format='spack', to_format='json', compression=Compression.Iden elif to_format == 'spack': comp = Compression.to_native(compression) encoder = Encoder.create_spack(None, SECONDS, comp) + elif to_format == 'prometheus': + metric_name_label = kwargs.get('metric_name_label', 'sensor').encode() + encoder = Encoder.create_prometheus(None, metric_name_label) else: raise ValueError('Unsupported format ' + to_format) diff --git a/library/python/monlib/ut/py2/test.py b/library/python/monlib/ut/py2/test.py index c845b043539..95647cf631e 100644 --- a/library/python/monlib/ut/py2/test.py +++ b/library/python/monlib/ut/py2/test.py @@ -460,3 +460,39 @@ def test_mem_only_metrics(): bucket_count=5, base=2 ) + + +def test_prometheus_conversion(request): + JSON_METRICS = """{ + "sensors": + [ + { + "kind":"GAUGE", + "labels": + { + "name":"gauge-metric" + }, + "value":10 + }, + { + "kind":"RATE", + "labels": + { + "name":"rate-metric" + }, + "value":20 + } + ] + }""" + EXPECTED = "\n".join( + [ + "# TYPE gauge_metric gauge", + "gauge_metric 10", + "# TYPE rate_metric counter", + "rate_metric 20", + "", + "", + ] + ).encode('utf-8') + result = loads(JSON_METRICS, from_format='json', to_format='prometheus', metric_name_label='name') + assert result == EXPECTED diff --git a/library/python/monlib/ut/py3/test.py b/library/python/monlib/ut/py3/test.py index 75cd0494cdb..4017509bc7e 100644 --- a/library/python/monlib/ut/py3/test.py +++ b/library/python/monlib/ut/py3/test.py @@ -458,3 +458,39 @@ def test_mem_only_metrics(): bucket_count=5, base=2 ) + + +def test_prometheus_conversion(request): + JSON_METRICS = """{ + "sensors": + [ + { + "kind":"GAUGE", + "labels": + { + "name":"gauge-metric" + }, + "value":10 + }, + { + "kind":"RATE", + "labels": + { + "name":"rate-metric" + }, + "value":20 + } + ] + }""" + EXPECTED = "\n".join( + [ + "# TYPE gauge_metric gauge", + "gauge_metric 10", + "# TYPE rate_metric counter", + "rate_metric 20", + "", + "", + ] + ).encode('utf-8') + result = loads(JSON_METRICS, from_format='json', to_format='prometheus', metric_name_label='name') + assert result == EXPECTED |