Files
thor/scripts/record_fixture.py
T
jaredandClaude Opus 5.5 983ba27386
CI / Lint (flake8, house style) (pull_request) Successful in 50s
CI / Test (pytest) (pull_request) Successful in 52s
CI / Security (bandit, pip-audit) (pull_request) Successful in 1m1s
CI / Integration (MariaDB) (pull_request) Successful in 1m40s
CI / Scan (secrets, workflows, shell, patterns) (pull_request) Successful in 1m15s
CI / Deploy (pull_request) Skipped
Engine, memory check, symptom checks, outbox, T1 replay
- thor/engine.py: one incident per root cause. New causes held one cycle, symptoms attached
  to the highest cause they relate to (same host, or cluster-wide effects for causes that
  can stall the cluster), priority only rises, rate-limited refresh, cleared after 15 min.
- thor/checks/memory.py: node_vmstat_oom_kill + kernel oom-kill lines; global vs cgroup,
  victim and CT, trigger cpuset; P3 / P2 (loop) / P1 (Ceph or other hosts stalling).
- thor/checks/symptoms.py: scrape timeouts, pvestatd timeouts, Ceph alerts switching on.
- thor/outbox.py: live actions to Tinker with backoff; shadow actions are only recorded.
- Migration 2: incidents, actions, observations.
- PromQL/LogQL regex escaping fixed (found by recording against the real Prometheus).
- tests/replay.py + tests/fixtures/t1.json: 2026-11-23 replayed through the real monitor:
  exactly one ticket (P1 from the start: pvestatd timed out on three other hosts with the
  first kill), 15 symptoms as Also seen, cleared 06:18, Loki's 06:00 outage delays only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 23:02:10 -05:00

73 lines
3.2 KiB
Python

#!/usr/bin/env python3
"""Record a replay fixture from the real Prometheus and Loki (read-only).
Usage: .venv/bin/python scripts/record_fixture.py t1
Each fixture holds the raw series and log lines Thor's checks query, for the whole incident
window plus margin. Replays (tests/replay.py) slice them by time, so a check can change its
windows without re-recording; a new query needs a re-record. Only what Thor parses is kept:
stream labels are cut down to host/job/unit/transport, and Ceph alert series that never
switched on are dropped.
"""
import json
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT))
from thor.checks import memory, symptoms # noqa: E402
from thor.clients.loki import LokiClient # noqa: E402
from thor.clients.prometheus import PrometheusClient # noqa: E402
from thor.hosts import HostRegistry # noqa: E402
PROM = 'http://10.10.10.48:9090'
LOKI = 'http://10.10.10.69:3100'
KEEP_LABELS = ('host', 'job', 'unit', 'transport')
SPECS = {
't1': {
'title': '2026-11-23 OOM loop on compute-storage-01 (CT 154), Tinker 615797777',
'start': 1795410000.0, # 2026-11-23 05:00:00 UTC
'end': 1795416000.0, # 2026-11-23 06:40:00 UTC
'oom_hosts': ['compute-storage-01'],
# Loki answered 500 around the 06:01 stall (Tinker 615797777).
'blind': {'loki': [[1795413600.0, 1795413690.0]]},
'guests': [{'vmid': 154, 'name': 'pelican-wings-02', 'type': 'lxc'}],
},
}
def record(name: str) -> dict:
spec = SPECS[name]
start, end = spec['start'] - 600, spec['end'] + 600
prom, loki, hosts = PrometheusClient(PROM, timeout=60), LokiClient(LOKI, timeout=60), HostRegistry()
exprs = [memory.COUNTER_QUERY, symptoms.CEPH_QUERY] + list(symptoms.ScrapeCheck().queries(hosts).values())
out = {'name': name, 'title': spec['title'], 'start': spec['start'], 'end': spec['end'],
'blind': spec.get('blind', {}), 'guests': spec.get('guests', []),
'prometheus': {}, 'loki': {}}
for expr in exprs:
series = prom.query_range(expr, start, end, 30)
if expr == symptoms.CEPH_QUERY:
series = [s for s in series if any(v >= 1 for _, v in s['values'])]
out['prometheus'][expr] = [{'metric': s['metric'], 'values': s['values']} for s in series]
logql = [symptoms.PVESTATD_QUERY] + [memory.kernel_query(hosts.loki_names(h)) for h in spec['oom_hosts']]
for q in logql:
lines, t = [], start
while t < end: # hour by hour, so no window hits the line limit
chunk = loki.lines(q, t, min(t + 3600, end), limit=5000, direction='forward')
lines += [[ts, line, {k: v for k, v in labels.items() if k in KEEP_LABELS}] for ts, line, labels in chunk]
t += 3600
out['loki'][q] = lines
return out
if __name__ == '__main__':
name = sys.argv[1]
data = record(name)
path = ROOT / 'tests' / 'fixtures' / f'{name}.json'
path.write_text(json.dumps(data, indent=1, sort_keys=True) + '\n')
n_series = sum(len(v) for v in data['prometheus'].values())
n_lines = sum(len(v) for v in data['loki'].values())
print(f'wrote {path}: {n_series} series, {n_lines} log lines')