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
| @Component public class IntelligentAlertManager { private final Map<String, AlertRule> alertRules = new ConcurrentHashMap<>(); private final Map<String, AlertState> alertStates = new ConcurrentHashMap<>(); private final NotificationService notificationService; @PostConstruct public void initializeAlertRules() { alertRules.put("high_latency", AlertRule.builder() .name("High Latency") .condition("p95_latency > 5000") .severity(AlertSeverity.WARNING) .cooldownPeriod(Duration.ofMinutes(5)) .escalationRules(Arrays.asList( EscalationRule.builder() .condition("p95_latency > 10000") .severity(AlertSeverity.CRITICAL) .delay(Duration.ofMinutes(2)) .build() )) .build()); alertRules.put("high_error_rate", AlertRule.builder() .name("High Error Rate") .condition("error_rate > 0.05") .severity(AlertSeverity.WARNING) .cooldownPeriod(Duration.ofMinutes(3)) .build()); alertRules.put("high_memory_usage", AlertRule.builder() .name("High Memory Usage") .condition("memory_usage > 0.85") .severity(AlertSeverity.WARNING) .cooldownPeriod(Duration.ofMinutes(10)) .build()); } @EventListener public void evaluateAlerts(PerformanceMetricsEvent event) { alertRules.forEach((ruleId, rule) -> { try { boolean conditionMet = evaluateCondition(rule.getCondition(), event.getMetrics()); AlertState currentState = alertStates.get(ruleId); if (conditionMet) { if (currentState == null || currentState.getState() == AlertStateType.RESOLVED) { triggerAlert(ruleId, rule, event.getMetrics()); } else if (currentState.getState() == AlertStateType.FIRING) { checkEscalation(ruleId, rule, currentState, event.getMetrics()); } } else { if (currentState != null && currentState.getState() == AlertStateType.FIRING) { resolveAlert(ruleId, rule); } } } catch (Exception e) { logger.error("Failed to evaluate alert rule: {}", ruleId, e); } }); } private void triggerAlert(String ruleId, AlertRule rule, PerformanceMetrics metrics) { AlertState state = AlertState.builder() .ruleId(ruleId) .state(AlertStateType.FIRING) .severity(rule.getSeverity()) .triggeredAt(Instant.now()) .metrics(metrics) .build(); alertStates.put(ruleId, state); Alert alert = Alert.builder() .id(UUID.randomUUID().toString()) .ruleId(ruleId) .name(rule.getName()) .severity(rule.getSeverity()) .message(generateAlertMessage(rule, metrics)) .triggeredAt(state.getTriggeredAt()) .build(); notificationService.sendAlert(alert); logger.warn("Alert triggered: {} - {}", rule.getName(), alert.getMessage()); } private void checkEscalation(String ruleId, AlertRule rule, AlertState currentState, PerformanceMetrics metrics) { for (EscalationRule escalationRule : rule.getEscalationRules()) { if (evaluateCondition(escalationRule.getCondition(), metrics)) { Duration timeSinceTriggered = Duration.between(currentState.getTriggeredAt(), Instant.now()); if (timeSinceTriggered.compareTo(escalationRule.getDelay()) >= 0 && currentState.getSeverity().ordinal() < escalationRule.getSeverity().ordinal()) { escalateAlert(ruleId, rule, escalationRule, metrics); break; } } } } private void escalateAlert(String ruleId, AlertRule rule, EscalationRule escalationRule, PerformanceMetrics metrics) { AlertState currentState = alertStates.get(ruleId); AlertState escalatedState = currentState.toBuilder() .severity(escalationRule.getSeverity()) .escalatedAt(Instant.now()) .build(); alertStates.put(ruleId, escalatedState); Alert escalatedAlert = Alert.builder() .id(UUID.randomUUID().toString()) .ruleId(ruleId) .name(rule.getName() + " (Escalated)") .severity(escalationRule.getSeverity()) .message("ESCALATED: " + generateAlertMessage(rule, metrics)) .triggeredAt(escalatedState.getEscalatedAt()) .build(); notificationService.sendAlert(escalatedAlert); logger.error("Alert escalated: {} - {}", rule.getName(), escalatedAlert.getMessage()); } }
|