Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[improve]improve:Ensure that the start time of the alarm is set to th… #2862

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import static org.apache.hertzbeat.common.constants.CommonConstants.TAG_METRICS;
import static org.apache.hertzbeat.common.constants.CommonConstants.TAG_MONITOR_APP;
import static org.apache.hertzbeat.common.constants.CommonConstants.TAG_MONITOR_ID;
import static org.apache.hertzbeat.common.constants.CommonConstants.TAG_MONITOR_NAME;
import jakarta.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -47,7 +46,6 @@
import org.apache.hertzbeat.common.constants.CommonConstants;
import org.apache.hertzbeat.common.entity.alerter.Alert;
import org.apache.hertzbeat.common.entity.alerter.AlertDefine;
import org.apache.hertzbeat.common.entity.manager.Monitor;
import org.apache.hertzbeat.common.entity.manager.TagItem;
import org.apache.hertzbeat.common.entity.message.CollectRep;
import org.apache.hertzbeat.common.queue.CommonDataQueue;
Expand All @@ -69,7 +67,7 @@
public class CalculateAlarm {

private static final String SYSTEM_VALUE_ROW_COUNT = "system_value_row_count";

private static final int CALCULATE_THREADS = 3;

/**
Expand Down Expand Up @@ -101,18 +99,6 @@ public CalculateAlarm(AlerterWorkerPool workerPool, CommonDataQueue dataQueue,
this.bundle = ResourceBundleUtil.getBundle("alerter");
this.triggeredAlertMap = new ConcurrentHashMap<>(16);
this.notRecoveredAlertMap = new ConcurrentHashMap<>(16);
// Initialize stateAlertMap
List<Monitor> monitors = monitorDao.findMonitorsByStatus(CommonConstants.MONITOR_DOWN_CODE);
if (monitors != null) {
for (Monitor monitor : monitors) {
HashMap<String, String> tags = new HashMap<>(8);
tags.put(TAG_MONITOR_ID, String.valueOf(monitor.getId()));
tags.put(TAG_MONITOR_NAME, monitor.getName());
tags.put(TAG_MONITOR_APP, monitor.getApp());
this.notRecoveredAlertMap.put(monitor.getId() + CommonConstants.AVAILABILITY,
Alert.builder().tags(tags).target(CommonConstants.AVAILABILITY).status(ALERT_STATUS_CODE_PENDING).build());
}
}
startCalculate();
}

Expand Down Expand Up @@ -181,6 +167,7 @@ private void calculate(CollectRep.MetricsData metricsData) {
if (define.isRecoverNotice()) {
handleRecoveredAlert(currentTimeMilli, define, expr, alarmKey);
}
notRecoveredAlertMap.remove(alarmKey);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
Expand Down Expand Up @@ -236,6 +223,7 @@ private void calculate(CollectRep.MetricsData metricsData) {
if (define.isRecoverNotice()) {
handleRecoveredAlert(currentTimeMilli, define, expr, alarmKey);
}
notRecoveredAlertMap.remove(alarmKey);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
Expand All @@ -258,8 +246,10 @@ private void handleRecoveredAlert(long currentTimeMilli, AlertDefine define, Str
.content(content)
.priority(CommonConstants.ALERT_PRIORITY_CODE_WARNING)
.status(CommonConstants.ALERT_STATUS_CODE_RESTORED)
.firstAlarmTime(currentTimeMilli)
.lastAlarmTime(notResolvedAlert.getLastAlarmTime())
//only update the alarm time without increasing the alarm count
.firstAlarmTime(notResolvedAlert.getFirstAlarmTime())
.lastAlarmTime(currentTimeMilli)
.times(notResolvedAlert.getTimes())
.triggerTimes(1)
.build();
alarmCommonReduce.reduceAndSendAlarm(resumeAlert);
Expand All @@ -270,15 +260,27 @@ private void afterThresholdRuleMatch(long currentTimeMilli, long monitorId, Stri
Map<String, Object> fieldValueMap, AlertDefine define) {
String alarmKey = String.valueOf(monitorId) + define.getId() + tagStr;
Alert triggeredAlert = triggeredAlertMap.get(alarmKey);
Alert notResolvedAlert = notRecoveredAlertMap.get(alarmKey);
if (triggeredAlert == null && notResolvedAlert != null) {
//an alarm has been triggered, entering a new triggering cycle
triggeredAlert = notResolvedAlert.clone();
triggeredAlert.setTriggerTimes(0);
triggeredAlertMap.put(alarmKey, triggeredAlert);
}
if (triggeredAlert != null) {
int times = triggeredAlert.getTriggerTimes() + 1;
triggeredAlert.setTriggerTimes(times);
triggeredAlert.setFirstAlarmTime(currentTimeMilli);
triggeredAlert.setLastAlarmTime(currentTimeMilli);
if (notResolvedAlert == null) {
//The first alarm
triggeredAlert.setFirstAlarmTime(currentTimeMilli);
}
int defineTimes = define.getTimes() == null ? 1 : define.getTimes();
if (times >= defineTimes) {
int alertTimes = triggeredAlert.getTimes() == null ? 0 : triggeredAlert.getTimes();
triggeredAlert.setStatus(ALERT_STATUS_CODE_PENDING);
triggeredAlertMap.remove(alarmKey);
triggeredAlert.setTimes(alertTimes + 1);
notRecoveredAlertMap.put(alarmKey, triggeredAlert);
alarmCommonReduce.reduceAndSendAlarm(triggeredAlert.clone());
}
Expand Down Expand Up @@ -310,6 +312,8 @@ private void afterThresholdRuleMatch(long currentTimeMilli, long monitorId, Stri
int defineTimes = define.getTimes() == null ? 1 : define.getTimes();
if (1 >= defineTimes) {
alert.setStatus(ALERT_STATUS_CODE_PENDING);
//The first alarm
alert.setTimes(1);
notRecoveredAlertMap.put(alarmKey, alert);
alarmCommonReduce.reduceAndSendAlarm(alert.clone());
} else {
Expand Down Expand Up @@ -370,6 +374,14 @@ private void handlerAvailableMetrics(long monitorId, String app, CollectRep.Metr
tags.put(tagItem.getName(), tagItem.getValue());
}
}
String notResolvedAlertKey = monitorId + CommonConstants.AVAILABILITY;
Alert notResolvedAlert = notRecoveredAlertMap.get(notResolvedAlertKey);
if (preAlert == null && notResolvedAlert != null) {
// Previously triggered an availability alarm, entering a new triggering cycle
preAlert = notResolvedAlert.clone();
preAlert.setTriggerTimes(0);
triggeredAlertMap.put(String.valueOf(monitorId), preAlert);
}
if (preAlert == null) {
Alert.AlertBuilder alertBuilder = Alert.builder()
.tags(tags)
Expand All @@ -381,8 +393,8 @@ private void handlerAvailableMetrics(long monitorId, String app, CollectRep.Metr
.lastAlarmTime(currentTimeMill)
.triggerTimes(1);
if (avaAlertDefine.getTimes() == null || avaAlertDefine.getTimes() <= 1) {
String notResolvedAlertKey = monitorId + CommonConstants.AVAILABILITY;
alertBuilder.status(ALERT_STATUS_CODE_PENDING);
alertBuilder.times(1);
notRecoveredAlertMap.put(notResolvedAlertKey, alertBuilder.build());
alarmCommonReduce.reduceAndSendAlarm(alertBuilder.build());
} else {
Expand All @@ -391,12 +403,16 @@ private void handlerAvailableMetrics(long monitorId, String app, CollectRep.Metr
} else {
int times = preAlert.getTriggerTimes() + 1;
preAlert.setTriggerTimes(times);
preAlert.setFirstAlarmTime(currentTimeMill);
if (notResolvedAlert == null) {
//The first alarm
preAlert.setFirstAlarmTime(currentTimeMill);
}
preAlert.setLastAlarmTime(currentTimeMill);
int defineTimes = avaAlertDefine.getTimes() == null ? 1 : avaAlertDefine.getTimes();
if (times >= defineTimes) {
int alertTimes = preAlert.getTimes() == null ? 0 : preAlert.getTimes();
preAlert.setTimes(alertTimes + 1);
preAlert.setStatus(ALERT_STATUS_CODE_PENDING);
String notResolvedAlertKey = monitorId + CommonConstants.AVAILABILITY;
notRecoveredAlertMap.put(notResolvedAlertKey, preAlert.clone());
alarmCommonReduce.reduceAndSendAlarm(preAlert.clone());
triggeredAlertMap.remove(String.valueOf(monitorId));
Expand All @@ -421,8 +437,10 @@ private void handlerAvailableMetrics(long monitorId, String app, CollectRep.Metr
.content(content)
.priority(CommonConstants.ALERT_PRIORITY_CODE_WARNING)
.status(CommonConstants.ALERT_STATUS_CODE_RESTORED)
.firstAlarmTime(currentTimeMill)
.lastAlarmTime(notResolvedAlert.getLastAlarmTime())
//For recovery alarms, only update the alarm time without increasing the alarm count
.firstAlarmTime(notResolvedAlert.getFirstAlarmTime())
.lastAlarmTime(currentTimeMill)
.times(notResolvedAlert.getTimes())
.triggerTimes(1)
.build();
alarmCommonReduce.reduceAndSendAlarm(resumeAlert);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public class AlarmCommonReduce {
private final AlertMonitorDao alertMonitorDao;

public void reduceAndSendAlarm(Alert alert) {
alert.setTimes(1);
Map<String, String> tags = alert.getTags();
if (tags == null) {
tags = new HashMap<>(8);
Expand Down
Loading