ci(metrics): keep full payload with checks and platform stats
This commit is contained in:
parent
dfbe46b0f8
commit
1713dd07c7
90
Jenkinsfile
vendored
90
Jenkinsfile
vendored
@ -179,96 +179,6 @@ spec:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
post {
|
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", "atlasbot")
|
|
||||||
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", "atlasbot")
|
|
||||||
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 {
|
always {
|
||||||
script {
|
script {
|
||||||
if (fileExists('build.env')) {
|
if (fileExists('build.env')) {
|
||||||
|
|||||||
@ -19,6 +19,10 @@ import xml.etree.ElementTree as ET
|
|||||||
from pathlib import Path
|
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 _as_int(node: ET.Element, name: str) -> int:
|
def _as_int(node: ET.Element, name: str) -> int:
|
||||||
raw = node.attrib.get(name) or "0"
|
raw = node.attrib.get(name) or "0"
|
||||||
try:
|
try:
|
||||||
@ -61,6 +65,25 @@ def _load_coverage_percent(path: Path) -> float:
|
|||||||
return 0.0
|
return 0.0
|
||||||
|
|
||||||
|
|
||||||
|
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_text(url: str) -> str:
|
def _read_text(url: str) -> str:
|
||||||
try:
|
try:
|
||||||
with urllib.request.urlopen(url, timeout=10) as resp:
|
with urllib.request.urlopen(url, timeout=10) as resp:
|
||||||
@ -110,10 +133,15 @@ def main() -> int:
|
|||||||
junit_path = Path(os.getenv("JUNIT_PATH", "build/junit.xml"))
|
junit_path = Path(os.getenv("JUNIT_PATH", "build/junit.xml"))
|
||||||
coverage_path = Path(os.getenv("COVERAGE_PATH", "build/coverage.json"))
|
coverage_path = Path(os.getenv("COVERAGE_PATH", "build/coverage.json"))
|
||||||
|
|
||||||
|
repo_root = Path(__file__).resolve().parents[1]
|
||||||
totals = _load_junit(junit_path)
|
totals = _load_junit(junit_path)
|
||||||
coverage_pct = _load_coverage_percent(coverage_path)
|
coverage_pct = _load_coverage_percent(coverage_path)
|
||||||
|
over_500 = _count_lines_over_limit(repo_root)
|
||||||
passed = max(totals["tests"] - totals["failures"] - totals["errors"] - totals["skipped"], 0)
|
passed = max(totals["tests"] - totals["failures"] - totals["errors"] - totals["skipped"], 0)
|
||||||
outcome = "ok" if totals["tests"] > 0 and totals["failures"] == 0 and totals["errors"] == 0 else "failed"
|
outcome = "ok" if totals["tests"] > 0 and totals["failures"] == 0 and totals["errors"] == 0 else "failed"
|
||||||
|
tests_check = "ok" if outcome == "ok" else "failed"
|
||||||
|
coverage_check = "ok" if coverage_pct >= 95.0 else "failed"
|
||||||
|
loc_check = "ok" if over_500 == 0 else "failed"
|
||||||
|
|
||||||
metrics = _read_text(f"{pushgateway_url}/metrics")
|
metrics = _read_text(f"{pushgateway_url}/metrics")
|
||||||
ok_count = _counter(metrics, suite, "ok")
|
ok_count = _counter(metrics, suite, "ok")
|
||||||
@ -135,6 +163,14 @@ def main() -> int:
|
|||||||
f'atlasbot_quality_gate_tests_total{{suite="{suite}",result="skipped"}} {totals["skipped"]}',
|
f'atlasbot_quality_gate_tests_total{{suite="{suite}",result="skipped"}} {totals["skipped"]}',
|
||||||
"# TYPE atlasbot_quality_gate_coverage_percent gauge",
|
"# TYPE atlasbot_quality_gate_coverage_percent gauge",
|
||||||
f'atlasbot_quality_gate_coverage_percent{{suite="{suite}"}} {coverage_pct:.3f}',
|
f'atlasbot_quality_gate_coverage_percent{{suite="{suite}"}} {coverage_pct:.3f}',
|
||||||
|
"# TYPE platform_quality_gate_workspace_line_coverage_percent gauge",
|
||||||
|
f'platform_quality_gate_workspace_line_coverage_percent{{suite="{suite}"}} {coverage_pct:.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 atlasbot_quality_gate_checks_total gauge",
|
||||||
|
f'atlasbot_quality_gate_checks_total{{suite="{suite}",check="tests",result="{tests_check}"}} 1',
|
||||||
|
f'atlasbot_quality_gate_checks_total{{suite="{suite}",check="coverage",result="{coverage_check}"}} 1',
|
||||||
|
f'atlasbot_quality_gate_checks_total{{suite="{suite}",check="loc",result="{loc_check}"}} 1',
|
||||||
]
|
]
|
||||||
) + "\n"
|
) + "\n"
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user