Files
mtusi2026lab7/scripts/verify_telemetry.py
T
2026-11-28 12:29:50 +01:00

54 lines
2.6 KiB
Python

#!/usr/bin/env python3
"""Verify that all three telemetry signals are really stored and queryable."""
import base64
import json
import time
import urllib.parse
import urllib.request
def get(url, headers=None):
req = urllib.request.Request(url, headers=headers or {})
with urllib.request.urlopen(req, timeout=15) as response:
return json.loads(response.read().decode())
def main():
# 1. Metrics: real low-cardinality HTTP and job metrics from the Collector.
prom = get("http://localhost:9090/api/v1/query?query=" +
urllib.parse.quote("http_server_requests_total"))["data"]["result"]
assert prom, "no http_server_requests_total series"
assert any("http_route" in s["metric"] for s in prom), "route label missing"
jobs = get("http://localhost:9090/api/v1/query?query=" +
urllib.parse.quote("messenger_jobs_total"))["data"]["result"]
assert jobs, "no messenger_jobs_total series"
print("METRICS OK: http series=%d, job series=%d" % (len(prom), len(jobs)))
# 2. Logs: structured OTLP records with trace/span IDs for an API request.
now = int(time.time() * 1e9)
query = urllib.parse.quote('{job=~"messenger-.*"} |= "message.created"')
logs = get("http://localhost:3100/loki/api/v1/query_range?query=%s&start=%d&end=%d&limit=3"
% (query, now - 30 * 60 * 10**9, now + 30 * 60 * 10**9))["data"]["result"]
assert logs, "no API logs in Loki"
line = logs[0]["values"][0][1]
assert "traceid" in line and "spanid" in line, "trace/span IDs missing from log"
print("LOGS OK: job=%s, traceid/spanid present" % logs[0]["stream"]["job"])
# 3. Traces: real API and worker spans in Tempo.
api = get("http://localhost:3200/api/search?tags=service.name%3Dmessenger-api&limit=3")["traces"]
worker = get("http://localhost:3200/api/search?tags=service.name%3Dmessenger-worker&limit=3")["traces"]
assert api and worker, "missing API or worker traces"
print("TRACES OK: api=%d, worker-linked=%d" % (len(api), len(worker)))
# 4. Grafana provisioning: datasources and an imported dashboard.
auth = {"Authorization": "Basic " + base64.b64encode(b"admin:admin-local-only").decode()}
sources = get("http://localhost:3000/api/datasources", auth)
boards = get("http://localhost:3000/api/search?type=dash-db", auth)
assert len(sources) >= 3, "datasources not provisioned"
assert any(b["uid"] == "messenger-lab6" for b in boards), "dashboard not provisioned"
print("GRAFANA OK: datasources=%d, dashboard=messenger-lab6" % len(sources))
if __name__ == "__main__":
main()