Skip to content

Commit 1715c09

Browse files
committed
fix(middleware): add alert sorting for correct processing
1 parent f3dba61 commit 1715c09

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

middleware/alerts-middleware.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,28 @@ def log_request_details(req):
483483
)
484484
print(log_line)
485485

486+
def sort_alerts_for_processing(alerts):
487+
"""
488+
Sort alerts for processing:
489+
- First, all 'resolved' alerts, ordered by endsAt (oldest first)
490+
- Then, all 'firing' alerts, ordered by startsAt (oldest first)
491+
- Alerts with unknown status are left at the end, in original order
492+
"""
493+
def parse_time(s):
494+
try:
495+
return datetime.fromisoformat(s.replace('Z', '+00:00'))
496+
except Exception:
497+
return datetime.max
498+
499+
resolved = [a for a in alerts if a.get('status') == 'resolved']
500+
firing = [a for a in alerts if a.get('status') == 'firing']
501+
other = [a for a in alerts if a.get('status') not in ('resolved', 'firing')]
502+
503+
resolved_sorted = sorted(resolved, key=lambda a: parse_time(a.get('endsAt', '9999-12-31T23:59:59Z')))
504+
firing_sorted = sorted(firing, key=lambda a: parse_time(a.get('startsAt', '9999-12-31T23:59:59Z')))
505+
506+
return resolved_sorted + firing_sorted + other
507+
486508
@app.route("/health", methods=["GET"])
487509
def health():
488510
"""Health check endpoint for container orchestration and load balancers."""
@@ -495,14 +517,20 @@ def webhook():
495517
record_memory()
496518
data = request.json
497519

498-
for alert in data.get("alerts", []):
520+
alerts = data.get("alerts", [])
521+
if len(alerts) > 1:
522+
alerts = sort_alerts_for_processing(alerts)
523+
logger.info(f"Sorted {len(alerts)} alerts for processing order.")
524+
for alert in alerts:
499525
labels = alert.get("labels", {})
500526
instance = labels.get("instance")
501527
alertname = labels.get("alertname")
502528
status_page_component = labels.get("status_page_component")
503529
status_page_alert = labels.get("status_page_alert")
504530
status = alert["status"]
505531

532+
logger.info(f"Processing alert in state '{status}' with startsAt: {alert.get('startsAt')} and endsAt: {alert.get('endsAt')}")
533+
506534
if not status_page_alert:
507535
logger.info(f"Alert '{alertname}' for instance '{instance}' has status_page_alert != true. Ignoring.")
508536
continue

0 commit comments

Comments
 (0)