#!/usr/bin/env python3 """Publish Metis quality-gate test metrics to Pushgateway (Prometheus ingest).""" from __future__ import annotations import json import os import urllib.request import xml.etree.ElementTree as ET def _escape_label(value: str) -> str: return value.replace("\\", "\\\\").replace("\n", "\\n").replace('"', '\\"') def _label_str(labels: dict[str, str]) -> str: parts = [f'{key}="{_escape_label(val)}"' for key, val in labels.items() if val] return "{" + ",".join(parts) + "}" if parts else "" def _load_coverage(path: str) -> float: with open(path, "r", encoding="utf-8") as handle: payload = json.load(handle) summary = payload.get("summary") or {} percent = summary.get("percent_covered") if isinstance(percent, (int, float)): return float(percent) raise RuntimeError("coverage summary missing percent_covered") def _load_junit(path: str) -> dict[str, int]: tree = ET.parse(path) root = tree.getroot() def _as_int(node: ET.Element, name: str) -> int: raw = node.attrib.get(name) or "0" try: return int(float(raw)) except ValueError: return 0 if root.tag == "testsuite": suites = [root] elif root.tag == "testsuites": suites = list(root.findall("testsuite")) else: suites = [] totals = {"tests": 0, "failures": 0, "errors": 0, "skipped": 0} for suite in suites: totals["tests"] += _as_int(suite, "tests") totals["failures"] += _as_int(suite, "failures") totals["errors"] += _as_int(suite, "errors") totals["skipped"] += _as_int(suite, "skipped") return totals def _load_exit_code(path: str) -> int | None: if not path or not os.path.exists(path): return None with open(path, "r", encoding="utf-8") as handle: raw = handle.read().strip() if not raw: return None try: return int(raw) except ValueError as exc: raise RuntimeError(f"invalid test exit code {raw!r} in {path}") from exc def _post_text(url: str, payload: str) -> None: req = urllib.request.Request( url, data=payload.encode("utf-8"), method="POST", headers={"Content-Type": "text/plain"}, ) with urllib.request.urlopen(req, timeout=10) as resp: if resp.status >= 400: raise RuntimeError(f"metrics push failed status={resp.status}") def main() -> int: coverage_path = os.getenv("COVERAGE_JSON", "build/coverage.json") junit_path = os.getenv("JUNIT_XML", "build/junit.xml") test_exit_code_path = os.getenv("TEST_EXIT_CODE_PATH", "build/test.exitcode") pushgateway_url = os.getenv( "PUSHGATEWAY_URL", "http://platform-quality-gateway.monitoring.svc.cluster.local:9091" ).strip() suite = os.getenv("SUITE_NAME", "metis") branch = os.getenv("BRANCH_NAME", "") build_number = os.getenv("BUILD_NUMBER", "") commit = os.getenv("GIT_COMMIT", "") strict = os.getenv("METRICS_STRICT", "") == "1" if not os.path.exists(coverage_path): raise RuntimeError(f"missing coverage file {coverage_path}") if not os.path.exists(junit_path): raise RuntimeError(f"missing junit file {junit_path}") coverage = _load_coverage(coverage_path) totals = _load_junit(junit_path) test_exit_code = _load_exit_code(test_exit_code_path) passed = max(totals["tests"] - totals["failures"] - totals["errors"] - totals["skipped"], 0) outcome = "ok" if ( (test_exit_code is not None and test_exit_code != 0) or totals["tests"] <= 0 or totals["failures"] > 0 or totals["errors"] > 0 ): outcome = "failed" labels = { "job": "platform-quality-ci", "suite": suite, "branch": branch, "build_number": build_number, "commit": commit, } payload_lines = [ "# TYPE metis_quality_gate_tests_total gauge", f'metis_quality_gate_tests_total{{suite="{suite}",result="total"}} {totals["tests"]}', f'metis_quality_gate_tests_total{{suite="{suite}",result="passed"}} {passed}', f'metis_quality_gate_tests_total{{suite="{suite}",result="failed"}} {totals["failures"]}', f'metis_quality_gate_tests_total{{suite="{suite}",result="error"}} {totals["errors"]}', f'metis_quality_gate_tests_total{{suite="{suite}",result="skipped"}} {totals["skipped"]}', "# TYPE metis_quality_gate_run_status gauge", f'metis_quality_gate_run_status{{suite="{suite}",status="ok"}} {1 if outcome == "ok" else 0}', f'metis_quality_gate_run_status{{suite="{suite}",status="failed"}} {1 if outcome == "failed" else 0}', "# TYPE metis_quality_gate_coverage_percent gauge", f'metis_quality_gate_coverage_percent{{suite="{suite}"}} {coverage:.3f}', "# TYPE metis_quality_gate_build_info gauge", f"metis_quality_gate_build_info{_label_str(labels)} 1", ] payload = "\n".join(payload_lines) + "\n" try: _post_text(f"{pushgateway_url.rstrip('/')}/metrics/job/{labels['job']}/suite/{suite}", payload) except Exception as exc: print(f"metrics push failed: {exc}") if strict: raise print( json.dumps( { "suite": suite, "outcome": outcome, "tests_total": totals["tests"], "tests_passed": passed, "tests_failed": totals["failures"], "tests_errors": totals["errors"], "tests_skipped": totals["skipped"], "coverage_percent": round(coverage, 3), "test_exit_code": test_exit_code, }, indent=2, ) ) return 0 if __name__ == "__main__": raise SystemExit(main())