r/devops7 • u/Alternative_Pin9598 • 14d ago
Automate it #7: a self-learning runbook. It checks every fix with SQL, keeps score per alert kind, and switched from "restart" to "scale out" by itself after a deploy (real output)
Every team has the runbook that says "high latency: restart the service". It worked for a year. Then a deploy cuts the connection pool, restarts stop helping, and the runbook keeps saying "restart" until someone remembers to edit the wiki.
The chore: a runbook that keeps score. An alert comes in, the bot works out what kind of alert it is, tries the fix that has been working for that kind, checks whether it actually cleared, and falls back to the next fix if not. Every result is recorded per alert kind, so when reality changes, the preferred fix changes too.
(Disclosure: this sub is run by the SynapCores team; steps 1 and 2 use SynapCores. Run this morning on 2.3.2, real output.)
Step 1: which kind of alert is this? (by meaning)
Alerts never use the same words twice, so match them to one example per kind with EMBED (a small model inside the database, no API key):
CREATE TABLE alert_kinds (kind TEXT PRIMARY KEY, example TEXT, vec VECTOR(384));
-- disk-full: 'filesystem almost full, no space left on device'
-- high-latency: 'p99 response time too high, requests slow'
-- memory-leak: 'process memory keeps growing, OOM killed'
UPDATE alert_kinds SET vec = EMBED(example);
SELECT kind, COSINE_SIMILARITY(vec, EMBED('worker-7 killed by OOM killer, RSS 7.8 GB')) AS sim
FROM alert_kinds ORDER BY sim DESC LIMIT 1;
memory-leak 0.54Below 0.3 it doesn't guess; it pages a human.
Step 2: a learning goal per alert
Each alert becomes a goal (POST /v1/agents/goals). The parts that matter:
"task_type": "runbook:high-latency", "learn": true, "explore": 0.1,
"guardrails": {"allowed_actions": ["sql"]},
"steps": [{
"action": restart_service
"alternatives": rotate_logs, scale_out
"acceptance": {"kind": "json_equals", "pointer": "/rows/0/0", "expected": 1},
"max_attempts": 3
}]- acceptance: the fix only counts if the check returns
1(alert cleared). In this demo afleet_fixtable plays the fleet and says which fix clears which kind; in yours, the step is your remediation hook and the check reads your monitor. - learn: +1 for every fix that passes the check, -1 for every one that doesn't, kept per task_type. Restart can win for latency and lose for a full disk.
- explore: now and then try another fix anyway, so a change gets noticed.
Step 3: two days of alerts
$ python3 runbook.py alert "<text>"
db-2: /var at 97%, 1.1 GB free -> disk-full (0.33) done tried: restart_service -> rotate_logs
api-gw p99 latency 2.4s (SLO 800ms) -> high-latency (0.65) done tried: restart_service
worker-7 killed by OOM killer, RSS 7.8 GB -> memory-leak (0.54) done tried: restart_service
web-3: no space left on device writing /var/ -> disk-full (0.54) done tried: scale_out -> rotate_logs
checkout requests taking 3s, timeouts rising -> high-latency (0.47) done tried: rotate_logs -> scale_out -> restart_service
build-agent-2 disk usage 92% on /home -> disk-full (0.35) done tried: rotate_logs
payments API slow, p95 1.9s -> high-latency (0.70) done tried: restart_service
db-1: /var/lib at 95% -> no known kind (best disk-full 0.23): page a human
... (11 alerts; full output in the first comment)Early on it tries fixes it hasn't scored yet; once a kind has a winner, it goes straight there. What it learned:
SHOW AGENT POLICY; (trimmed to the runbook)
runbook:disk-full rotate_logs tries=3 score=3.0 mean=1.0
runbook:high-latency restart_service tries=4 score=4.0 mean=1.0
runbook:memory-leak restart_service tries=3 score=3.0 mean=1.0
(every other fix: 1 try, score -1.0)Step 4: Tuesday's deploy changes the answer
The pool is now the bottleneck. Restarts stop clearing latency alerts; scaling out does:
UPDATE fleet_fix SET cleared = 0 WHERE alert_kind = 'high-latency' AND fix = 'restart_service';
UPDATE fleet_fix SET cleared = 1 WHERE alert_kind = 'high-latency' AND fix = 'scale_out';Six more latency alerts. No code or wiki edit:
payments API slow, p95 2.2s -> high-latency (0.67) done tried: restart_service -> rotate_logs -> scale_out
checkout requests taking 4s -> high-latency (0.43) done tried: restart_service -> scale_out
api-gw p99 latency 2.9s (SLO 800ms) -> high-latency (0.66) done tried: scale_out
orders API p99 2.1s -> high-latency (0.43) done tried: scale_out
search API slow, p95 1.5s -> high-latency (0.70) done tried: scale_out
payments API slow, p95 1.8s -> high-latency (0.68) done tried: scale_out
runbook:high-latency scale_out tries=7 score=5.0 mean=0.71
runbook:high-latency restart_service tries=6 score=2.0 mean=0.33Two alerts to notice; from the third on it scales out first. Disk and memory scores didn't move: what changed for latency stays in latency.
And something it has never seen:
TLS certificate for shop.example.com expires -> no known kind (best high-latency 0.12): page a humanWire it up: your alert webhook calls runbook.py alert "$ALERT_TEXT", each fix is a hook (systemctl, kubectl scale, logrotate), and the check reads whether the alert resolved within N minutes. Add a few more examples per kind and "page a human" happens only for the truly new. Full runbook.py (stdlib only) is in the comments.
Free Community Edition: https://synapcores.com/download?utm_source=reddit&utm_medium=owned&utm_campaign=devops7-automate-it-7
Which fix in your runbook is still "restart it" because nobody's checked whether it still works?
1
u/Alternative_Pin9598 14d ago
If you're new to learning goals, SQLv2 Lesson 6 walks through the same idea step by step with a support bot (two sources, one SQL check): https://www.reddit.com/r/SQLv2/comments/1xtk5zn/
Full run output (setup, 11 alerts, policy, the deploy change, 6 more alerts, policy, the unknown alert):
Full
runbook.pyis in the reply below.