File size: 11,280 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
"""Agent-facing text: the repo README (team conventions), slo/services.yaml, the instruction (U5, S7)."""

from __future__ import annotations

import random

import yaml

from .alerts import RECORD_PREFIX
from .series import FAMILIES
from .world import Req, World

OPENINGS = {
    "terse": "You own the monitoring repo at {company}.",
    "neutral": "You're on the SRE rotation at {company} this week, and the alerting backlog is yours.",
    "urgent-pager": "You're on call at {company}, and after last night the alerting backlog is today's priority.",
}


def readme(w: World) -> str:
    fmt_note = ("Rule files under `rules/` are **PrometheusRule** manifests (`monitoring.coreos.com/v1`); "
                "rules live under `.spec.groups`. Keep that format for new files."
                if w.axes["rules_format"] == "prometheusrule" else
                "Rule files under `rules/` are plain Prometheus rule files (`groups:` at the top level).")
    return f"""# {w.company} monitoring

Alerting rules and Alertmanager config for {w.company}, reviewed like code.

## Layout
- `rules/*.yml` - recording and alerting rules. {fmt_note}
  Conventionally one file per service (`slo-<service>.yml`), `service-health.yml`, `fleet.yml`, and one file per exporter.
- `alertmanager/alertmanager.yml` - routing tree, receivers, inhibition rules.
- `slo/services.yaml` - the SLO catalog: owning team, SLO target, SLI family and metric names.
- `tests/visible/` - example `promtool test rules` files. They are illustrative, not exhaustive.
- `postmortems/` - incident write-ups that mention alerting follow-ups.
- `bin/af-check` - runs `promtool check rules`, the example tests and `amtool check-config`.

Prometheus scrapes every 1m and evaluates rules every 1m. Every series carries a `service` label
(added by relabeling), plus `namespace`, `pod` and `instance` where they apply.

## Conventions

### SLI recording rules
- Name: `{RECORD_PREFIX}<window>` for windows `5m`, `30m`, `1h`, `6h`.
- Value: the fraction of bad events over the window, aggregated `by (service)` (one series per service, no other labels).
- Bad events by SLI family:
  - `http`: requests to `{FAMILIES['http']['metric']}` with `{FAMILIES['http']['bad_sel']}` (4xx is not an error).
  - `grpc`: calls to `{FAMILIES['grpc']['metric']}` with `{FAMILIES['grpc']['bad_sel']}`.
  - `latency`: requests slower than the SLO threshold: `1 - rate(http_request_duration_seconds_bucket{{le="<threshold>"}}) / rate(http_request_duration_seconds_count)`.
- Several services can share a record name; add a separate rule per SLI family.

### SLO burn-rate alerts (multiwindow, multi-burn-rate; 30-day SLO window)
Error budget `b = 1 - <SLO target>`.

| Tier | Name | Condition | for | severity |
|---|---|---|---|---|
| fast burn | `<ServiceCamel>ErrorBudgetBurnFast` | 1h ratio > 14.4·b **and** 5m ratio > 14.4·b | 2m | page |
| slow burn | `<ServiceCamel>ErrorBudgetBurnSlow` | 6h ratio > 6·b **and** 30m ratio > 6·b | 15m | ticket |

`<ServiceCamel>` is the service name in CamelCase (`checkout-api` → `CheckoutApi`). Burn alerts read the
SLI recording rules for their own service (`{{service="<name>"}}`).
Labels: exactly `severity`, `team` (the owning team) and `slo` (`<service>-availability`, or `<service>-latency` for latency SLOs).

### Service health
`<ServiceCamel>Down` fires when the service has **no healthy scrape target** for 3m: every target reports
`up == 0`, *or* the service has no `up` series at all (its pods are gone). Labels: exactly `severity: page`
and `team: <owning team>`.

### Fleet alerts
Owned by the `{w.platform_team}` team, `severity: warning`, aggregated `by (service)` (one alert per service,
never per pod). Ratio alerts must also require at least 1 request/second over the same window, so a
single failure at 4 a.m. does not page anyone.

### Annotations (every alert)
- `summary`: must name the service with `{{{{ $labels.service }}}}`; only reference labels the expression keeps.
- `runbook_url`: `https://runbooks.{w.domain}/alerts/<AlertName>`.

### Routing policy
Every team has a subtree matching `team="<team>"`:

| severity | receivers |
|---|---|
| page | `pagerduty-<team>` and `slack-<team>` |
| ticket | `jira-sre` and `slack-<team>` |
| warning | `slack-<team>` |

Receivers for every team already exist; do not change receivers, `global`, or add time intervals.
Inhibition rules always use `equal: [service]` so one service's outage never silences another service.

### Change policy
Only touch what a change request asks for. Do not rename, delete, or change the behavior of other rules,
routes or inhibitions, and do not change rule group `interval`, `query_offset` or `limit`.
"""


def services_yaml(w: World) -> str:
    svcs = list(w.services) + ([w.sibling] if w.sibling else [])
    doc = {"services": []}
    for s in svcs:
        d = {"name": s.name, "team": s.team, "slo_target": float(s.target), "sli_family": s.family}
        if s.family == "latency":
            d["latency_threshold_seconds"] = float(s.slo_le)
            d["metrics"] = ["http_request_duration_seconds_bucket", "http_request_duration_seconds_count"]
        else:
            d["metrics"] = [FAMILIES[s.family]["metric"]]
        doc["services"].append(d)
    return yaml.safe_dump(doc, sort_keys=False)


def _svc(w: World, name: str):
    return next((s for s in w.services if s.name == name), None)


