File size: 15,290 Bytes
44084af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
"""Scoring of expert-tier requirement kinds from promtool snapshot results (see xgrade.py for the flow).

Outcome requirements count *delivered* alerts: each snapshot is filtered through the inhibition simulator
before anything is counted, so an over-broad inhibition cannot make a quiet pager look fixed.
"""

from __future__ import annotations

import json

from . import amcheck, notify
from .norm import effective
from .static import template_labels

CLASS_SEV = {"page": {"page", "critical"}, "ticket": {"ticket"}}
PAGER = "pagerduty-"


def _mean(xs) -> float:
    xs = list(xs)
    return sum(xs) / len(xs) if xs else 1.0


def labels_cover(labels: dict | None, required: dict) -> bool:
    """The expert README's label rule: every alert carries the labels it names (`severity`, `team`, `service` for
    service alerts, `slo` for burn alerts), with those values. It never says "only these", so an alert may carry
    more; whether extra labels change where it goes is graded on delivery (Rt, route checks, `_routes_alike`)."""
    return isinstance(labels, dict) and all(k in labels and str(labels[k]) == str(v) for k, v in required.items())


def counted(alerts: list[dict], svc: str, oc: dict, cls: str | None = None, extra: set | None = None) -> list[dict]:
    sev = CLASS_SEV[cls or oc["services"][svc]["class"]]
    named = oc.get("alertname")
    excl = set(oc.get("exclude", [])) | (extra or set())
    return [a for a in alerts if a.get("service") == svc and a.get("severity") in sev
            and a.get("alertname") not in excl and (named is None or a.get("alertname") == named)]


class Ctx:
    """What the scorers need from one grade: parsed rules, AM config, promtool results, second passes."""

    def __init__(self, alerts, am, am_path, am_text, results, masks, rerun):
        self.alerts, self.am, self.am_path, self.am_text = alerts, am, am_path, am_text
        self.results, self.masks, self.rerun = results, masks, rerun

    def snap(self, group: str, pid: str):
        res = self.results.get(group)
        return None if res is None else res.get(pid)

    def delivered(self, alerts: list[dict]) -> list[dict] | None:
        if not isinstance(self.am, dict):
            return None
        return notify.delivered(self.am, alerts)

    def receivers(self, labels: dict) -> list[str] | None:
        if not isinstance(self.am, dict):
            return None
        return amcheck.resolve(self.am_path, self.am_text, labels)


def window_quiet(ctx: Ctx, g: dict, p: dict, pick) -> bool:
    """HS-2: a window probe passes if nothing counted fired; otherwise re-check minute by minute after
    inhibition (a counted alert that was inhibited by a real source in the same minute is not delivered)."""
    got = ctx.snap(g["name"], p["id"])
    if got is None:
        return False
    if not pick(got):
        return True
    snaps = ctx.rerun(g, p["t0"], p["t"])
    if snaps is None:
        return False
    for alerts in snaps:
        live = ctx.delivered(alerts)
        if live is None or pick(live):
            return False
    return True


def labelless_ok(ctx: Ctx, g: dict, cluster_level: set[str]) -> bool:
    """G: no label-less alert that reaches a pager, outside the cluster-level list and the group's exempt
    names (alertnames that fire label-less in the pristine or oracle state of this scenario)."""
    p = next((x for x in g["probes"] if x.get("family") == "G"), None)
    if p is None:
        return True
    got = ctx.snap(g["name"], p["id"])
    if got is None:
        return False
    exempt = set(g.get("g_exempt", [])) | cluster_level
    for a in got:
        if a.get("alertname") in exempt:
            continue
        recv = ctx.receivers(a)
        if recv is None or any(r.startswith(PAGER) for r in recv):
            return False
    return True


