Skip to content

Commit

Permalink
Convert numeric bucket keys to strings so we can construct label values
Browse files Browse the repository at this point in the history
  • Loading branch information
braedon committed Nov 19, 2016
1 parent 5a168e9 commit 60cb3d6
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
2 changes: 1 addition & 1 deletion prometheus_es_exporter/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
77 changes: 77 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
# {
Expand Down

0 comments on commit 60cb3d6

Please sign in to comment.