Files
mtusi2026lab6/student-tests/telemetry_persistence.py

37 lines
2.1 KiB
Python

"""Recreate storage containers with the same volumes; verify historical signals."""
import json
import time
from common import *
def main():
evidence=json.loads((ROOT/'evidence/signals.json').read_text(encoding='utf-8'))
tid=evidence['trace_id']
stamps=[int(v[0]) for stream in evidence['logs']['data']['result'] for v in stream['values']]
params={'query':'{service_name=~"messenger-api|messenger-worker|messenger-dispatcher"} |= "'+tid+'"',
'start':str(min(stamps)-10**9),'end':str(max(stamps)+10**9),'limit':100}
historical_time=time.time()-10
def observe():
_,spans=trace_wait(tid)
logs=query('loki','/loki/api/v1/query_range',**params)
metrics=query('prometheus','/api/v1/query',query='messenger_http_requests_total',time=historical_time)
return {'span_ids':sorted(s['id'] for s in spans),
'logs':sorted(v for stream in logs['data']['result'] for v in stream['values']),
'metrics':sorted(metrics['data']['result'],key=lambda row:json.dumps(row['metric'],sort_keys=True))}
before=observe()
check(before['logs'] and before['metrics'] and before['span_ids'],'historical signals absent before test')
containers=snapshot()
dc('up','-d','--no-deps','--no-build','--force-recreate','prometheus','loki','tempo','grafana')
subprocess.check_call([sys.executable,'scripts/wait_ready.py','--project',PROJECT,'--base-url',BASE,'--telemetry'],cwd=ROOT)
after=observe()
result={'before_containers':containers,'after_containers':snapshot(),'trace_id':tid,
'historical_query_time':historical_time,'span_count':len(before['span_ids']),'log_count':len(before['logs']),
'metric_series':len(before['metrics']),'same_spans':before['span_ids']==after['span_ids'],
'same_logs':before['logs']==after['logs'],'same_metrics':before['metrics']==after['metrics']}
save('telemetry-persistence.json',result)
check(result['same_spans'] and result['same_logs'] and result['same_metrics'],'historical signals differ after recreate')
print('PASS: historical traces, logs and metrics survive storage container recreation with volumes')
if __name__=='__main__':main()