The before code sends an individual email alert for each service exceeding error rate threshold, causing alert noise. The after code groups alerts by team and sends a single aggregated Slack message per team, reducing noise and improving routing.
### Before: Naive alerting without aggregation or routing
alerts = []
for service in services:
if service.error_rate > 0.05:
alerts.append(f"Alert: {service.name} error rate high")
for alert in alerts:
send_email(alert)
### After: Alerting with aggregation and routing
from collections import defaultdict
alerts_by_team = defaultdict(list)
for service in services:
if service.error_rate > 0.05:
alerts_by_team[service.team].append(f"{service.name} error rate high")
for team, alerts in alerts_by_team.items():
message = "\n".join(alerts)
send_slack(team, message)
def send_email(message):
# send email implementation
pass
def send_slack(team, message):
# send slack message to team's channel
pass