feat(alerts): an acknowledgement is on the record, with who made it (#5463)
#5463 "Acknowledgements on record: an event and who did it", step 1 of milestone 499 "Alerts you can trust". The live walk in #5461 found that acknowledging changed only the alert's current state: once it resolved, nothing said who had acknowledged it or when. - alert_events.actor_id (migration 0040): the person behind a transition. - Acknowledging writes a firing → acknowledged event by the acknowledger. - A rule edit, disable or enable that resets an alert carries its actor (web and MCP alike). - The history page notes "acknowledged by X" and "rule disabled by X"; MCP get_alert_history returns `by`. - One usernames() helper in alerts.service, used by the active list, the history page and the MCP tools. - docs/core/alerting.md: the state diagram no longer shows an acknowledged alert re-firing, and says what an event records. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -16,7 +16,7 @@ RULE = SimpleNamespace(metric_name="cpu_pct", operator=AlertOperator(">"), thres
|
||||
def _event(**kw):
|
||||
base = dict(transitioned_at=datetime(2026, 10, 6, tzinfo=timezone.utc), from_state="inactive",
|
||||
to_state="firing", metric_value=97.5, note=None, email_sent=None, email_error=None,
|
||||
webhook_sent=None, webhook_error=None, ansible_run_id=None)
|
||||
webhook_sent=None, webhook_error=None, ansible_run_id=None, actor_id=None)
|
||||
return SimpleNamespace(**{**base, **kw})
|
||||
|
||||
|
||||
@@ -48,3 +48,16 @@ def test_only_attempted_deliveries_are_listed():
|
||||
row = _history_row(_event(email_sent=True, webhook_sent=True), RULE)
|
||||
assert [(label, sent) for label, sent, _ in row["deliveries"]] == [("email", True), ("webhook", True)]
|
||||
assert _history_row(_event(), RULE)["deliveries"] == []
|
||||
|
||||
|
||||
def test_a_transition_a_person_made_says_who():
|
||||
"""#5463: an acknowledgement, and a rule change that reset the alert,
|
||||
name the person; one whose account is gone still reads as a person's."""
|
||||
names = {"u1": "bob"}
|
||||
ack = _event(from_state="firing", to_state="acknowledged", metric_value=None, actor_id="u1")
|
||||
assert _history_row(ack, RULE, names)["notes"] == ["acknowledged by bob"]
|
||||
off = _event(from_state="firing", to_state="resolved", note="rule disabled", actor_id="u1")
|
||||
assert _history_row(off, RULE, names)["notes"] == ["rule disabled by bob"]
|
||||
gone = _event(from_state="firing", to_state="acknowledged", actor_id="u9")
|
||||
assert _history_row(gone, RULE, names)["notes"] == ["acknowledged by a deleted user"]
|
||||
assert _history_row(_event(note="rule enabled"), RULE, names)["notes"] == ["rule enabled"]
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
"""Integration: alert state against real Postgres. An acknowledgement holds
|
||||
while the breach lasts, an alert clears after its OK run, and changing what a
|
||||
rule watches (or disabling it) resets its state with a note on the event."""
|
||||
while the breach lasts and is on the record with who made it (#5463), an alert
|
||||
clears after its OK run, and changing what a rule watches (or disabling it)
|
||||
resets its state with a note and the person on the event."""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
@@ -44,7 +45,7 @@ def test_acknowledge_holds_clear_after_and_reset(app):
|
||||
metric_name="cpu_pct", operator=">", threshold=90,
|
||||
consecutive_failures_required=1, consecutive_ok_required=2)
|
||||
rule = await alert_svc.add_rule(s, fields, created_by=user.id)
|
||||
rule_id = rule.id
|
||||
rule_id, user_id = rule.id, user.id
|
||||
|
||||
async def sample(v):
|
||||
async with app.db_sessionmaker() as s:
|
||||
@@ -62,11 +63,18 @@ def test_acknowledge_holds_clear_after_and_reset(app):
|
||||
.order_by(AlertEvent.transitioned_at))).scalars().all()
|
||||
return [(e.from_state, e.to_state, e.note) for e in rows]
|
||||
|
||||
async def last_actor():
|
||||
async with app.db_sessionmaker() as s:
|
||||
return (await s.execute(select(AlertEvent.actor_id).where(
|
||||
AlertEvent.rule_id == rule_id).order_by(
|
||||
AlertEvent.transitioned_at.desc()).limit(1))).scalar_one()
|
||||
|
||||
await sample(95)
|
||||
assert (await state()).state == AlertStateEnum.firing
|
||||
async with app.db_sessionmaker() as s:
|
||||
async with s.begin():
|
||||
await alert_svc.acknowledge(s, rule_id, None)
|
||||
await alert_svc.acknowledge(s, rule_id, user_id)
|
||||
assert await last_actor() == user_id
|
||||
for _ in range(3):
|
||||
await sample(97) # still breached: the acknowledgement holds
|
||||
assert (await state()).state == AlertStateEnum.acknowledged
|
||||
@@ -75,7 +83,9 @@ def test_acknowledge_holds_clear_after_and_reset(app):
|
||||
await sample(10)
|
||||
st = await state()
|
||||
assert st.state == AlertStateEnum.inactive and st.acknowledged_at is None
|
||||
assert await events() == [("inactive", "firing", None), ("acknowledged", "resolved", None)]
|
||||
assert await events() == [("inactive", "firing", None), ("firing", "acknowledged", None),
|
||||
("acknowledged", "resolved", None)]
|
||||
assert await last_actor() is None # the readings resolved it, not a person
|
||||
|
||||
# Changing the threshold of a firing rule resets it, saying why.
|
||||
await sample(95)
|
||||
@@ -100,9 +110,10 @@ def test_acknowledge_holds_clear_after_and_reset(app):
|
||||
async with app.db_sessionmaker() as s:
|
||||
async with s.begin():
|
||||
r = await s.get(AlertRule, rule_id)
|
||||
await alert_svc.apply_rule_change(s, r, {}, enabled=False)
|
||||
await alert_svc.apply_rule_change(s, r, {}, enabled=False, actor_id=user_id)
|
||||
assert (await state()).state == AlertStateEnum.inactive
|
||||
assert (await events())[-1] == ("firing", "resolved", "rule disabled")
|
||||
assert await last_actor() == user_id
|
||||
|
||||
# Re-enabling starts the watch over: silence is measured from now.
|
||||
async with app.db_sessionmaker() as s:
|
||||
|
||||
Reference in New Issue
Block a user