2026-11-25 23:02:10 -05:00
|
|
|
"""Replay a recorded incident through the real monitor, engine and checks (thor#1, Area 7).
|
|
|
|
|
|
|
|
|
|
Fixtures (tests/fixtures/<name>.json, made by scripts/record_fixture.py) hold raw series and
|
|
|
|
|
log lines for the incident window. The fakes below slice them by time; a query the fixture
|
|
|
|
|
doesn't have fails loudly so a changed check is noticed instead of silently seeing nothing.
|
|
|
|
|
"""
|
|
|
|
|
import json
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
from typing import Dict, List
|
|
|
|
|
|
|
|
|
|
from thor.clients import SourceError
|
|
|
|
|
from thor.config import Settings
|
|
|
|
|
from thor.monitor import Monitor, build_checks
|
|
|
|
|
|
|
|
|
|
from .fakes import Clock, FakeStore
|
|
|
|
|
|
|
|
|
|
FIXTURES = Path(__file__).resolve().parent / 'fixtures'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load(name: str) -> dict:
|
|
|
|
|
return json.loads((FIXTURES / f'{name}.json').read_text())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class _Blindable:
|
|
|
|
|
def __init__(self, name: str, fixture: dict, clock: Clock):
|
|
|
|
|
self.name = name
|
|
|
|
|
self.clock = clock
|
|
|
|
|
self.blind = fixture.get('blind', {}).get(name, [])
|
|
|
|
|
|
|
|
|
|
def _guard(self):
|
|
|
|
|
if any(s <= self.clock.t <= e for s, e in self.blind):
|
|
|
|
|
raise SourceError(self.name, 'HTTP 500 (recorded outage)')
|
|
|
|
|
|
|
|
|
|
def ping(self):
|
|
|
|
|
self._guard()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ReplayPrometheus(_Blindable):
|
|
|
|
|
def __init__(self, fixture, clock):
|
|
|
|
|
super().__init__('prometheus', fixture, clock)
|
|
|
|
|
self.series: Dict[str, List[dict]] = fixture['prometheus']
|
|
|
|
|
self.asked: List[str] = []
|
|
|
|
|
|
|
|
|
|
def query_range(self, expr, start, end, step):
|
|
|
|
|
self._guard()
|
|
|
|
|
self.asked.append(expr)
|
|
|
|
|
if expr not in self.series:
|
|
|
|
|
raise AssertionError(f'fixture has no series for {expr!r}: re-record it')
|
|
|
|
|
return [{'metric': s['metric'], 'values': [(t, v) for t, v in s['values'] if start <= t <= end]}
|
|
|
|
|
for s in self.series[expr]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ReplayLoki(_Blindable):
|
|
|
|
|
def __init__(self, fixture, clock):
|
|
|
|
|
super().__init__('loki', fixture, clock)
|
|
|
|
|
self.streams: Dict[str, list] = fixture['loki']
|
|
|
|
|
|
|
|
|
|
def lines(self, logql, start, end, limit=100, direction='backward'):
|
|
|
|
|
self._guard()
|
|
|
|
|
if logql not in self.streams:
|
|
|
|
|
raise AssertionError(f'fixture has no lines for {logql!r}: re-record it')
|
|
|
|
|
out = [(ts, line, labels) for ts, line, labels in self.streams[logql] if start <= ts <= end]
|
|
|
|
|
out.sort(key=lambda x: x[0], reverse=(direction != 'forward'))
|
|
|
|
|
return out[:limit]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ReplayProxmox(_Blindable):
|
|
|
|
|
def __init__(self, fixture, clock):
|
|
|
|
|
super().__init__('proxmox', fixture, clock)
|
|
|
|
|
self.guests = fixture.get('guests', [])
|
|
|
|
|
|
|
|
|
|
def resources(self, kind=None):
|
|
|
|
|
self._guard()
|
|
|
|
|
return list(self.guests)
|
|
|
|
|
|
|
|
|
|
def cluster_status(self):
|
|
|
|
|
self._guard()
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
|
2026-11-25 23:13:59 -05:00
|
|
|
class ReplayGandalf(_Blindable):
|
|
|
|
|
"""Suppressions as Gandalf would have shown them at the replay's current time."""
|
|
|
|
|
|
|
|
|
|
def __init__(self, fixture, clock):
|
|
|
|
|
super().__init__('gandalf', fixture, clock)
|
|
|
|
|
self.records = fixture.get('suppressions', [])
|
|
|
|
|
|
|
|
|
|
def suppressions(self, hours=24, include_ended=True):
|
|
|
|
|
from thor.checks.maintenance import parse_time
|
|
|
|
|
self._guard()
|
|
|
|
|
now, out = self.clock.t, []
|
|
|
|
|
for r in self.records:
|
|
|
|
|
start, end = parse_time(r['started_at']), parse_time(r.get('ended_at'))
|
|
|
|
|
if start > now or start < now - hours * 3600 - 86400:
|
|
|
|
|
continue
|
|
|
|
|
if end is not None and end > now:
|
|
|
|
|
r = dict(r, ended_at=None, state='active')
|
|
|
|
|
out.append(r)
|
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
2026-11-25 23:02:10 -05:00
|
|
|
class Replay:
|
|
|
|
|
def __init__(self, name: str, settings: Settings = None):
|
|
|
|
|
self.fixture = load(name)
|
|
|
|
|
self.clock = Clock(self.fixture['start'])
|
|
|
|
|
self.store = FakeStore()
|
|
|
|
|
self.settings = settings or Settings()
|
|
|
|
|
self.prometheus = ReplayPrometheus(self.fixture, self.clock)
|
|
|
|
|
sources = {'prometheus': self.prometheus, 'loki': ReplayLoki(self.fixture, self.clock),
|
2026-11-25 23:13:59 -05:00
|
|
|
'proxmox': ReplayProxmox(self.fixture, self.clock),
|
|
|
|
|
'gandalf': ReplayGandalf(self.fixture, self.clock)}
|
2026-11-25 23:02:10 -05:00
|
|
|
self.monitor = Monitor(self.settings, self.store, sources, checks=build_checks(), clock=self.clock)
|
|
|
|
|
self.results = []
|
|
|
|
|
|
|
|
|
|
def run(self, until: float = None):
|
|
|
|
|
until = until or self.fixture['end']
|
2026-11-25 23:13:59 -05:00
|
|
|
down = self.fixture.get('thor_down', [])
|
2026-11-25 23:02:10 -05:00
|
|
|
while self.clock.t <= until:
|
2026-11-25 23:13:59 -05:00
|
|
|
if not any(s <= self.clock.t <= e for s, e in down): # Thor was dark too: no cycle
|
|
|
|
|
self.results.append(self.monitor.cycle())
|
2026-11-25 23:02:10 -05:00
|
|
|
self.clock.advance(self.settings.cycle_seconds)
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
# -- what tests look at ---------------------------------------------------
|
|
|
|
|
def actions(self, kind: str = None) -> List[dict]:
|
|
|
|
|
return [a for a in self.store.actions if kind is None or a['kind'] == kind]
|
|
|
|
|
|
|
|
|
|
def opened(self) -> List[dict]:
|
|
|
|
|
return self.actions('open')
|