File size: 9,684 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
"""The three ADRs a task's README points at (R2/R9, editorial pass E8): Context / Decision / Consequences, an author
and a date, numbered the way that company numbered its decisions. Each comes in three wordings with the same
decision in it; the policy states general rules only (SPEC 6.2), never the trap a ticket is about."""

from __future__ import annotations

import random

from . import variety
from .people import rota
from .repo import FACTORS


def numbers(company) -> dict[str, str]:
    """ADR numbers per company: the scrape-interval decision came first, then SLO alerting, then disks."""
    rng = random.Random(f"adr:{company.name}")
    a = rng.randint(2, 5)
    b = a + rng.randint(2, 6)
    c = b + rng.randint(2, 7)
    return {"scrape": f"{a:04d}", "slo": f"{b:04d}", "disk": f"{c:04d}"}


def files(w) -> dict[str, str]:
    n = w.notes.get("adr") or numbers(w.company)
    return {f"docs/adr/{n['scrape']}-scrape-intervals.md": scrape(w, n["scrape"]),
            f"docs/adr/{n['slo']}-slo-alerting.md": slo(w, n["slo"]),
            f"docs/adr/{n['disk']}-disk-alerts.md": disk(w, n["disk"])}


def _meta(w, key: str, n: str, date: str) -> tuple[str, str]:  # noqa: ARG001
    """(author, date): someone in observability; dates in decision order, different at every company."""
    import datetime as dt
    obs = rota(w, "observability")
    who = obs[int(n) % len(obs)].full
    rng = random.Random(f"adr-dates:{w.company.name}")
    days = sorted(rng.sample(range(0, 800), 3))
    when = {k: (dt.date(2024, 3, 1) + dt.timedelta(days=d)).isoformat() for k, d in zip(("scrape", "slo", "disk"), days)}
    return who, when[key]


def scrape(w, n: str) -> str:
    who, date = _meta(w, "scrape", n, "2025-03-04")
    tables = ["""| job | interval | notes |
|---|---|---|
| app targets (`kubernetes-pods`) | 1m | |
| ingress | 1m | |
| `kafka-exporter` | 1m | was 15s until August; the exporter got too expensive on the big clusters |
| cAdvisor federation from platform | 1m | allowlist in README |
| node, postgres, redis exporters | 1m | |""", """| scrape job | every | |
|---|---|---|
| `kubernetes-pods` (our apps) | 1m | |
| ingress controller | 1m | |
| `kafka-exporter` | 1m | 15s before August, which cost too much on the large clusters |
| federated cAdvisor series (platform) | 1m | see the README allowlist |
| node / postgres / redis exporters | 1m | |""", """- `kubernetes-pods` (app targets): 1m
- ingress: 1m
- `kafka-exporter`: 1m. It ran at 15s until August, when the big clusters made that too expensive.
- cAdvisor, federated from platform: 1m (allowlist in README)
- node, postgres and redis exporters: 1m"""]
    table = variety.house(w, "adr:scrape-table", tables)
    return variety.house(w, "adr:scrape", [
        f"""# ADR {n}: scrape intervals

Status: accepted (last amended in August) · Author: {who} · {date}

## Context

Every job had its own interval and nobody could say which rate windows were safe.

## Decision

The global `scrape_interval` is 1m. Jobs and exceptions:

{table}

## Consequences

Changing an interval means checking the rate windows of every rule that reads the job.
""",
        f"""# {n}. Scrape intervals

* Status: accepted, amended in August
* Deciders: observability, platform
* Date: {date} ({who})

## Context

Prometheus memory doubled in a year, mostly from fast scrapes nobody needed.

## Decision

One interval for everything, 1m, set globally. Per job:

{table}

## Consequences

- A rule's rate window has to fit the interval of the job it reads. Whoever changes an interval checks those
  windows.
""",
        f"""# ADR {n}: one scrape interval

Accepted. Written by {who}, {date}; the kafka-exporter row was changed in August.

## Context

Mixed intervals (10s, 15s, 30s, 1m) made every new rule a guessing game about its window.

## Decision

Global `scrape_interval: 1m`. Current state per job:

{table}

## Consequences

Any change to a job's interval comes with a look at the windows of the rules reading that job.
"""])


