Verify the grade 3 telemetry stack and inherited API guarantees
This commit is contained in:
Executable
+13
@@ -0,0 +1,13 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
export COMPOSE_PROJECT_NAME=${COMPOSE_PROJECT_NAME:-mtusi2026lab6}
|
||||
export LB_HTTP_PORT=${LB_HTTP_PORT:-18086} LB_STATS_PORT=${LB_STATS_PORT:-26085}
|
||||
export BROKER_MANAGEMENT_PORT=${BROKER_MANAGEMENT_PORT:-26086}
|
||||
export PROMETHEUS_PORT=${PROMETHEUS_PORT:-26090} LOKI_PORT=${LOKI_PORT:-26310}
|
||||
export TEMPO_PORT=${TEMPO_PORT:-26320} GRAFANA_PORT=${GRAFANA_PORT:-26300}
|
||||
export BASE_URL=${BASE_URL:-http://127.0.0.1:$LB_HTTP_PORT}
|
||||
export PROMETHEUS_URL=http://127.0.0.1:$PROMETHEUS_PORT LOKI_URL=http://127.0.0.1:$LOKI_PORT
|
||||
export TEMPO_URL=http://127.0.0.1:$TEMPO_PORT GRAFANA_URL=http://127.0.0.1:$GRAFANA_PORT
|
||||
export NO_PROXY=localhost,127.0.0.1,::1
|
||||
exec "$@"
|
||||
@@ -66,7 +66,7 @@ def wait_for(what, check, timeout, interval=2.0):
|
||||
result = check()
|
||||
if result:
|
||||
return result
|
||||
except Failure:
|
||||
except (Failure, OSError):
|
||||
pass
|
||||
if time.monotonic() > deadline:
|
||||
raise Failure(f"timeout waiting for {what}")
|
||||
@@ -253,6 +253,10 @@ def request_check(args, b):
|
||||
if not trace_id:
|
||||
raise Failure("log record has no trace_id")
|
||||
spans = wait_for("API trace in Tempo", lambda: tempo_spans(b.trace(trace_id)[0]), args.timeout)
|
||||
if not any(s["kind"] == "SPAN_KIND_SERVER" and s["service"] == args.api_service for s in spans):
|
||||
raise Failure("no real API server span")
|
||||
if not any(s["kind"] == "SPAN_KIND_CLIENT" and s["attributes"].get("db.system") == "postgresql" for s in spans):
|
||||
raise Failure("no PostgreSQL client span inside API trace")
|
||||
print(json.dumps({"checked_at": now(), "request_id": rid, "log": {"line": line[:200], "trace_id": trace_id,
|
||||
"span_id": labels.get("span_id"),
|
||||
"service_name": labels.get("service_name"),
|
||||
@@ -282,6 +286,10 @@ def job_check(args, b):
|
||||
query = f'{{service_name="{args.worker_service}"}} | {args.log_attachment_key}="{attachment["id"]}"'
|
||||
worker_logs = wait_for("worker logs", lambda: b.logs(query, t0 - 30, time.time() + 30), args.timeout)
|
||||
api.logout(auth)
|
||||
if ready["status"] != "ready" or not any(s["kind"] == "SPAN_KIND_CONSUMER" and s["service"] == args.worker_service for s in chain):
|
||||
raise Failure("job is not ready or no real consumer span")
|
||||
if not any(labels.get("trace_id") and labels.get("span_id") for labels, _ in worker_logs):
|
||||
raise Failure("worker log is not correlated")
|
||||
print(json.dumps({"checked_at": now(), "upload_request_id": rid, "attachment_status": ready["status"],
|
||||
"traces": sorted({c["trace_id"] for c in chain}), "spans": chain,
|
||||
"worker_logs": [{"line": line[:200], "trace_id": labels.get("trace_id"), "span_id": labels.get("span_id")}
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import time
|
||||
|
||||
from obs_check import Api, Backends, get_json, http, now, tempo_spans, wait_for
|
||||
|
||||
|
||||
def backends():
|
||||
return Backends(
|
||||
argparse.Namespace(
|
||||
prometheus=os.environ["PROMETHEUS_URL"],
|
||||
loki=os.environ["LOKI_URL"],
|
||||
tempo=os.environ["TEMPO_URL"],
|
||||
grafana=os.environ["GRAFANA_URL"],
|
||||
grafana_password_file=os.environ.get(
|
||||
"GRAFANA_PASSWORD_FILE", "/".join(("secrets", "grafana_admin_password"))
|
||||
),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def compose(*args):
|
||||
return subprocess.run(
|
||||
["docker", "compose", *args], check=True, capture_output=True, text=True
|
||||
).stdout.strip()
|
||||
|
||||
|
||||
def history(b):
|
||||
started = time.time()
|
||||
_, _, _, rid = Api(os.environ["BASE_URL"]).register()
|
||||
query = '{service_name="messenger-api"} | request_id="' + rid + '"'
|
||||
logs = wait_for("log", lambda: b.logs(query, started - 1, time.time() + 1), 60)
|
||||
trace_id = logs[0][0]["trace_id"]
|
||||
spans = wait_for("trace", lambda: tempo_spans(b.trace(trace_id)[0]), 60)
|
||||
expr = 'sum(http_server_request_duration_seconds_count{job="messenger-api"})'
|
||||
before = wait_for("metrics", lambda: b.promql(expr), 60)
|
||||
at = before[0]["value"][0]
|
||||
compose("restart", "prometheus", "loki", "tempo", "grafana")
|
||||
wait_for("Grafana", lambda: get_json(b.grafana + "/api/health"), 90)
|
||||
after_logs = wait_for("retained log", lambda: b.logs(query, started - 1, time.time() + 1), 90)
|
||||
after_spans = wait_for("retained trace", lambda: tempo_spans(b.trace(trace_id)[0]), 90)
|
||||
after = wait_for("retained metric", lambda: b.promql(expr, at=at), 90)
|
||||
if before[0]["value"][1] != after[0]["value"][1] or len(after_spans) < len(spans):
|
||||
raise RuntimeError("telemetry history changed after restart")
|
||||
return {
|
||||
"request_id": rid,
|
||||
"trace_id": trace_id,
|
||||
"spans_before": len(spans),
|
||||
"spans_after": len(after_spans),
|
||||
"logs_after": len(after_logs),
|
||||
"metric_before": before,
|
||||
"metric_after": after,
|
||||
}
|
||||
|
||||
|
||||
def collector():
|
||||
compose("stop", "otel-collector")
|
||||
try:
|
||||
status = http("GET", os.environ["BASE_URL"] + "/health/ready")[0]
|
||||
result = subprocess.run(["make", "test"], capture_output=True, text=True, check=False)
|
||||
if status != 200 or result.returncode:
|
||||
raise RuntimeError("application failed without collector")
|
||||
replica = compose("ps", "-q", "api").splitlines()[0]
|
||||
started = time.monotonic()
|
||||
subprocess.run(["docker", "stop", "-t", "8", replica], check=True, capture_output=True)
|
||||
elapsed = time.monotonic() - started
|
||||
code = subprocess.check_output(
|
||||
["docker", "inspect", "-f", "{{.State.ExitCode}}", replica], text=True
|
||||
).strip()
|
||||
if code != "0" or elapsed > 8:
|
||||
raise RuntimeError("unbounded API shutdown")
|
||||
return {
|
||||
"ready": status,
|
||||
"smoke": result.stdout.strip(),
|
||||
"api_exit_code": code,
|
||||
"api_shutdown_seconds": round(elapsed, 3),
|
||||
}
|
||||
finally:
|
||||
compose("up", "-d", "--no-deps", "--wait", "api", "otel-collector")
|
||||
subprocess.run(["scripts/lb-wait.sh"], check=True)
|
||||
|
||||
|
||||
def main():
|
||||
b = backends()
|
||||
report = {
|
||||
"utc": now(),
|
||||
"sha": subprocess.check_output(["git", "rev-parse", "HEAD"], text=True).strip(),
|
||||
}
|
||||
report["history"] = history(b)
|
||||
report["collector_down"] = collector()
|
||||
report["result"] = "PASS"
|
||||
print(json.dumps(report, indent=2))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user