Skip to content

Commit 3acc4ee

Browse files
author
smith4
committed
fix: return list when only one element in vector of query result
1 parent fb99fda commit 3acc4ee

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

handle_prometheus.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,17 @@ def extract_value_from_prometheus_response(expression,response,filterdict=dict()
2121
if response['data']['resultType']=='vector':
2222
result = [ x for x in response['data']['result']
2323
if x.get('metric',None) is not None and is_subdict(filterdict,x['metric']) ]
24-
if len(result)>1:
25-
log.debug('Multiple results in prometheus response for expression "{0}": "{1}"'
26-
.format(expression,str(result)))
27-
if not isinstance(expression,list):
28-
raise Exception('Multiple results in prometheus response for expression "{0}": "{1}"'
29-
.format(expression,str(result)))
30-
else:
31-
return [ x.get('metric',dict()).get(expression[1]) \
32-
for x in result if x.get('metric',dict()).get(expression[1])]
3324
if len(result)<1:
3425
raise Exception('No results found in prometheus response for expression "{0}": "{1}"'
3526
.format(expression,str(result)))
36-
if not result[0].get('value'):
37-
raise Exception('Unrecognised result in prometheus response for expression "{0}": "{1}"'
38-
.format(expression,str(result[0])))
39-
value=result[0]['value']
27+
log.debug('Multiple results in prometheus response for expression "{0}": "{1}"'
28+
.format(expression,str(result)))
29+
if not isinstance(expression,list):
30+
raise Exception('Multiple results in prometheus response for expression "{0}": "{1}"'
31+
.format(expression,str(result)))
32+
else:
33+
return [ x.get('metric',dict()).get(expression[1]) \
34+
for x in result if x.get('metric',dict()).get(expression[1])]
4035
else:
4136
value=response['data']['result']
4237
if not isinstance(value,list) or \

0 commit comments

Comments
 (0)