Files
mtusi2026lab7/scripts/signals.py

33 lines
1.6 KiB
Python

import json
import subprocess
import time
from urllib.parse import urlencode
def query(port,path,**params):
url="http://127.0.0.1:"+str(port)+path+"?"+urlencode(params)
output=subprocess.check_output(["docker","compose","exec","-T","telemetry","curl","-fsS",url])
return json.loads(output)
for service in ("api","worker"):
for attempt in range(24):
try:
metrics=query(9090,"/api/v1/query",query="sum(messenger_"+service+"_operations_total)")["data"]["result"]
traces=query(3200,"/api/search",tags="service.name=messenger-"+service,limit=5)["traces"]
logs=query(3100,"/loki/api/v1/query_range",query='{service_name="messenger-'+service+'"}',limit=30)["data"]["result"]
log_ids={v[2].get("trace_id") for stream in logs for v in stream["values"] if len(v)>2}
matching=next((t["traceID"] for t in traces if t["traceID"] in log_ids),None)
if not matching:
for t in traces:
found=query(3100,"/loki/api/v1/query_range",query='{service_name="messenger-'+service+'"} | trace_id="'+t["traceID"]+'"',limit=1)["data"]["result"]
if found:
matching=t["traceID"]
break
if metrics and matching:
print(service,"operations",metrics[0]["value"][1],"trace with correlated log",matching,flush=True)
break
except (KeyError,ValueError,subprocess.CalledProcessError):
pass
time.sleep(5)
else:
raise SystemExit("Missing correlated signals for "+service)