#!/usr/bin/env python3 """Prove the SLI also observes a 503 generated by HAProxy, outside API SDKs.""" import json,time,urllib.request,urllib.error from observability import ROOT,BASE,Client,check,dc,scalar,wait_for c=Client(BASE);a,b,e,chat=c.bootstrap();before=scalar('sum(haproxy_backend_http_responses_total{proxy="replicas",code="5xx"})') or 0 try: dc('stop','api');time.sleep(2) req=urllib.request.Request(BASE+'/api/v1/users/me',headers={'Authorization':'Bearer '+a['token']}) try:response=urllib.request.urlopen(req,timeout=4) except urllib.error.HTTPError as error:response=error with response:check(response.code==503,'expected LB 503');status=response.code after,delay=wait_for(lambda: (lambda n:n if n is not None and n>before else None)(scalar('sum(haproxy_backend_http_responses_total{proxy="replicas",code="5xx"})')),15) result={'client_status':status,'api_processes_stopped':True,'ingress_5xx_before':before,'ingress_5xx_after':after,'observation_seconds':delay,'lb_generated_error_counted':True} (ROOT/'evidence/ingress-guard.json').write_text(json.dumps(result,indent=2)+'\n');print('PASS: LB-generated 503 included in business ingress SLI') finally:dc('up','-d','--no-deps','--wait','api') c.call('GET','/api/v1/users/me',actor=a,schema='User')