File size: 9,183 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 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 | """Reference evaluator: the Prometheus semantics that hidden checks rely on, re-implemented exactly.
Every expected value in a hidden check comes from here, never from oracle rule text. The gate then
proves the oracle against real promtool, so a disagreement between this module and Prometheus is
caught before a task ships.
Semantics mirrored (Prometheus 3.x):
- range selectors are left-open: samples with ts in (t - w, t]
- instant lookback is 5m, left-open, and a `stale` marker ends a series immediately
- rate/increase use `extrapolatedRate` (counter resets, zero-clamp, 1.1x gap threshold)
- histogram_quantile uses linear interpolation inside the bucket that holds the rank
- the alert `for` state machine: ActiveAt is the first eval with the condition true; the alert is
firing at eval t when t - ActiveAt >= for (for = 0 fires immediately)
"""
from __future__ import annotations
from dataclasses import dataclass, field
from fractions import Fraction as F
from typing import Callable, Iterable
STALE = "stale"
LOOKBACK_MIN = 5
@dataclass
class Series:
metric: str
labels: dict[str, str]
values: list # per minute: int, None (missing sample) or STALE
def selector(self) -> str:
inner = ",".join(f'{k}="{v}"' for k, v in sorted(self.labels.items()))
return f"{self.metric}{{{inner}}}"
@dataclass
class Scenario:
"""A set of input series evaluated for `minutes` minutes (sample index == minute)."""
name: str
series: list[Series] = field(default_factory=list)
@property
def minutes(self) -> int:
return max(len(s.values) for s in self.series) - 1
def select(self, metric: str, pred: Callable[[dict], bool] | None = None) -> list[Series]:
return [s for s in self.series if s.metric == metric and (pred is None or pred(s.labels))]
# ---------------------------------------------------------------------------- range functions
def _window(values: list, t: int, w: int) -> list[tuple[int, int]]:
"""Samples with minute index in (t-w, t], excluding missing and stale samples."""
lo = t - w
out = []
for m in range(max(lo + 1, 0), min(t, len(values) - 1) + 1):
v = values[m]
if v is None or v == STALE:
continue
out.append((m, v))
return out
def extrapolated(values: list, t: int, w: int, is_rate: bool = True) -> F | None:
"""Prometheus extrapolatedRate for a counter, in per-second units when is_rate (minutes → seconds)."""
pts = _window(values, t, w)
if len(pts) < 2:
return None
result = F(pts[-1][1] - pts[0][1])
prev = pts[0][1]
for _, v in pts[1:]:
if v < prev:
result += prev
prev = v
first_t, first_v = pts[0]
last_t = pts[-1][0]
range_start, range_end = F(t - w) * 60, F(t) * 60
dur_start = F(first_t) * 60 - range_start
dur_end = range_end - F(last_t) * 60
sampled = F(last_t - first_t) * 60
avg = sampled / (len(pts) - 1)
if result > 0 and first_v >= 0:
dur_zero = sampled * (F(first_v) / result)
if dur_zero < dur_start:
dur_start = dur_zero
threshold = avg * F(11, 10)
interval = sampled
interval += dur_start if dur_start < threshold else avg / 2
interval += dur_end if dur_end < threshold else avg / 2
factor = interval / sampled
if is_rate:
factor /= F(w * 60)
return result * factor
def instant(values: list, t: int) -> int | None:
"""Most recent sample within the 5m lookback, or None if absent/stale."""
for m in range(min(t, len(values) - 1), max(t - LOOKBACK_MIN, -1), -1):
v = values[m]
if v is None:
continue
return None if v == STALE else v
return None
def sum_rate_by(series: Iterable[Series], by: str, t: int, w: int, is_rate: bool = True) -> dict[str, F]:
out: dict[str, F] = {}
for s in series:
r = extrapolated(s.values, t, w, is_rate)
if r is None:
continue
key = s.labels.get(by, "")
out[key] = out.get(key, F(0)) + r
return out
def ratio_by(scen: Scenario, metric: str, bad: Callable[[dict], bool], t: int, w: int,
total_metric: str | None = None, good: Callable[[dict], bool] | None = None) -> dict[str, F]:
"""sum by (service)(rate(bad[w])) / sum by (service)(rate(total[w])).
With `good`, the numerator is instead total - good (latency SLIs: 1 - fast/total).
"""
total = sum_rate_by(scen.select(total_metric or metric), "service", t, w)
if good is not None:
num = sum_rate_by(scen.select(metric, good), "service", t, w)
return {k: (v - num.get(k, F(0))) / v for k, v in total.items() if v != 0 and k in num}
num = sum_rate_by(scen.select(metric, bad), "service", t, w)
return {k: num[k] / v for k, v in total.items() if v != 0 and k in num}
def histogram_quantile(q: F, buckets: list[tuple[F, F]]) -> F | None:
"""buckets: (upper_bound, cumulative_count) sorted by bound, last bound is +Inf (None)."""
if len(buckets) < 2 or buckets[-1][0] is not None:
return None
total = buckets[-1][1]
if total == 0:
return None
rank = q * total
b = next((i for i in range(len(buckets) - 1) if buckets[i][1] >= rank), len(buckets) - 1)
if b == len(buckets) - 1:
return buckets[-2][0]
upper = buckets[b][0]
if b == 0 and upper <= 0:
return upper
start, count = F(0), buckets[b][1]
if b > 0:
start = buckets[b - 1][0]
count -= buckets[b - 1][1]
rank -= buckets[b - 1][1]
if count == 0:
return upper
return start + (upper - start) * (rank / count)
def quantile_by_service(scen: Scenario, metric: str, q: F, t: int, w: int) -> dict[str, F]:
per: dict[str, dict] = {}
for s in scen.select(metric):
r = extrapolated(s.values, t, w)
if r is None:
continue
le = s.labels["le"]
bound = None if le == "+Inf" else F(le)
d = per.setdefault(s.labels["service"], {})
d[bound] = d.get(bound, F(0)) + r
out = {}
for svc, d in per.items():
finite = sorted(k for k in d if k is not None)
bl = [(k, d[k]) for k in finite] + ([(None, d[None])] if None in d else [])
v = histogram_quantile(q, bl)
if v is not None:
out[svc] = v
return out
# ---------------------------------------------------------------------------- alert state machine
def firing_timeline(minutes: int, cond: Callable[[int], set[str]], for_min: int) -> list[set[str]]:
"""firing[t] = services whose alert is firing at eval t (evals every minute from 0)."""
active_since: dict[str, int] = {}
out = []
for t in range(minutes + 1):
now = cond(t)
for svc in list(active_since):
if svc not in now:
del active_since[svc]
for svc in now:
active_since.setdefault(svc, t)
out.append({s for s, a in active_since.items() if t - a >= for_min})
return out
def near(value: F, threshold: F, rel: F = F(1, 10**6)) -> bool:
"""True when a value is too close to its threshold for float-vs-exact agreement to be safe."""
return abs(value - threshold) <= abs(threshold) * rel
# ---------------------------------------------------------------------------- promtool encoding
def encode_values(values: list) -> str:
"""Encode per-minute samples in promtool expanding notation (runs of constant delta)."""
parts: list[str] = []
i, n = 0, len(values)
while i < n:
v = values[i]
if v is None or v == STALE:
j = i
while j + 1 < n and values[j + 1] == v:
j += 1
run = j - i + 1
if v is None: # promtool: '_xN' is N missing samples
parts.append("_" if run == 1 else f"_x{run}")
else:
parts.extend(["stale"] * run)
i = j + 1
continue
j = i
if i + 1 < n and isinstance(values[i + 1], int):
d = values[i + 1] - v
j = i + 1
while j + 1 < n and isinstance(values[j + 1], int) and values[j + 1] - values[j] == d:
j += 1
k = j - i
parts.append(f"{v}{'+' if d >= 0 else '-'}{abs(d)}x{k}")
else:
parts.append(str(v))
i = j + 1
return " ".join(parts)
def decode_values(text: str) -> list:
"""Inverse of encode_values for the subset it emits (used by tests)."""
out: list = []
for tok in text.split():
if tok == "stale":
out.append(STALE)
elif tok.startswith("_"):
out.extend([None] * (int(tok[2:]) if "x" in tok else 1))
elif "x" in tok:
head, cnt = tok.split("x")
idx = next((i for i in range(1, len(head)) if head[i] in "+-"), None)
if idx is None:
start, d = int(head), 0
else:
start, d = int(head[:idx]), int(head[idx + 1:]) * (1 if head[idx] == "+" else -1)
out.extend(start + d * k for k in range(int(cnt) + 1))
else:
out.append(int(tok))
return out
|