Probes log locally only while the hub is unreachable; the hub confirms silences with that log
Probe: no access log, nothing written while reports get through. When the hub can't be reached, failed reports go to data/outage.jsonl and unsent results to data/backlog.jsonl (survives restarts). The first report that gets through uploads both and they are deleted. Hub: a probe that misses reports gets a "Not reporting" incident. When it reports again, the runs from its outage log become confirmed "Hub unreachable" incidents; the rest of the silence (probe off or restarting) is dropped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
462525b1e2
commit
be727949c1
13 changed files with 449 additions and 32 deletions
127
tests/test_outages.py
Normal file
127
tests/test_outages.py
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
"""Probes that stop reporting: the hub opens an incident, the probe's outage log settles it."""
|
||||
|
||||
import asyncio
|
||||
import time
|
||||
|
||||
import httpx
|
||||
|
||||
from app.hub import outages
|
||||
from app.hub.db import db
|
||||
from tests.test_probes import add, send
|
||||
|
||||
|
||||
def go_quiet(probe_id, seconds):
|
||||
"""Pretend the probe last reported `seconds` ago and let the hub notice."""
|
||||
with db() as con:
|
||||
con.execute("UPDATE probes SET last_seen = ? WHERE id = ?", (time.time() - seconds, probe_id))
|
||||
outages.open_silences(con)
|
||||
|
||||
|
||||
def report_incidents(hub, probe_id):
|
||||
inc = hub.get("/api/health/incidents", params={"range": "24h", "probe": probe_id}).json()["incidents"]
|
||||
return [i for i in inc if i["kind"] == "report"]
|
||||
|
||||
|
||||
def test_silent_probe_is_an_ongoing_incident_then_dropped_when_it_was_off(hub):
|
||||
p = add(hub, "attic")
|
||||
send(hub, p["token"])
|
||||
go_quiet(p["id"], 3600)
|
||||
[i] = report_incidents(hub, p["id"])
|
||||
assert i["ongoing"] and not i["confirmed"] and i["type"] == "down"
|
||||
with db() as con:
|
||||
assert outages.open_silences(con) == 0 # one per silence, not one per check
|
||||
send(hub, p["token"]) # back, with no outage log: it was switched off
|
||||
assert report_incidents(hub, p["id"]) == []
|
||||
|
||||
|
||||
def test_outage_log_confirms_the_logged_part_only(hub):
|
||||
p = add(hub, "attic")
|
||||
send(hub, p["token"])
|
||||
go_quiet(p["id"], 3600)
|
||||
now = time.time()
|
||||
# Failed reports for 20 minutes, then the probe was off, then up and failing until now.
|
||||
runs = [{"start": now - 3500, "end": now - 2300, "failures": 40, "error": "timeout"},
|
||||
{"start": now - 600, "end": now - 30, "failures": 20, "error": "connection refused"}]
|
||||
send(hub, p["token"], outages=runs)
|
||||
inc = sorted(report_incidents(hub, p["id"]), key=lambda i: i["start"])
|
||||
assert len(inc) == 2 and all(i["confirmed"] and not i["ongoing"] for i in inc)
|
||||
assert inc[0]["start"] == now - 3530 and inc[0]["end"] == now - 2300 and inc[0]["failed"] == 40
|
||||
assert inc[1]["end"] >= now and inc[1]["error"] == "connection refused"
|
||||
|
||||
|
||||
def test_hub_restart_gap_is_settled_without_an_open_incident(hub):
|
||||
p = add(hub, "attic")
|
||||
send(hub, p["token"])
|
||||
with db() as con: # the hub was down too, so it never opened an incident
|
||||
con.execute("UPDATE probes SET last_seen = ? WHERE id = ?", (time.time() - 900, p["id"]))
|
||||
now = time.time()
|
||||
send(hub, p["token"], outages=[{"start": now - 880, "end": now - 25, "failures": 29, "error": "timeout"}])
|
||||
[i] = report_incidents(hub, p["id"])
|
||||
assert i["confirmed"] and i["failed"] == 29
|
||||
|
||||
|
||||
def test_short_hiccup_is_not_an_incident(hub):
|
||||
p = add(hub, "attic")
|
||||
send(hub, p["token"])
|
||||
now = time.time()
|
||||
send(hub, p["token"], outages=[{"start": now - 20, "end": now - 20, "failures": 1, "error": "timeout"}])
|
||||
assert report_incidents(hub, p["id"]) == []
|
||||
|
||||
|
||||
def test_deleting_a_probe_removes_its_outages(hub):
|
||||
p = add(hub, "attic")
|
||||
send(hub, p["token"])
|
||||
go_quiet(p["id"], 3600)
|
||||
assert hub.delete(f"/api/probes/{p['id']}").status_code == 204
|
||||
with db() as con:
|
||||
assert con.execute("SELECT COUNT(*) FROM probe_outages").fetchone()[0] == 0
|
||||
|
||||
|
||||
def test_probe_writes_nothing_while_the_hub_answers_and_logs_while_it_doesnt(tmp_path, monkeypatch):
|
||||
from app.probe import outage, server
|
||||
|
||||
monkeypatch.setattr(server, "state", dict(server.state)) # probe_loop writes module state
|
||||
calls = {"n": 0, "bodies": []}
|
||||
plan = ["ok", "down", "down", "ok", "ok"]
|
||||
|
||||
def handler(request):
|
||||
i = calls["n"]
|
||||
calls["n"] += 1
|
||||
if plan[min(i, len(plan) - 1)] == "down":
|
||||
raise httpx.ConnectError("no route to host")
|
||||
calls["bodies"].append(request.read())
|
||||
return httpx.Response(200, json={"interval": 5, "peers": [], "dns": [], "http": []})
|
||||
|
||||
real_client = httpx.AsyncClient
|
||||
monkeypatch.setattr(server.httpx, "AsyncClient", lambda **kw: real_client(transport=httpx.MockTransport(handler), **kw)
|
||||
if kw.get("timeout") == 10 else real_client(**kw))
|
||||
|
||||
async def fake_checks(cfg, cycle):
|
||||
return [{"ts": time.time(), "target": "gateway", "label": "gw", "kind": "lan", "method": "icmp", "ok": True}]
|
||||
monkeypatch.setattr(server, "run_checks", fake_checks)
|
||||
monkeypatch.setattr(server.random, "uniform", lambda a, b: 0)
|
||||
seen = []
|
||||
|
||||
async def fake_sleep(_):
|
||||
files = sorted(p.name for p in tmp_path.iterdir())
|
||||
seen.append(files)
|
||||
if len(seen) > len(plan):
|
||||
raise asyncio.CancelledError
|
||||
monkeypatch.setattr(server.asyncio, "sleep", fake_sleep)
|
||||
monkeypatch.setenv("HUB_URL", "http://hub")
|
||||
monkeypatch.setenv("PROBE_TOKEN", "t")
|
||||
try:
|
||||
asyncio.run(server.probe_loop())
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
# seen[0] is the start-up delay; then one entry per round, after its report.
|
||||
assert seen[1] == ["probe-config.json"] # ok: only the config cache
|
||||
assert seen[2] == seen[3] == ["backlog.jsonl", "outage.jsonl", "probe-config.json"]
|
||||
assert seen[4] == seen[5] == ["probe-config.json"] # delivered and cleaned up
|
||||
import json
|
||||
first_back = json.loads(calls["bodies"][1])
|
||||
assert len(first_back["measurements"]) == 3 # nothing lost
|
||||
[run] = first_back["outages"]
|
||||
assert run["failures"] == 2 and "no route" in run["error"]
|
||||
assert json.loads(calls["bodies"][2])["outages"] == []
|
||||
assert not outage.active()
|
||||
Loading…
Add table
Add a link
Reference in a new issue