File size: 9,677 Bytes
f3b0a91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44084af
 
 
 
f3b0a91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44084af
 
 
 
 
 
f3b0a91
 
 
 
 
 
 
44084af
f3b0a91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44084af
 
f3b0a91
 
44084af
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
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
"""Alert kinds: spec → oracle rule text, and spec → reference condition (independent of the text).

Seven kinds (U3): two SLO burn tiers plus five fleet/service templates modeled on common community
rules (error ratio with a traffic guard, memory near limit, crash looping, p99 latency, target down).
"""

from __future__ import annotations

from dataclasses import dataclass, field
from fractions import Fraction as F
from typing import Callable

from . import promsim as ps
from .series import FAMILIES

RECORD_PREFIX = "slo:sli_error:ratio_rate"
WINDOW_MIN = {"5m": 5, "30m": 30, "1h": 60, "6h": 360}
FOR_MIN = {"burn_page": 2, "burn_ticket": 15, "error_ratio": 5, "mem_high": 5, "crashloop": 10,
           "latency_p99": 10, "target_down": 3}
# Lowest accepted `for` when a repair leaves the duration to the agent (B02 on fleet alerts): the postmortem
# only says spikes of "a minute or two" must not page. Instant conditions need 3m (2m spike + 1m margin);
# 5m-rate conditions keep a short spike inside the window for ~5 evaluations, so they need 5m.
FOR_FLOOR = {"error_ratio": 5, "latency_p99": 5, "mem_high": 3, "crashloop": 3}
BURN = {"burn_page": ("1h", "5m", F("14.4")), "burn_ticket": ("6h", "30m", F(6))}
FLEET_KINDS = ("error_ratio", "mem_high", "crashloop", "latency_p99")
FLEET_NAMES = {
    "error_ratio": ["HighErrorRatio", "ServiceErrorRateHigh", "HTTPErrorRatioHigh"],
    "mem_high": ["ContainerMemoryNearLimit", "PodMemoryHigh"],
    "crashloop": ["PodCrashLooping", "ContainerRestartingOften"],
    "latency_p99": ["LatencyP99High", "SlowRequestsP99"],
}


class NearThreshold(Exception):
    """A reference value is too close to a threshold; the scenario must be re-sampled."""


@dataclass
class AlertSpec:
    req_id: str
    kind: str
    name: str
    service: str | None           # per-service alerts (burn_*, target_down); None for fleet alerts
    family: str                   # SLI family: http | grpc | latency
    labels: dict[str, str]
    runbook: str
    params: dict = field(default_factory=dict)

    @property
    def for_min(self) -> int:
        return int(self.params.get("for_min", FOR_MIN[self.kind]))

    @property
    def for_range(self) -> tuple[int, int]:
        """Accepted `for` minutes (lo, hi); a single value unless the task text leaves it open."""
        lo, hi = self.params.get("for_range", (self.for_min, self.for_min))
        return int(lo), int(hi)

    @property
    def uses_records(self) -> bool:
        return self.kind in BURN

    @property
    def threshold(self) -> F:
        if self.kind in BURN:
            return F(self.params.get("factor", BURN[self.kind][2])) * (1 - F(self.params["target"]))
        return F(self.params.get("thr", 0))

    def records_used(self) -> list[str]:
        if self.kind not in BURN:
            return []
        lw, sw, _ = BURN[self.kind]
        return [RECORD_PREFIX + lw, RECORD_PREFIX + sw]


def fmt(x: F) -> str:
    s = f"{float(x):.10f}".rstrip("0").rstrip(".")
    return s or "0"


def _sel(service: str | None) -> str:
    return f'{{service="{service}"}}' if service else ""


def oracle_expr(a: AlertSpec) -> str:
    k, p = a.kind, a.params
    if k in BURN:
        lw, sw, factor = BURN[k]
        b = fmt(1 - F(p["target"]))
        return (f"{RECORD_PREFIX}{lw}{_sel(a.service)} > ({fmt(factor)} * {b})"
                f" and {RECORD_PREFIX}{sw}{_sel(a.service)} > ({fmt(factor)} * {b})")
    if k == "error_ratio":
        fam = FAMILIES[a.family]
        m, bad = fam["metric"], fam["bad_sel"]
        return (f"(sum by (service) (rate({m}{{{bad}}}[5m])) / sum by (service) (rate({m}[5m])))"
                f" > {fmt(F(p['thr']))} and sum by (service) (rate({m}[5m])) > {fmt(F(p['min_rps']))}")
    if k == "mem_high":
        return ('max by (service) (container_memory_working_set_bytes{container="app"}'
                ' / container_spec_memory_limit_bytes{container="app"})'
                f" > {fmt(F(p['thr']))}")
    if k == "crashloop":
        return (f"sum by (service) (increase(kube_pod_container_status_restarts_total[15m]))"
                f" > {fmt(F(p['thr']))}")
    if k == "latency_p99":
        return ("histogram_quantile(0.99, sum by (service, le) "
                f"(rate(http_request_duration_seconds_bucket[5m]))) > {fmt(F(p['thr']))}")
    if k == "target_down":
        s = a.service
        return f'sum by (service) (up{{service="{s}"}}) == 0 or absent(up{{service="{s}"}})'
    raise ValueError(k)


