ci(metrics): publish checks + platform coverage/loc metrics
This commit is contained in:
parent
eb931e8d46
commit
6f4c141d97
90
Jenkinsfile
vendored
90
Jenkinsfile
vendored
@ -212,96 +212,6 @@ python -c "import json; payload=json.load(open('build/coverage.json', encoding='
|
||||
}
|
||||
|
||||
post {
|
||||
success {
|
||||
container('tester') {
|
||||
sh '''
|
||||
set -euo pipefail
|
||||
export QUALITY_STATUS=ok
|
||||
python - <<'PY'
|
||||
import os
|
||||
import re
|
||||
import urllib.request
|
||||
|
||||
suite = os.environ.get("SUITE_NAME", "ariadne")
|
||||
status = os.environ.get("QUALITY_STATUS", "failed")
|
||||
gateway = os.environ.get("PUSHGATEWAY_URL", "http://platform-quality-gateway.monitoring.svc.cluster.local:9091").rstrip("/")
|
||||
text = urllib.request.urlopen(f"{gateway}/metrics", timeout=10).read().decode("utf-8", errors="replace")
|
||||
|
||||
def counter(name: str) -> float:
|
||||
pattern = re.compile(
|
||||
rf'^platform_quality_gate_runs_total\\{{[^}}]*job="platform-quality-ci"[^}}]*suite="{re.escape(suite)}"[^}}]*status="{name}"[^}}]*\\}}\\s+([0-9]+(?:\\.[0-9]+)?)$',
|
||||
re.M,
|
||||
)
|
||||
match = pattern.search(text)
|
||||
return float(match.group(1)) if match else 0.0
|
||||
|
||||
ok = counter("ok")
|
||||
failed = counter("failed")
|
||||
if status == "ok":
|
||||
ok += 1
|
||||
else:
|
||||
failed += 1
|
||||
payload = (
|
||||
"# TYPE platform_quality_gate_runs_total counter\\n"
|
||||
f'platform_quality_gate_runs_total{{suite="{suite}",status="ok"}} {int(ok)}\\n'
|
||||
f'platform_quality_gate_runs_total{{suite="{suite}",status="failed"}} {int(failed)}\\n'
|
||||
)
|
||||
req = urllib.request.Request(
|
||||
f"{gateway}/metrics/job/platform-quality-ci/suite/{suite}",
|
||||
data=payload.encode("utf-8"),
|
||||
method="POST",
|
||||
headers={"Content-Type": "text/plain"},
|
||||
)
|
||||
urllib.request.urlopen(req, timeout=10).read()
|
||||
PY
|
||||
'''
|
||||
}
|
||||
}
|
||||
failure {
|
||||
container('tester') {
|
||||
sh '''
|
||||
set -euo pipefail
|
||||
export QUALITY_STATUS=failed
|
||||
python - <<'PY'
|
||||
import os
|
||||
import re
|
||||
import urllib.request
|
||||
|
||||
suite = os.environ.get("SUITE_NAME", "ariadne")
|
||||
status = os.environ.get("QUALITY_STATUS", "failed")
|
||||
gateway = os.environ.get("PUSHGATEWAY_URL", "http://platform-quality-gateway.monitoring.svc.cluster.local:9091").rstrip("/")
|
||||
text = urllib.request.urlopen(f"{gateway}/metrics", timeout=10).read().decode("utf-8", errors="replace")
|
||||
|
||||
def counter(name: str) -> float:
|
||||
pattern = re.compile(
|
||||
rf'^platform_quality_gate_runs_total\\{{[^}}]*job="platform-quality-ci"[^}}]*suite="{re.escape(suite)}"[^}}]*status="{name}"[^}}]*\\}}\\s+([0-9]+(?:\\.[0-9]+)?)$',
|
||||
re.M,
|
||||
)
|
||||
match = pattern.search(text)
|
||||
return float(match.group(1)) if match else 0.0
|
||||
|
||||
ok = counter("ok")
|
||||
failed = counter("failed")
|
||||
if status == "ok":
|
||||
ok += 1
|
||||
else:
|
||||
failed += 1
|
||||
payload = (
|
||||
"# TYPE platform_quality_gate_runs_total counter\\n"
|
||||
f'platform_quality_gate_runs_total{{suite="{suite}",status="ok"}} {int(ok)}\\n'
|
||||
f'platform_quality_gate_runs_total{{suite="{suite}",status="failed"}} {int(failed)}\\n'
|
||||
)
|
||||
req = urllib.request.Request(
|
||||
f"{gateway}/metrics/job/platform-quality-ci/suite/{suite}",
|
||||
data=payload.encode("utf-8"),
|
||||
method="POST",
|
||||
headers={"Content-Type": "text/plain"},
|
||||
)
|
||||
urllib.request.urlopen(req, timeout=10).read()
|
||||
PY
|
||||
'''
|
||||
}
|
||||
}
|
||||
always {
|
||||
script {
|
||||
if (fileExists('build/junit.xml')) {
|
||||
|
||||
@ -8,6 +8,11 @@ import os
|
||||
import sys
|
||||
import urllib.request
|
||||
import xml.etree.ElementTree as ET
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
SOURCE_SUFFIXES = {".py", ".js", ".mjs", ".ts", ".tsx", ".json", ".yaml", ".yml", ".sh"}
|
||||
SKIP_DIRS = {".git", ".venv", "venv", "node_modules", "build", "dist", "__pycache__", ".pytest_cache"}
|
||||
|
||||
|
||||
def _escape_label(value: str) -> str:
|
||||
@ -57,6 +62,25 @@ def _load_junit(path: str) -> dict[str, int]:
|
||||
return totals
|
||||
|
||||
|
||||
def _count_lines_over_limit(root: Path, *, max_lines: int = 500) -> int:
|
||||
count = 0
|
||||
for path in root.rglob("*"):
|
||||
if not path.is_file():
|
||||
continue
|
||||
if any(part in SKIP_DIRS for part in path.parts):
|
||||
continue
|
||||
if path.name != "Jenkinsfile" and path.suffix.lower() not in SOURCE_SUFFIXES:
|
||||
continue
|
||||
try:
|
||||
with path.open("r", encoding="utf-8", errors="ignore") as handle:
|
||||
lines = sum(1 for _ in handle)
|
||||
except OSError:
|
||||
continue
|
||||
if lines > max_lines:
|
||||
count += 1
|
||||
return count
|
||||
|
||||
|
||||
def _read_http(url: str) -> str:
|
||||
try:
|
||||
with urllib.request.urlopen(url, timeout=10) as resp:
|
||||
@ -113,8 +137,10 @@ def main() -> int:
|
||||
if not os.path.exists(junit_path):
|
||||
raise RuntimeError(f"missing junit file {junit_path}")
|
||||
|
||||
repo_root = Path(__file__).resolve().parents[1]
|
||||
coverage = _load_coverage(coverage_path)
|
||||
totals = _load_junit(junit_path)
|
||||
over_500 = _count_lines_over_limit(repo_root)
|
||||
passed = max(totals["tests"] - totals["failures"] - totals["errors"] - totals["skipped"], 0)
|
||||
|
||||
outcome = "ok"
|
||||
@ -143,6 +169,9 @@ def main() -> int:
|
||||
"build_number": build_number,
|
||||
"commit": commit,
|
||||
}
|
||||
tests_check = "ok" if outcome == "ok" else "failed"
|
||||
coverage_check = "ok" if coverage >= 95.0 else "failed"
|
||||
loc_check = "ok" if over_500 == 0 else "failed"
|
||||
payload_lines = [
|
||||
"# TYPE platform_quality_gate_runs_total counter",
|
||||
f'platform_quality_gate_runs_total{{suite="{suite}",status="ok"}} {ok_count:.0f}',
|
||||
@ -154,6 +183,14 @@ def main() -> int:
|
||||
f'ariadne_quality_gate_tests_total{{suite="{suite}",result="skipped"}} {totals["skipped"]}',
|
||||
"# TYPE ariadne_quality_gate_coverage_percent gauge",
|
||||
f'ariadne_quality_gate_coverage_percent{{suite="{suite}"}} {coverage:.3f}',
|
||||
"# TYPE platform_quality_gate_workspace_line_coverage_percent gauge",
|
||||
f'platform_quality_gate_workspace_line_coverage_percent{{suite="{suite}"}} {coverage:.3f}',
|
||||
"# TYPE platform_quality_gate_source_lines_over_500_total gauge",
|
||||
f'platform_quality_gate_source_lines_over_500_total{{suite="{suite}"}} {over_500}',
|
||||
"# TYPE ariadne_quality_gate_checks_total gauge",
|
||||
f'ariadne_quality_gate_checks_total{{suite="{suite}",check="tests",result="{tests_check}"}} 1',
|
||||
f'ariadne_quality_gate_checks_total{{suite="{suite}",check="coverage",result="{coverage_check}"}} 1',
|
||||
f'ariadne_quality_gate_checks_total{{suite="{suite}",check="loc",result="{loc_check}"}} 1',
|
||||
"# TYPE ariadne_quality_gate_build_info gauge",
|
||||
f"ariadne_quality_gate_build_info{_label_str(labels)} 1",
|
||||
]
|
||||
@ -171,6 +208,7 @@ def main() -> int:
|
||||
"tests_errors": totals["errors"],
|
||||
"tests_skipped": totals["skipped"],
|
||||
"coverage_percent": round(coverage, 3),
|
||||
"source_lines_over_500": over_500,
|
||||
"ok_counter": ok_count,
|
||||
"failed_counter": failed_count,
|
||||
},
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user