refactor code
This commit is contained in:
@@ -22,12 +22,14 @@ def prom(path, query=None):
|
||||
if query is not None: url += '?' + urllib.parse.urlencode({'query': query})
|
||||
with urllib.request.urlopen(urllib.request.Request(url, headers={'Authorization': auth}), timeout=5) as r: return json.load(r)['data']
|
||||
def metric(name):
|
||||
result = prom('query', name + '{service_name="messenger-outbox"}')['result']
|
||||
result = prom('query', 'max(' + name + '{service_name="messenger-outbox"})')['result']
|
||||
return float(result[0]['value'][1]) if result else None
|
||||
def sample(phase):
|
||||
alerts = prom('alerts')['alerts']
|
||||
return {'time': utc(), 'monotonic': time.monotonic(), 'phase': phase,
|
||||
'firing': sorted(a['labels']['alertname'] for a in alerts if a['state']=='firing' and a['labels']['alertname'].startswith('MessengerReadyBurn')),
|
||||
'all_firing': sorted(a['labels']['alertname'] for a in alerts if a['state']=='firing'),
|
||||
'queue_depth': metric('messenger_queue_depth'), 'queue_age': metric('messenger_queue_age_seconds'),
|
||||
'eligible': metric('messenger_slo_ready_eligible_total'), 'bad': metric('messenger_slo_ready_bad_total')}
|
||||
def docker(*args): subprocess.run(['docker','compose',*args], cwd=ROOT, check=True, stdout=subprocess.DEVNULL)
|
||||
def run():
|
||||
@@ -43,13 +45,15 @@ def run():
|
||||
for _ in range(8): accepted.append(client.attachment(actor,chat)[0])
|
||||
out['accepted_at']={'time':utc(),'monotonic':time.monotonic()}
|
||||
print('8 synthetic jobs accepted; waiting for burn alert', flush=True)
|
||||
seen=set()
|
||||
deadline=time.monotonic()+100
|
||||
while time.monotonic()<deadline:
|
||||
s=sample('worker_stopped'); samples.append(s)
|
||||
if 'MessengerReadyBurnFast' in s['firing']: break
|
||||
seen.update(s['all_firing'])
|
||||
if {'MessengerReadyBurnFast','MessengerQueueLag'} <= seen: break
|
||||
time.sleep(5)
|
||||
else: raise RuntimeError('Ready fast burn did not fire in 100 seconds')
|
||||
docker('start','worker'); worker_stopped=False
|
||||
else: raise RuntimeError('Both ready fast burn and QueueLag were not observed in 100 seconds')
|
||||
docker('up','-d','--no-deps','worker'); worker_stopped=False
|
||||
out['worker_started']={'time':utc(),'monotonic':time.monotonic()}
|
||||
for att in accepted: client.wait_ready(actor,att,wait_seconds=90)
|
||||
out['all_ready']={'time':utc(),'monotonic':time.monotonic()}
|
||||
@@ -57,13 +61,13 @@ def run():
|
||||
deadline=time.monotonic()+100
|
||||
while time.monotonic()<deadline:
|
||||
s=sample('recovery');samples.append(s)
|
||||
if not s['firing']: break
|
||||
if not s['firing'] and 'MessengerQueueLag' not in s['all_firing']: break
|
||||
time.sleep(5)
|
||||
else: raise RuntimeError('Ready burn alert did not clear in 100 seconds')
|
||||
out['result']='PASS'
|
||||
finally:
|
||||
if worker_stopped:
|
||||
try: docker('start','worker')
|
||||
try: docker('up','-d','--no-deps','worker')
|
||||
except Exception: out['worker_restore_failed']=True
|
||||
(ROOT/'evidence/slo-ready-incident.json').write_text(json.dumps(out,ensure_ascii=False,indent=2)+'\n')
|
||||
print('PASS: ready SLO incident; evidence/slo-ready-incident.json',flush=True)
|
||||
|
||||
Reference in New Issue
Block a user