From 60cb3d6af47cd4267ad1347aa9c4fc20e5d541ca Mon Sep 17 00:00:00 2001 From: Braedon Vickers Date: Sat, 19 Nov 2016 17:08:45 +1300 Subject: [PATCH] Convert numeric bucket keys to strings so we can construct label values --- prometheus_es_exporter/parser.py | 2 +- setup.py | 2 +- tests/test_parser.py | 77 ++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 2 deletions(-) diff --git a/prometheus_es_exporter/parser.py b/prometheus_es_exporter/parser.py index e95c250..af153fe 100644 --- a/prometheus_es_exporter/parser.py +++ b/prometheus_es_exporter/parser.py @@ -5,7 +5,7 @@ def parse_buckets(buckets, metric=[], labels={}): labels_next = labels.copy() if 'key' in bucket.keys(): - bucket_key = bucket['key'] + bucket_key = str(bucket['key']) if 'bucket' in labels_next.keys(): labels_next['bucket'] = labels_next['bucket'] + [bucket_key] else: diff --git a/setup.py b/setup.py index a747656..58f9efe 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name='prometheus-es-exporter', - version='0.1.2', + version='0.1.3', description='Elasticsearch query Prometheus exporter', url='https://github.com/Braedon/prometheus-es-exporter', author='Braedon Vickers', diff --git a/tests/test_parser.py b/tests/test_parser.py index 63231b9..c153cc6 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -516,6 +516,83 @@ def test_terms(self): result = convert_result(parse_response(response)) self.assertEqual(result, expected) + def test_terms_numeric(self): + # Query: + # { + # 'size': 0, + # 'query': { + # 'match_all': {} + # }, + # 'aggs': { + # 'val_terms': { + # 'terms': {'field': 'val'}, + # 'aggs': { + # 'val_sum': { + # 'sum': {'field': 'val'} + # } + # } + # } + # } + # } + response = { + "_shards" : { + "total" : 5, + "successful" : 5, + "failed" : 0 + }, + "aggregations" : { + "val_terms" : { + "doc_count_error_upper_bound" : 0, + "sum_other_doc_count" : 0, + "buckets" : [ + { + "key" : 1, + "doc_count" : 1, + "val_sum" : { + "value" : 1.0 + } + }, + { + "key" : 2, + "doc_count" : 1, + "val_sum" : { + "value" : 2.0 + } + }, + { + "key" : 3, + "doc_count" : 1, + "val_sum" : { + "value" : 3.0 + } + } + ] + } + }, + "hits" : { + "total" : 3, + "max_score" : 0.0, + "hits" : [] + }, + "timed_out" : False, + "took" : 4 + } + + + expected = { + 'hits': 3, + 'val_terms_doc_count_error_upper_bound': 0, + 'val_terms_sum_other_doc_count': 0, + 'val_terms_doc_count{bucket="1"}': 1, + 'val_terms_val_sum_value{bucket="1"}': 1.0, + 'val_terms_doc_count{bucket="2"}': 1, + 'val_terms_val_sum_value{bucket="2"}': 2.0, + 'val_terms_doc_count{bucket="3"}': 1, + 'val_terms_val_sum_value{bucket="3"}': 3.0 + } + result = convert_result(parse_response(response)) + self.assertEqual(result, expected) + def test_nested_terms(self): # Query: # {