Files
mtusi2026lab6/student-tests/sampling_retention.py
T

39 lines
1.6 KiB
Python

"""Separate synchronized rare-error/slow-success retention experiment."""
import json
from pathlib import Path
from observability_cost import set_mode, probes
from observability import save, trace, wait
import urllib.error
def verify_presence(rows):
for row in rows:
if row["trace_sampled"]:
data=wait(lambda:trace(row["trace_id"]),timeout=90)
assert data.get("batches",data.get("resourceSpans"))
row["tempo_trace_found"]=True
else:
try:
trace(row["trace_id"])
except urllib.error.HTTPError as error:
assert error.code==404
row["tempo_trace_found"]=False
else:
raise AssertionError("Unsampled trace unexpectedly stored")
return rows
def main():
result={'method':'Three repeats per full/10%/25% setting; each holds sessions lock until pg_locks confirms it. 3x503 plus one slow200 per repeat. Sampling is ordinary SDK head sampling without forced traceparent.','runs':[]}
try:
for mode,ratio in [('full',1),('sample10',.1),('sample25',.25)]:
c=set_mode(1,ratio)
actor=c.user('retention')
for repeat in range(3):
result['runs'].append({'mode':mode,'repeat':repeat,'probes':verify_presence(probes(c,actor,mode))})
save('sampling-retention.json',result)
print(mode,repeat,'retention recorded',flush=True)
finally:set_mode(1,.25)
print('PASS: synchronized rare 503/slow200 retention study; balanced sampling restored.')
if __name__=='__main__':main()