File size: 6,709 Bytes
f3b0a91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
"""promtool driver: syntax checks, hidden scenario groups, JUnit parsing into per-probe results.

One promtool test group per scenario; each probe is tagged with label_replace(..., "af_check", id)
so every failing expression maps back to exactly one atomic check (§12.2).
"""

from __future__ import annotations

import os
import re
import subprocess
import xml.etree.ElementTree as ET

import yaml

from .loader import RuleFile, plain_yaml

PROMTOOL_TIMEOUT = 90
_RULE_EVAL_ERR = re.compile(r"^\s*rule: .*, time: .*, err: ", re.S)


def tool(name: str) -> str:
    return os.environ.get(f"AF_{name.upper()}", name)


def write_rule_files(files: list[RuleFile], outdir: str, strip_records: set[str] | None = None,
                     order: list[str] | None = None) -> list[str]:
    """Write parsed groups as plain rule files; optionally drop given record names (decoupled pass).

    Groups get unique names (af<file>_<group>) and, when `order` is given, their evaluation order is
    appended to it: recording-only groups first, then the rest, in file order. promtool otherwise
    evaluates groups in Go map order, which makes record-to-alert lag (and so timing) nondeterministic.
    """
    os.makedirs(outdir, exist_ok=True)
    paths, first, rest = [], [], []
    for i, f in enumerate(files):
        groups = f.groups or []
        if strip_records:
            groups = [{**g, "rules": [r for r in (g.get("rules") or [])
                                      if str(r.get("record")) not in strip_records]} for g in groups]
        renamed = []
        for j, g in enumerate(groups):
            name = f"af{i:03d}_{j:03d}"
            renamed.append({**g, "name": name})
            rules = g.get("rules") or []
            (first if rules and all("record" in r for r in rules) else rest).append(name)
        p = os.path.join(outdir, f"f{i:03d}.yml")
        with open(p, "w") as fh:
            fh.write(plain_yaml(renamed))
        paths.append(p)
    if order is not None:
        order.extend(first + rest)
    return paths


def check_rules(files: list[RuleFile], outdir: str) -> dict[str, str]:
    """promtool check rules per parsed file. Returns {rel path: error} for files that fail."""
    errors: dict[str, str] = {}
    parsed = [f for f in files if f.groups is not None]
    for f in files:
        if f.groups is None:
            errors[f.path] = f.error or "unparsable YAML"
    if not parsed:
        return errors
    paths = write_rule_files(parsed, outdir)
    for f, p in zip(parsed, paths):
        try:
            r = subprocess.run([tool("promtool"), "check", "rules", "--lint=none", p],
                               capture_output=True, text=True, timeout=30)
        except subprocess.TimeoutExpired:
            errors[f.path] = "promtool check rules timed out"
            continue
        if r.returncode != 0:
            msg = (r.stderr or r.stdout).strip().replace(p, f.path)
            errors[f.path] = msg[-500:]
    return errors


def dyn_probe(pid: str, expr: str, t: int, expect: int) -> dict:
    return {"id": pid, "expr": expr, "t": t, "expect": expect}


def alert_count_expr(name: str, service: str, extra: dict | None = None, op: str = "=") -> str:
    sel = [f'alertname="{name}"', 'alertstate="firing"', f'service{op}"{service}"']
    for k, v in (extra or {}).items():
        sel.append(f'{k}{"!=" if v is None else "="}"{"" if v is None else v}"')
    return f"count(ALERTS{{{','.join(sel)}}}) or vector(0)"


def build_tests(groups: list[dict], rule_paths: list[str], extra: dict[str, list[dict]],
                order: list[str] | None = None) -> dict:
    tests = []
    for g in groups:
        probes = list(g["probes"]) + list(extra.get(g["name"], []))
        pe = []
        for p in probes:
            pe.append({
                "expr": f'label_replace({p["expr"]}, "af_check", "{p["id"]}", "", "")',
                "eval_time": f'{p["t"]}m',
                "exp_samples": [{"labels": f'{{af_check="{p["id"]}"}}', "value": p["expect"]}],
            })
        tests.append({"name": g["name"], "interval": "1m", "input_series": g["input_series"],
                      "promql_expr_test": pe})
    doc = {"rule_files": rule_paths, "evaluation_interval": "1m", "tests": tests}
    if order:
        doc["group_eval_order"] = order
    return doc


def run_tests(groups: list[dict], rule_paths: list[str], extra: dict[str, list[dict]],
              workdir: str, tag: str, order: list[str] | None = None) -> dict[str, bool]:
    """Run promtool on the given scenario groups; returns {probe id: passed}."""
    all_ids = {g["name"]: [p["id"] for p in g["probes"]] + [p["id"] for p in extra.get(g["name"], [])]
               for g in groups}
    result = {pid: False for ids in all_ids.values() for pid in ids}
    if not groups:
        return result
    if not rule_paths:
        empty = os.path.join(workdir, f"empty_{tag}.yml")
        with open(empty, "w") as fh:
            fh.write("groups: []\n")
        rule_paths = [empty]
    test_path = os.path.join(workdir, f"hidden_{tag}.test.yml")
    junit = os.path.join(workdir, f"junit_{tag}.xml")
    with open(test_path, "w") as fh:
        yaml.safe_dump(build_tests(groups, rule_paths, extra, order), fh, sort_keys=False, width=10**9)
    try:
        subprocess.run([tool("promtool"), "test", "rules", f"--junit={junit}", test_path],
                       capture_output=True, text=True, timeout=PROMTOOL_TIMEOUT)
    except subprocess.TimeoutExpired:
        return result  # grader DoS → checks fail, never a grader error
    if not os.path.exists(junit):
        return result
    try:
        root = ET.parse(junit).getroot()
    except ET.ParseError:
        return result
    for tc in root.iter("testcase"):
        ids = all_ids.get(tc.get("name", ""), [])
        failures = tc.findall("failure")
        if not failures:
            for pid in ids:
                result[pid] = True
            continue
        failed: set[str] = set()
        global_failure = False
        for f in failures:
            text = f.text or ""
            found = {pid for pid in ids if re.search(re.escape(pid) + r'\\?"', text)}
            # A per-rule evaluation error (e.g. two copies of an alert writing ALERTS_FOR_STATE with
            # different ActiveAt) is non-fatal in promtool: the probes still run and report on their
            # own. Only id-less failures of other kinds (load/parse errors) fail the whole group.
            if not found and not _RULE_EVAL_ERR.search(text):
                global_failure = True
            failed.update(found)
        for pid in ids:
            result[pid] = not global_failure and pid not in failed
    return result