Files
mtusi2026lab7/checks/telemetry_probe.py
T

32 lines
1.9 KiB
Python

"""Read telemetry using the operator's mTLS identity; never emit secret canaries."""
import json
import sys
import time
import urllib.request
import urllib.parse
from urllib.error import HTTPError
from tls_matrix import context
args=json.load(sys.stdin);trace_id=args['trace_id']
def get(host,port,path):
with urllib.request.urlopen('https://'+host+':'+str(port)+path,context=context('operator'),timeout=8) as r:return json.load(r)
deadline=time.monotonic()+90
while True:
try:
trace=get('tempo',8444,'/api/traces/'+trace_id)
spans=[s for b in trace.get('batches',trace.get('resourceSpans',[])) for scope in b.get('scopeSpans',b.get('instrumentationLibrarySpans',[])) for s in scope.get('spans',[])]
names={s['name'] for s in spans}
query=urllib.parse.urlencode({'query':'{service_name=~"messenger-(api|worker|dispatcher)"} |= "'+trace_id+'"','limit':1000,'start':int((time.time()-600)*1e9),'end':int(time.time()*1e9)})
logs=get('loki',8443,'/loki/api/v1/query_range?'+query)
lines=[line for stream in logs['data']['result'] for _,line in stream['values']]
if {'attachment.consume','db.attachment.commit','attachment.publish','image.render'}<=names and any('job_finished' in line for line in lines):break
except HTTPError as e:
if e.code!=404:raise
if time.monotonic()>deadline:raise AssertionError('Telemetry did not arrive over TLS')
time.sleep(2)
raw=json.dumps([trace,logs])
assert all(secret not in raw for secret in args['canaries'])
metrics=get('prometheus',8443,'/api/v1/query?'+urllib.parse.urlencode({'query':'http_server_requests_total'}))
assert metrics['status']=='success' and metrics['data']['result']
print(json.dumps({'passed':True,'trace_id':trace_id,'span_count':len(spans),'span_names':sorted(names),'correlated_log_count':len(lines),'http_metric_series':len(metrics['data']['result']),'credential_canaries_absent':True,'all_queries':'mTLS verified operator identity'},indent=2))