def outcome_q(r: dict, groups: list[dict], ctx: Ctx, cluster_level: set[str]) -> tuple[float, dict]:
    oc = r["outcome"]
    named = oc.get("alertname")
    exists = named is None or bool(ctx.alerts.get(named))
    touch = set(oc.get("touch", []))
    fam: dict[str, list[float]] = {"F_detect": [], "F_regress": [], "S_defect": [], "S_std": []}
    rt, lab, gl, dup = [], [], [], 1.0
    seen: set[str] = set()
    noisy: list[str] = []
    for g in groups:
        masked = set(ctx.masks.get(g["name"], []))
        own = [p for p in g["probes"] if p.get("family") in fam]
        if not own:
            continue
        gl.append(1.0 if labelless_ok(ctx, g, cluster_level) else 0.0)
        bg = set(g.get("o_exempt", [])) - touch
        for p in own:
            if p["type"] == "count":
                fam[p["family"]].append(1.0 if ctx.snap(g["name"], p["id"]) is True else 0.0)
                continue
            svc = p["svc"]
            if p["family"].startswith("F"):
                hits = []
                for pid in p.get("any_of", [p["id"]]):
                    got = ctx.snap(g["name"], pid)
                    live = ctx.delivered(got) if got is not None else None
                    if live is not None:
                        hits.extend(counted(live, svc, oc, p.get("class"), bg))
                fam[p["family"]].append(1.0 if (hits and exists) else 0.0)
                seen.update(a["alertname"] for a in hits)
                if hits:
                    want = oc["services"][svc] if not p.get("class") else oc["classes"][p["class"]]
                    rt.append(_route_ok(ctx, hits, want))
                    lab.append(_labels_ok(ctx, hits, want, oc))
                    if len({a["alertname"] for a in hits}) > oc.get("expected_count", 1):
                        dup = 0.8
            else:
                quiet = not (masked & touch) and window_quiet(
                    ctx, g, p, lambda xs, s=svc, c=p.get("class"): counted(xs, s, oc, c, bg))
                fam[p["family"]].append(1.0 if quiet else 0.0)
                if not quiet:
                    got = ctx.snap(g["name"], p["id"]) or []
                    noisy.append(f"{p['id']}:" + ",".join(sorted({a.get('alertname', '') for a in counted(got, svc, oc, p.get('class'), bg)})))
    q = 1.0
    for v in fam.values():
        q *= _mean(v)
    G, Rt, L = _mean(gl), _mean(rt), _mean(lab)
    q *= G * dup * (0.6 + 0.25 * Rt + 0.15 * L)
    return q, {**{k: round(_mean(v), 4) for k, v in fam.items()}, "G": G, "dup": dup, "Rt": round(Rt, 4),
               "L": round(L, 4), "exists": exists, "counted": sorted(seen), "noisy": noisy[:6]}


def _route_ok(ctx: Ctx, hits: list[dict], want: dict) -> float:
    ok = []
    for a in hits:
        recv = ctx.receivers(a)
        if recv is None:
            ok.append(0.0)
            continue
        good = set(want["expect"]) <= set(recv)
        stray = any(x.startswith(PAGER) and x not in want["expect"] for x in recv)
        ok.append(1.0 if good and not stray else 0.0)
    return _mean(ok)


def _merged(d):
    """A rule reference whose labels include its group's `labels:` (the rule's own value wins)."""
    from .loader import RuleRef
    g = d.group.get("labels") if isinstance(d.group.get("labels"), dict) else {}
    r = d.rule.get("labels") if isinstance(d.rule.get("labels"), dict) else {}
    if not g:
        return d
    return RuleRef(d.file, d.group, {**d.rule, "labels": {**g, **r}})