def slo(w, n: str) -> str:
    who, date = _meta(w, "slo", n, "2025-06-17")
    f30, s30 = FACTORS[30]
    latency = [
        "A latency SLI counts requests slower than the objective. Use the bucket boundary nearest the objective "
        "without going over it; if the objective is a boundary, use it. Latency SLOs are the fraction of requests "
        "slower than the objective, never a quantile.",
        "For latency, the SLI is the share of requests slower than the objective, computed on the histogram bucket "
        "closest to the objective that doesn't exceed it (the objective's own boundary when there is one). A latency "
        "SLO is always that fraction; we don't write quantile SLOs.",
        "Latency SLOs are written as the fraction of requests slower than the objective, not as a quantile. Count the "
        "slow ones against the nearest bucket boundary at or below the objective; when the objective is itself a "
        "boundary, that boundary."]
    floor = [
        "A burn rate means nothing when there is no traffic: when a service stops getting requests its ratios go "
        "empty and the burn alerts resolve. Losing the traffic altogether is the floor alert's job "
        "(`traffic_floor_rps` in the catalog, measured on the service's own request counter).",
        "Burn alerts can't see a service that gets no requests at all: the ratios have nothing to divide and the "
        "alerts resolve. That case belongs to the traffic floor (`traffic_floor_rps` in the catalog, on the "
        "service's own request counter).",
        "Without traffic there is no burn rate; the ratios go empty and every burn alert resolves. Missing traffic "
        "is covered by the floor alert instead, against `traffic_floor_rps` in the catalog measured on the "
        "service's own request counter."]
    v = variety.house(w, "adr:slo", [0, 1, 2])
    body = [
        f"""# ADR {n}: SLO alerting

Status: accepted · {who}, {date}

## Context

Threshold alerts on error ratios paged for blips and stayed quiet through slow leaks.

## Decision

We page when a service has burned 2% of its error budget within an hour, and we don't page on a blip, so the
last 5 minutes have to agree. Slow burns (5% in 6 hours, confirmed over 30 minutes) open a ticket. The budget is
whatever `slo/services.yaml` says the window is: most services are on 30 days, batch and low-volume services
are on 7.

For a 30-day window that works out to {f30}x over 1h and 5m for the page and {s30}x over 6h and 30m for the
ticket. Other windows scale the same way (budget fraction × window hours / alert window hours).

## Consequences

{floor[0]}

{latency[0]}
""",
        f"""# {n}. Multi-window burn-rate alerts

* Status: accepted
* Author: {who}
* Date: {date}

## Context

Every team had its own error threshold, and most of them had been picked after an incident.

## Decision

- Page: 2% of the error budget gone within an hour, with the last 5 minutes agreeing (no paging on a blip).
- Ticket: 5% of the budget gone in 6 hours, confirmed over the last 30 minutes.
- The budget's window is the one in `slo/services.yaml`; 30 days for most services, 7 for batch and low-volume ones.
- On a 30-day window: {f30}x over 1h and 5m (page), {s30}x over 6h and 30m (ticket). Any other window scales the
  same way: budget fraction × window hours / alert window hours.

## Consequences

{floor[1]}

{latency[1]}
""",
        f"""# ADR {n}: alerting on SLOs

Accepted {date}. Author: {who}.

## Context

We want to be woken up for budget we are actually losing, not for every spike.

## Decision

A page means 2% of the error budget burned in the last hour, and the last 5 minutes still burning (so a blip
doesn't page). A ticket means 5% burned over 6 hours, still burning over the last 30 minutes. How much budget a
service has comes from its window in `slo/services.yaml`: 30 days for most, 7 for batch and low-volume services.
With 30 days that is {f30}x over 1h and 5m to page and {s30}x over 6h and 30m for the ticket; for other windows,
budget fraction × window hours / alert window hours.

## Consequences

{floor[2]}

{latency[2]}
"""][v]
    return body


def disk(w, n: str) -> str:
    who, date = _meta(w, "disk", n, "2025-10-08")
    return variety.house(w, "adr:disk", [
        f"""# ADR {n}: disk alerts

Status: accepted ({date}) · by {who}

## Context

Disk predictions paged for every backup and compaction.

## Decision

Open a ticket when a volume is under 15% free and on course to fill within 4 hours. Page when it's under 5% free
for 5 minutes.

## Consequences

A prediction alone is not a reason to page: backups, compactions and bulk loads all look like a volume filling up
for an hour.
""",
        f"""# {n}. When disks page

* Status: accepted
* Author: {who} ({date})

## Context

The old rule paged on a 4-hour fill prediction, which every nightly job set off.

## Decision

- Ticket: less than 15% free and predicted to fill within 4 hours.
- Page: less than 5% free, for 5 minutes.

## Consequences

Predictions never page on their own. Backups, compactions and bulk loads look like a filling volume for an hour
at a time, and that is not worth waking anyone.
""",
        f"""# ADR {n}: disk space

Accepted on {date}; {who}.

## Context

Too many disk pages at night, almost all of them for jobs that free the space again on their own.

## Decision

Below 15% free with the volume set to fill within 4 hours: ticket. Below 5% free for 5 minutes: page.

## Consequences

Nothing pages on a prediction alone, since a backup, a compaction or a bulk load makes a volume look like it is
filling for an hour.
"""])