SUMMARY = {
    "burn_page": "{{ $labels.service }} is burning its error budget fast (14.4x over 1h and 5m)",
    "burn_ticket": "{{ $labels.service }} is burning its error budget steadily (6x over 6h and 30m)",
    "error_ratio": "{{ $labels.service }} error ratio is {{ $value | humanizePercentage }}",
    "mem_high": "{{ $labels.service }} has a container above its memory limit threshold",
    "crashloop": "{{ $labels.service }} containers restarted more than {{ $value }} times in 15m",
    "latency_p99": "{{ $labels.service }} p99 latency is {{ $value | humanizeDuration }}",
    "target_down": "{{ $labels.service }} has no healthy scrape targets",
}


def oracle_rule(a: AlertSpec) -> dict:
    rule = {"alert": a.name, "expr": oracle_expr(a)}
    if a.for_min:
        rule["for"] = f"{a.for_min}m"
    rule["labels"] = dict(a.labels)
    rule["annotations"] = {"summary": SUMMARY[a.kind], "runbook_url": a.runbook}
    return rule


def record_expr(family: str, window: str, slo_le: str = "0.5") -> str:
    if family == "latency":
        return (f'1 - (sum by (service) (rate(http_request_duration_seconds_bucket{{le="{slo_le}"}}[{window}]))'
                f" / sum by (service) (rate(http_request_duration_seconds_count[{window}])))")
    fam = FAMILIES[family]
    m = fam["metric"]
    return f"sum by (service) (rate({m}{{{fam['bad_sel']}}}[{window}])) / sum by (service) (rate({m}[{window}]))"


def record_rules(family: str, windows: list[str], slo_le: str = "0.5") -> list[dict]:
    return [{"record": RECORD_PREFIX + w, "expr": record_expr(family, w, slo_le)} for w in windows]


# ---------------------------------------------------------------------------- reference semantics

def sli_ratio(scen: ps.Scenario, family: str, t: int, w: int, slo_le: str = "0.5") -> dict[str, F]:
    if family == "latency":
        return ps.ratio_by(scen, "http_request_duration_seconds_bucket", None, t, w,
                           total_metric="http_request_duration_seconds_count",
                           good=lambda l, le=slo_le: l.get("le") == le)
    fam = FAMILIES[family]
    return ps.ratio_by(scen, fam["metric"], fam["bad"], t, w)


def _gt(values: dict[str, F], thr: F, only: str | None = None) -> set[str]:
    out = set()
    for svc, v in values.items():
        if only and svc != only:
            continue
        if ps.near(v, thr):
            raise NearThreshold(f"{svc}={float(v)} vs {float(thr)}")
        if v > thr:
            out.add(svc)
    return out


def condition(a: AlertSpec, scen: ps.Scenario) -> Callable[[int], set[str]]:
    """Reference: services for which the alert expression returns a sample at minute t."""
    k, p, thr = a.kind, a.params, a.threshold

    def burn(t: int) -> set[str]:
        lw, sw, _ = BURN[k]
        le = p.get("slo_le", "0.5")
        long_ = _gt(sli_ratio(scen, a.family, t, WINDOW_MIN[lw], le), thr, a.service)
        short = _gt(sli_ratio(scen, a.family, t, WINDOW_MIN[sw], le), thr, a.service)
        return long_ & short

    def error_ratio(t: int) -> set[str]:
        fam = FAMILIES[a.family]
        ratio = _gt(ps.ratio_by(scen, fam["metric"], fam["bad"], t, 5), thr)
        traffic = _gt(ps.sum_rate_by(scen.select(fam["metric"]), "service", t, 5), F(p["min_rps"]))
        return ratio & traffic

    def mem_high(t: int) -> set[str]:
        best: dict[str, F] = {}
        limits = {tuple(sorted(s.labels.items())): s for s in scen.select("container_spec_memory_limit_bytes")}
        for s in scen.select("container_memory_working_set_bytes"):
            lim = limits.get(tuple(sorted(s.labels.items())))
            v, lv = ps.instant(s.values, t), ps.instant(lim.values, t) if lim else None
            if v is None or not lv:
                continue
            svc = s.labels["service"]
            best[svc] = max(best.get(svc, F(-1)), F(v, lv))
        return _gt(best, thr)

    def crashloop(t: int) -> set[str]:
        inc = ps.sum_rate_by(scen.select("kube_pod_container_status_restarts_total"), "service", t, 15,
                             is_rate=False)
        return _gt(inc, thr)

    def latency_p99(t: int) -> set[str]:
        return _gt(ps.quantile_by_service(scen, "http_request_duration_seconds_bucket", F(99, 100), t, 5), thr)

    def target_down(t: int) -> set[str]:
        vals = [ps.instant(s.values, t) for s in scen.select("up", lambda l: l["service"] == a.service)]
        present = [v for v in vals if v is not None]
        if not present or sum(present) == 0:
            return {a.service}
        return set()

    return {"burn_page": burn, "burn_ticket": burn, "error_ratio": error_ratio, "mem_high": mem_high,
            "crashloop": crashloop, "latency_p99": latency_p99, "target_down": target_down}[k]


def timeline(a: AlertSpec, scen: ps.Scenario, for_min: int | None = None) -> tuple[list[set[str]], list[set[str]]]:
    """(condition per minute, firing per minute) for the whole scenario (`for_min` defaults to the oracle's)."""
    cond = condition(a, scen)
    conds = [cond(t) for t in range(scen.minutes + 1)]
    fires = ps.firing_timeline(scen.minutes, lambda t: conds[t], a.for_min if for_min is None else for_min)
    return conds, fires