def _labels_ok(ctx: Ctx, hits: list[dict], want: dict, oc: dict) -> float:
    ok = []
    conv = want["labels"]
    for a in hits:
        defs = [_merged(d) for d in ctx.alerts.get(a["alertname"], [])]
        good = False
        for d in defs:
            lab = d.rule.get("labels") if isinstance(d.rule.get("labels"), dict) else {}
            lab = {str(k): str(v) for k, v in lab.items()}
            if lab.get("service") == a.get("service"):
                lab.pop("service")
            ann = d.rule.get("annotations") if isinstance(d.rule.get("annotations"), dict) else {}
            if (labels_cover(lab, conv) and str(ann.get("summary", "")).strip()
                    and str(ann.get("runbook_url", "")).strip() == oc["runbook_prefix"] + a["alertname"]):
                good = True
        ok.append(1.0 if good else 0.0)
    return _mean(ok)


def storm_q(r: dict, groups: list[dict], ctx: Ctx) -> tuple[float, dict]:
    st = r["storm"]
    bound, paged, split = [], [], []
    for g in groups:
        sg = g.get("storm")
        if not sg:
            continue
        snaps = [ctx.snap(g["name"], p["id"]) for p in g["probes"] if p.get("family") == "storm"]
        notes = None
        if isinstance(ctx.am, dict) and all(s is not None for s in snaps):
            notes = notify.notifications(ctx.am, ctx.am_path, ctx.am_text, snaps)
        if notes is None:
            bound.append(0.0), paged.append(0.0), split.append(0.0)
            continue
        pages = [n for n in notes if n[0].startswith(PAGER)]
        b_ok, p_ok = True, True
        muted = set(sg.get("muted", []))
        for svc in sg["failing"]:
            owner = PAGER + st["services"][svc]["team"]
            mine = [n for n in pages if svc in n[3]]
            b_ok &= len(mine) <= 1
            # a held-back service pages nobody (a stale team label paging the old rota is still a page)
            p_ok &= (not mine) if svc in muted else any(n[0] == owner for n in mine)
        if sg.get("cluster"):
            p_ok &= any(n[0] == sg["cluster"] for n in pages)
        bound.append(1.0 if b_ok else 0.0)
        paged.append(1.0 if p_ok else 0.0)
        if sg.get("split"):
            split.append(0.0 if any(len(n[3] & set(sg["failing"])) > 1 for n in pages) else 1.0)
    q = _mean(bound) * _mean(paged) * _mean(split)
    return q, {"bound_ok": round(_mean(bound), 4), "owner_paged": round(_mean(paged), 4),
               "split_ok": round(_mean(split), 4)}


def _routes_alike(ctx, name: str, svc: str | None, lab: dict, required: dict) -> bool:
    """Extra labels on a named alert are fine when they don't change where it goes (its routing is not otherwise
    graded): the receivers with them equal the receivers with the required labels alone."""
    extra = {k: v for k, v in lab.items() if k not in required and k != "service"}
    if not extra or ctx is None or not isinstance(ctx.am, dict):
        return True
    base = {"alertname": name, **({"service": svc} if svc else {}), **{k: str(v) for k, v in required.items()}}
    a, b = ctx.receivers({**base, **extra}), ctx.receivers(base)
    return a is not None and b is not None and sorted(set(a)) == sorted(set(b))


def _best_static(defs: list, a: dict, svc: str | None, ctx=None):
    """README reading of a named alert's static parts: the required labels with their values (others allowed when
    they don't change where it goes; a static `service` equal to the alert's own service is fine either way), the
    summary names the service (templated or written out, like the Down alerts do), and the runbook URL."""
    best, best_res = None, {"labels": False, "summary": False, "runbook": False}
    for d in defs:
        lab = d.rule.get("labels")
        lab = {str(k): str(v) for k, v in lab.items()} if isinstance(lab, dict) else None
        if lab is not None and svc and lab.get("service") == svc:
            lab.pop("service")
        ann = d.rule.get("annotations") if isinstance(d.rule.get("annotations"), dict) else {}
        s = ann.get("summary")
        res = {"labels": lab is not None and labels_cover(lab, a["labels"])
                         and _routes_alike(ctx, a["name"], svc, lab, a["labels"]),
               "summary": isinstance(s, str) and s.strip() != "" and (
                   "service" in template_labels({"annotations": {"s": s}}) or bool(svc and svc in s)),
               "runbook": str(ann.get("runbook_url", "")).strip() == a["runbook"]}
        if best is None or sum(res.values()) > sum(best_res.values()):
            best, best_res = d, res
    return best, best_res


