Skip to content

Commit c9af369

Browse files
committed
fix: correcting previous fix on handling list query
1 parent 3acc4ee commit c9af369

File tree

3 files changed

+20
-33
lines changed

3 files changed

+20
-33
lines changed

commands.txt

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11

2+
Policy keeper start:
3+
4+
source env/pk/bin/activate
5+
./policy_keeper.py --cfg configs/config.yaml --srv
6+
7+
8+
29
Policy keeper REST API:
310

411
Start a policy:
@@ -10,25 +17,4 @@ curl -X POST http://localhost:12345/policy/stop
1017
Fire an alert:
1118
curl -X POST http://localhost:12345/alerts/fire --data-binary @alert.yaml
1219

13-
alert.yaml:
14-
alerts:
15-
- annotations: {}
16-
endsAt: '0001-01-01T00:00:00Z'
17-
generatorURL: http://d41870505e27:9090/graph?g0.expr=vector%281.525268017e%2B09+-+time%28%29%29+%3C+0&g0.tab=1
18-
labels:
19-
alertname: MyAlert
20-
startsAt: '2018-05-03T07:41:01.255717752Z'
21-
status: firing
22-
commonAnnotations: {}
23-
commonLabels:
24-
alertname: time_elapsed
25-
externalURL: http://7c46054c555f:9093
26-
groupKey: '{}:{alertname="Myalert"}'
27-
groupLabels:
28-
alertname: MyAlert
29-
receiver: default
30-
status: firing
31-
version: '4'
32-
33-
----------------------
3420

configs/devel-config.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

22

3-
prometheus_endpoint: 'http://localhost'
4-
prometheus_config_template: '/tmp/original_prometheus_config.yaml'
5-
prometheus_config_target: '/tmp/prometheus_config.yaml'
6-
prometheus_rules_directory: '/var/lib/micado/prometheus/config'
3+
prometheus_endpoint: 'http://prometheus:9090/prometheus'
4+
prometheus_config_template: '/config/policykeeper/prometheus_config_template.yaml'
5+
prometheus_config_target: '/prometheus.yaml'
6+
prometheus_rules_directory: '/'
77

88
docker_node_unreachable_timeout: 120
99

handle_prometheus.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,18 @@ 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 isinstance(expression,list):
25+
log.debug('Multiple results in prometheus response for expression "{0}": "{1}"'
26+
.format(expression,str(result)))
27+
return [ x.get('metric',dict()).get(expression[1]) \
28+
for x in result if x.get('metric',dict()).get(expression[1])]
2429
if len(result)<1:
2530
raise Exception('No results found in prometheus response for expression "{0}": "{1}"'
2631
.format(expression,str(result)))
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])]
32+
if not result[0].get('value'):
33+
raise Exception('Unrecognised result in prometheus response for expression "{0}": "{1}"'
34+
.format(expression,str(result[0])))
35+
value=result[0]['value']
3536
else:
3637
value=response['data']['result']
3738
if not isinstance(value,list) or \

0 commit comments

Comments
 (0)