-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalert_manager.py
More file actions
282 lines (230 loc) · 10.7 KB
/
Copy pathalert_manager.py
File metadata and controls
282 lines (230 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/env python3
import time
import uuid
from typing import Dict, List, Optional, Set
from dataclasses import dataclass, field
from collections import defaultdict
from enum import Enum
class AlertSeverity(Enum):
"""Enumeration of alert severity levels."""
LOW = 1
MEDIUM = 2
HIGH = 3
CRITICAL = 4
class AlertStatus(Enum):
"""Enumeration of alert statuses."""
FIRING = "firing"
RESOLVED = "resolved"
SUPPRESSED = "suppressed"
@dataclass
class Alert:
"""Represents an alert with all its properties."""
id: str = field(default_factory=lambda: str(uuid.uuid4()))
title: str = ""
description: str = ""
severity: AlertSeverity = AlertSeverity.MEDIUM
status: AlertStatus = AlertStatus.FIRING
source: str = ""
tags: Set[str] = field(default_factory=set)
timestamp: float = field(default_factory=time.time)
escalation_count: int = 0
rule_id: Optional[str] = None
def __hash__(self) -> int:
"""Make Alert hashable based on its ID."""
return hash(self.id)
def __eq__(self, other) -> bool:
"""Compare alerts based on their ID."""
if not isinstance(other, Alert):
return False
return self.id == other.id
def fingerprint(self) -> str:
"""Generate a fingerprint for deduplication based on key attributes."""
return f"{self.title}:{self.source}:{':'.join(sorted(self.tags))}"
@dataclass
class Rule:
"""Represents an alert rule with suppression and escalation settings."""
id: str
name: str
dedup_window_seconds: float = 300.0 # 5 minutes default
suppression_tags: Set[str] = field(default_factory=set)
max_escalation_count: int = 3
escalation_interval_seconds: float = 3600.0 # 1 hour default
class AlertManager:
"""Manages alerts with deduplication, suppression, and escalation."""
def __init__(self) -> None:
"""Initialize the AlertManager."""
self.rules: Dict[str, Rule] = {}
self.active_alerts: Dict[str, Alert] = {} # alert_id -> Alert
self.alert_history: Dict[str, List[Alert]] = defaultdict(list) # fingerprint -> alerts
self.suppressed_alerts: Set[str] = set() # alert IDs that are suppressed
def add_rule(self, rule: Rule) -> None:
"""Add a rule to the manager."""
if not isinstance(rule, Rule):
raise TypeError("Rule must be an instance of Rule class")
self.rules[rule.id] = rule
def process_alert(self, alert: Alert) -> bool:
"""
Process an incoming alert.
Returns:
bool: True if alert was processed (not deduplicated), False if deduplicated
"""
if not isinstance(alert, Alert):
raise TypeError("Alert must be an instance of Alert class")
# Assign rule if not already assigned
if not alert.rule_id and len(self.rules) == 1:
alert.rule_id = next(iter(self.rules))
elif not alert.rule_id:
raise ValueError("Alert must have a rule_id or exactly one rule must exist")
rule = self.rules.get(alert.rule_id)
if not rule:
raise ValueError(f"Rule with ID {alert.rule_id} not found")
# Check for suppression
if self._is_suppressed(alert, rule):
alert.status = AlertStatus.SUPPRESSED
self.suppressed_alerts.add(alert.id)
self.active_alerts[alert.id] = alert
return True
# Check for deduplication
duplicate_id = self._find_duplicate(alert, rule)
if duplicate_id:
# Update the existing alert
existing_alert = self.active_alerts[duplicate_id]
existing_alert.timestamp = alert.timestamp
existing_alert.description = alert.description
existing_alert.escalation_count = alert.escalation_count
return False
# New alert - check for escalation
if self._should_escalate(alert, rule):
alert.escalation_count += 1
# Add to active alerts
self.active_alerts[alert.id] = alert
self.alert_history[alert.fingerprint()].append(alert)
return True
def resolve_alert(self, alert_id: str) -> bool:
"""
Resolve an active alert.
Returns:
bool: True if alert was resolved, False if not found
"""
if alert_id not in self.active_alerts:
return False
alert = self.active_alerts[alert_id]
alert.status = AlertStatus.RESOLVED
alert.timestamp = time.time()
del self.active_alerts[alert_id]
return True
def get_active_alerts(self) -> List[Alert]:
"""Get all currently active, non-suppressed alerts. (Suppressed
alerts are tracked in the same store; returning them as 'active'
would page on exactly the alerts suppression exists to silence.)"""
return [alert for alert in self.active_alerts.values()
if alert.id not in self.suppressed_alerts]
def get_suppressed_alerts(self) -> List[Alert]:
"""Get all currently suppressed alerts."""
return [alert for alert in self.active_alerts.values()
if alert.id in self.suppressed_alerts]
def _is_suppressed(self, alert: Alert, rule: Rule) -> bool:
"""Check if an alert should be suppressed based on rule tags."""
return bool(rule.suppression_tags & alert.tags)
def _find_duplicate(self, alert: Alert, rule: Rule) -> Optional[str]:
"""
Find a duplicate alert within the deduplication window.
Returns:
str or None: ID of duplicate alert if found, None otherwise
"""
fingerprint = alert.fingerprint()
current_time = time.time()
# Check recent alerts with same fingerprint
for historical_alert in reversed(self.alert_history[fingerprint]):
# Only consider active alerts
if historical_alert.id not in self.active_alerts:
continue
# Check if within dedup window
if current_time - historical_alert.timestamp <= rule.dedup_window_seconds:
return historical_alert.id
else:
# Since we're going backwards in time, no need to check further
break
return None
def _should_escalate(self, alert: Alert, rule: Rule) -> bool:
"""
Determine if an alert should be escalated.
Returns:
bool: True if alert should be escalated
"""
if rule.max_escalation_count <= 0:
return False
fingerprint = alert.fingerprint()
current_time = time.time()
# Count recent alerts with same fingerprint
recent_count = 0
for historical_alert in reversed(self.alert_history[fingerprint]):
if current_time - historical_alert.timestamp <= rule.escalation_interval_seconds:
recent_count += 1
else:
break
return recent_count >= rule.max_escalation_count
def _mk_alert(title, severity, source, tags, ts):
# timestamp passed explicitly: the dataclass default_factory bound the
# real time.time at class definition, so a patched clock can't reach it.
return Alert(title=title, description=title, severity=severity,
source=source, tags=tags, rule_id="service_rule", timestamp=ts)
def main():
"""Self-test on a fake clock: dedup inside the window, re-alert after it,
tag suppression, resolve lifecycle — all exact."""
_now = [50_000.0]
_real_time = time.time
time.time = lambda: _now[0]
try:
manager = AlertManager()
manager.add_rule(Rule(id="service_rule", name="Service Alert Rule",
dedup_window_seconds=10.0,
suppression_tags={"maintenance"},
max_escalation_count=2,
escalation_interval_seconds=30.0))
# First alert accepted; identical fingerprint inside 10s deduped.
a1 = _mk_alert("High CPU", AlertSeverity.HIGH, "server01", {"prod", "cpu"}, _now[0])
assert manager.process_alert(a1) is True, "first alert rejected"
_now[0] += 1.0
dup = _mk_alert("High CPU", AlertSeverity.HIGH, "server01", {"prod", "cpu"}, _now[0])
assert manager.process_alert(dup) is False, "duplicate inside window accepted"
# Different fingerprint is NOT deduped.
a3 = _mk_alert("High Memory", AlertSeverity.CRITICAL, "server01", {"prod"}, _now[0])
assert manager.process_alert(a3) is True, "distinct alert wrongly deduped"
# PAST the window the same fingerprint alerts again.
_now[0] += 11.0
again = _mk_alert("High CPU", AlertSeverity.HIGH, "server01", {"prod", "cpu"}, _now[0])
assert manager.process_alert(again) is True, \
"re-alert after the dedup window was swallowed"
# Suppression: maintenance-tagged alert is accepted but SUPPRESSED.
supp = _mk_alert("Disk Warning", AlertSeverity.MEDIUM, "server02",
{"maintenance", "disk"}, _now[0])
assert manager.process_alert(supp) is True
suppressed = manager.get_suppressed_alerts()
assert [s.title for s in suppressed] == ["Disk Warning"], \
f"suppressed set wrong: {[s.title for s in suppressed]}"
active_titles = sorted(a.title for a in manager.get_active_alerts())
assert "Disk Warning" not in active_titles, "suppressed alert is active"
# Resolve removes from active, reports honestly.
n_active_before = len(manager.get_active_alerts())
assert n_active_before == 3, \
f"exactly 3 alerts must be active (cpu, memory, cpu-again), got {n_active_before}"
assert manager.resolve_alert(a1.id) is True
assert manager.resolve_alert(a1.id) is False, "double resolve reported success"
assert manager.resolve_alert("ghost-id") is False
assert len(manager.get_active_alerts()) == n_active_before - 1, \
"resolve did not shrink the active set by exactly 1"
# Dedup applies per-fingerprint: 4 rapid repeats → 1 accepted, 3 deduped.
accepted = 0
for i in range(4):
e = _mk_alert("Recurring Error", AlertSeverity.MEDIUM, "serviceA", {"error"}, _now[0])
if manager.process_alert(e):
accepted += 1
_now[0] += 0.5
assert accepted == 1, f"4 repeats in 2s must accept exactly 1, accepted {accepted}"
finally:
time.time = _real_time
print("alert_manager: dedup in-window, re-alert at +11s, maintenance "
"suppressed, resolve exact, 4 repeats → 1 accepted — PASS")
if __name__ == "__main__":
main()