def alert_q(r: dict, groups: list[dict], ctx: Ctx) -> tuple[float, dict]:
    """Named alerts (core-style count probes): E · dup · fire · silent · (0.7 + 0.3·labels/annotations)."""
    a = r["alert"]
    defs = [_merged(d) for d in ctx.alerts.get(a["name"], [])]
    svc = a.get("service") or next((p["svc"] for g in groups for p in g["probes"] if p.get("svc")), None)
    best, res = _best_static(defs, a, svc, ctx)
    fire, silent, dyn = [], [], []
    for g in groups:
        for p in g["probes"]:
            got = ctx.snap(g["name"], p["id"])
            if p.get("family") in ("fire", "timing"):
                fire.append(1.0 if got is True else 0.0)
            elif p.get("family") == "silent":
                silent.append(1.0 if got is True else 0.0)
            elif p.get("family") == "dyn":
                mine = [x for x in (got or []) if x.get("alertname") == a["name"] and x.get("service") == p["svc"]]
                refs = template_labels(best.rule) if best is not None else set()
                dyn.append(1.0 if any(all(x.get(k) == v for k, v in a["labels"].items())
                                      and all(x.get(l) for l in refs) for x in mine) else 0.0)
    refs_ok = _mean(dyn) if dyn else 1.0
    E = 1.0 if defs else 0.0
    # identical copies fire as one alert (Alertmanager deduplicates them): only distinct definitions count
    distinct = {json.dumps(effective(x.rule, x.group), sort_keys=True, default=str) for x in defs}
    d = 0.8 if len(distinct) > a.get("expected_count", 1) else 1.0
    la = _mean([1.0 if res["labels"] else 0.0, 1.0 if res["summary"] else 0.0, 1.0 if res["runbook"] else 0.0,
                refs_ok])
    q = E * d * _mean(fire) * _mean(silent) * (0.7 + 0.3 * la)
    return q, {"E": E, "dup": d, "fire": round(_mean(fire), 4), "silent": round(_mean(silent), 4),
               "label_annot": round(la, 4)}


def annotation_q(r: dict, groups: list[dict], ctx: Ctx) -> tuple[float, dict]:
    """Every label an annotation template reads is present on the firing alert (a Slack line that stops
    mid-sentence reads a label the aggregation dropped)."""
    name = r["annotation"]["alert"]
    defs = ctx.alerts.get(name, [])
    fired = []
    for g in groups:
        for p in g["probes"]:
            if p.get("family") == "annot":
                got = ctx.snap(g["name"], p["id"]) or []
                fired += [a for a in got if a.get("alertname") == name]
    if not defs or not fired:
        return 0.0, {"fired": bool(fired), "exists": bool(defs)}
    refs = set()
    for d in defs:
        refs |= template_labels({"annotations": d.rule.get("annotations") or {}})
    ok = all(all(l in a for l in refs) for a in fired)
    svc = r["annotation"].get("service")

    def names_it(d) -> bool:
        """The summary says what is affected: it is non-empty and names the service, written out or through a
        label whose value on the fired alert is the service."""
        text = str((d.rule.get("annotations") or {}).get("summary", ""))
        if not text.strip():
            return False
        if svc is None or svc in text:
            return True
        return any(a.get(lbl) == svc for lbl in template_labels({"annotations": {"s": text}}) for a in fired)

    summ = all(names_it(d) for d in defs)
    return (1.0 if ok and summ else 0.0), {"refs": sorted(refs), "fired": len(fired), "names_service": summ}