def req_text(w: World, r: Req) -> str:
    ex = w.axes["expertise"]
    if r.kind in ("new_alert", "repair") and r.alert.kind in ("burn_page", "burn_ticket") and r.kind == "new_alert":
        a, s = r.alert, _svc(w, r.alert.service)
        fast = a.kind == "burn_page"
        if ex == "novice":
            lw, sw, k, f, sev = ("1h", "5m", "14.4", "2m", "page") if fast else ("6h", "30m", "6", "15m", "ticket")
            return (f"Add `{a.name}` for `{s.name}` (SLO {s.target}): fire when both `{RECORD_PREFIX}{lw}` and "
                    f"`{RECORD_PREFIX}{sw}` for the service exceed {k} × (1 − {s.target}), `for: {f}`, labels "
                    f"`severity: {sev}`, `team: {a.labels['team']}`, `slo: {a.labels['slo']}`, plus `summary` and "
                    f"`runbook_url` annotations per README.md.")
        if ex == "practitioner":
            return (f"`{s.name}` needs its {'fast-burn page' if fast else 'slow-burn ticket'} for SLO {s.target}, "
                    f"following the burn-rate conventions in README.md.")
        return (f"{'Page ' + s.team + ' when `' + s.name + '` is burning its error budget fast' if fast else 'Open a ticket when `' + s.name + '` burns its error budget slowly but steadily'}.")
    if r.kind == "new_alert" and r.alert.kind == "target_down":
        a, s = r.alert, _svc(w, r.alert.service)
        if ex == "novice":
            return (f"Add `{a.name}`: fire when `{s.name}` has no healthy scrape target for 3m, including when its "
                    f"`up` series disappear entirely; labels `severity: page`, `team: {s.team}`; annotations per README.md.")
        if ex == "practitioner":
            return f"Page {s.team} when `{s.name}` is completely down, including when it vanishes from service discovery (README: Service health)."
        return f"Make sure someone is paged if `{s.name}` is totally down, even when nothing is left to scrape."
    if r.kind == "new_alert":
        return f"Add the fleet alert `{r.alert.name}` per README.md."
    if r.kind == "repair":
        a = r.alert
        if r.defect == "B12t":
            return f"`{a.name}` belongs to `{a.labels['team']}` since the reorg; fix its routing labels."
        if r.pm:
            return f"`{a.name}` is misbehaving; see `postmortems/{r.pm}`. Fix the rule, keeping its purpose."
        return (f"`{a.name}` was flagged in the quarterly alert review as not doing what it is for. Audit it against "
                f"README.md and fix it.")
    if r.kind == "recording":
        rc = r.record
        ws = ", ".join(rc["windows"])
        names = ", ".join(f"`{RECORD_PREFIX}{x}`" for x in rc["windows"])
        if r.defect == "new":
            if ex == "novice":
                return (f"Add SLI recording rules {names} for the `{rc['family']}` SLI family (used by `{rc['service']}`), "
                        f"aggregated `by (service)`.")
            return f"Record the `{rc['family']}` SLI error ratios for windows {ws} (README: SLI recording rules)."
        return (f"Dashboards built on {names} show nonsense for `{rc['family']}` services (`{rc['service']}` among them). "
                f"Fix those recording rules.")
    if r.kind == "route":
        t, svc = r.route["team"], r.route["service"]
        msg = {"add": f"Team `{t}` is now on call for `{svc}`, but nothing routes to them yet. Route their alerts per the routing policy.",
               "R01": f"Pages for team `{t}` end up in the catch-all channel.",
               "R02": f"Pages about `{svc}` reach `slack-{t}` but never PagerDuty.",
               "R03": f"Pages for team `{t}` reach PagerDuty but never the team's Slack channel.",
               "R04": f"Tickets for team `{t}` are paging people."}[r.defect]
        if ex == "novice" and r.defect == "add":
            msg += f" Receivers: `pagerduty-{t}`, `slack-{t}`, `jira-sre`."
        return msg
    if r.kind == "inhibit":
        ih = r.inhibit
        tg = ", ".join(f"`{x}`" for x in ih["targets"])
        return {"add": f"While `{ih['source']}` is firing for a service, suppress {tg} for that same service only.",
                "R05": f"When `{ih['source']}` fires, alerts of *other* services are suppressed too. It should only affect its own service.",
                "R06": f"The inhibition that should silence {tg} while `{ih['source']}` fires does not work."}[r.defect]
    raise ValueError(r.kind)


def instruction(w: World, canary: str) -> str:
    rng = random.Random(w.tseed ^ 0x1257)
    lines = [f"<!-- harbor-canary GUID {canary} -->", OPENINGS[w.axes["tone"]].format(company=w.company),
             "The monitoring repo is at `/workspace/monitoring`; its README.md holds the team conventions.", ""]
    if w.old_team:
        moved = ", ".join(f"`{s.name}` → `{s.team}`" for s in w.services[:2] if s.team.startswith(w.old_team))
        lines += [f"Context: team `{w.old_team}` was split. Ownership moved: {moved}. The legacy `{w.old_team}` route stays as it is.", ""]
    lines += ["## Change requests", ""]
    order = list(w.reqs)
    rng.shuffle(order)
    for r in order:
        lines.append(f"- **{r.id}.** {req_text(w, r)}")
    lines += ["", "## Constraints",
              "- Rules live in `rules/*.yml`; Alertmanager config in `alertmanager/alertmanager.yml`.",
              "- Follow README.md for names, labels, annotations, windows and routing. Do not change anything that is not asked for.",
              "- Tools: `promtool`, `amtool`, and `bin/af-check` (syntax checks, the example tests in `tests/visible/`, `amtool check-config`).",
              "- Grading replays hidden traffic scenarios through promtool and amtool; the example tests are not the grading tests.",
              "", "## Done when",
              "Every change request is handled, every rule file loads, and `amtool check-config` passes.", ""]
    return "\n".join(lines)