Files
thor/scripts/record_fixture.py
T

72 lines
3.2 KiB
Python
Raw Normal View History

#!/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')