|
1 | 1 | """Verifies service health after remediation actions.""" |
2 | 2 |
|
| 3 | +import logging |
3 | 4 | import time |
4 | 5 |
|
| 6 | +import httpx |
| 7 | + |
5 | 8 | from autosre.models import RecoveryStatus |
6 | 9 |
|
| 10 | +logger = logging.getLogger(__name__) |
| 11 | + |
| 12 | +DEFAULT_POLL_INTERVAL = 3.0 |
| 13 | +STUB_RECOVERY_SECONDS = 92.0 |
| 14 | + |
7 | 15 |
|
8 | 16 | class RecoveryMonitor: |
9 | 17 | """ |
10 | | - Checks metrics (latency, error rate) to confirm recovery. |
| 18 | + Checks metrics (health endpoint) to confirm recovery. |
11 | 19 |
|
12 | | - Stub: waits briefly then returns RECOVERED for deterministic demo. |
| 20 | + When metrics_url is set: polls GET metrics_url until response has |
| 21 | + status == "healthy" or timeout. Tracks recovery time from action_start_time. |
| 22 | + When metrics_url is empty: stub behavior (short sleep, RECOVERED) for demo/CI. |
13 | 23 | """ |
14 | 24 |
|
| 25 | + def __init__(self, metrics_url: str | None = None) -> None: |
| 26 | + self._metrics_url = (metrics_url or "").strip() |
| 27 | + self._last_recovery_seconds: float = 0.0 |
| 28 | + |
15 | 29 | def verify( |
16 | | - self, incident_id: str, service_name: str, timeout_seconds: float = 120 |
| 30 | + self, |
| 31 | + incident_id: str, |
| 32 | + service_name: str, |
| 33 | + timeout_seconds: float = 120, |
| 34 | + action_start_time: float | None = None, |
17 | 35 | ) -> RecoveryStatus: |
18 | | - """Poll until recovery signals or timeout. Returns recovery status.""" |
19 | | - # Stub: short wait then recovered |
20 | | - time.sleep(1) |
21 | | - return RecoveryStatus.RECOVERED |
| 36 | + """Poll until recovery (status == healthy) or timeout. Returns recovery status.""" |
| 37 | + if not self._metrics_url: |
| 38 | + time.sleep(1) |
| 39 | + self._last_recovery_seconds = STUB_RECOVERY_SECONDS |
| 40 | + return RecoveryStatus.RECOVERED |
| 41 | + |
| 42 | + start = action_start_time if action_start_time is not None else time.monotonic() |
| 43 | + deadline = start + timeout_seconds |
| 44 | + poll_interval = DEFAULT_POLL_INTERVAL |
| 45 | + |
| 46 | + while time.monotonic() < deadline: |
| 47 | + try: |
| 48 | + with httpx.Client(timeout=5.0) as client: |
| 49 | + r = client.get(self._metrics_url) |
| 50 | + r.raise_for_status() |
| 51 | + data = r.json() |
| 52 | + if data.get("status") == "healthy": |
| 53 | + self._last_recovery_seconds = time.monotonic() - start |
| 54 | + logger.info( |
| 55 | + "Recovery verified", |
| 56 | + extra={ |
| 57 | + "incident_id": incident_id, |
| 58 | + "service_name": service_name, |
| 59 | + "recovery_seconds": self._last_recovery_seconds, |
| 60 | + }, |
| 61 | + ) |
| 62 | + return RecoveryStatus.RECOVERED |
| 63 | + except Exception as e: |
| 64 | + logger.debug("Health poll failed: %s", e) |
| 65 | + time.sleep(poll_interval) |
| 66 | + |
| 67 | + self._last_recovery_seconds = timeout_seconds |
| 68 | + logger.warning( |
| 69 | + "Recovery timeout", |
| 70 | + extra={"incident_id": incident_id, "service_name": service_name}, |
| 71 | + ) |
| 72 | + return RecoveryStatus.NOT_RECOVERED |
22 | 73 |
|
23 | 74 | def get_recovery_time_seconds(self) -> float: |
24 | | - """Return elapsed time from action to recovery (stub).""" |
25 | | - return 92.0 |
| 75 | + """Return elapsed time from action to recovery (from last verify run).""" |
| 76 | + return self._last_recovery_seconds |
0 commit comments