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

fix: alert cache retry #786

Closed
wants to merge 3 commits into from
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
@@ -0,0 +1,9 @@
/*
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0.
*/

-- ----------------------------
-- Table structure for alert_notification_template
-- ----------------------------
ALTER TABLE `alert_notification_template`
ADD COLUMN `description` varchar(255) DEFAULT NULL COMMENT '模板描述信息';
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.holoinsight.server.home.facade.AlertNotifyRecordDTO;
import io.holoinsight.server.home.facade.AlertRuleExtra;
import io.holoinsight.server.home.facade.InspectConfig;
import io.holoinsight.server.home.facade.NotificationTemplate;
import io.holoinsight.server.home.facade.trigger.Trigger;
import lombok.Data;

Expand Down Expand Up @@ -55,6 +56,8 @@ public class AlertNotifyRequest {

private AlertRuleExtra alertRuleExtra;

private NotificationTemplate notificationTemplate;

public Long alarmHistoryId;

public Long alarmHistoryDetailId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@
import io.holoinsight.server.home.biz.plugin.model.PluginContext;
import io.holoinsight.server.home.biz.service.IntegrationPluginService;
import io.holoinsight.server.home.dal.converter.AlarmRuleConverter;
import io.holoinsight.server.home.dal.converter.AlertNotificationTemplateConverter;
import io.holoinsight.server.home.dal.mapper.AlarmRuleMapper;
import io.holoinsight.server.home.dal.mapper.AlertNotificationTemplateMapper;
import io.holoinsight.server.home.dal.model.AlarmRule;
import io.holoinsight.server.home.dal.model.AlertNotificationTemplate;
import io.holoinsight.server.home.dal.model.dto.IntegrationPluginDTO;
import io.holoinsight.server.home.facade.AlarmRuleDTO;
import io.holoinsight.server.home.facade.AlertNotificationTemplateDTO;
import io.holoinsight.server.home.facade.AlertNotifyRecordDTO;
import io.holoinsight.server.home.facade.AlertRuleExtra;
import io.holoinsight.server.home.facade.NotificationTemplate;
import io.holoinsight.server.home.facade.trigger.Trigger;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -59,6 +64,10 @@ public abstract class GatewayService {
private EnvironmentProperties environmentProperties;
@Resource
private AlarmRuleConverter alarmRuleConverter;
@Resource
private AlertNotificationTemplateMapper alertNotificationTemplateMapper;
@Autowired
private AlertNotificationTemplateConverter alertNotificationTemplateConverter;

private static final String GATEWAY = "GatewayService";

Expand Down Expand Up @@ -117,6 +126,21 @@ public boolean sendAlertNotifyV3(AlertNotifyRequest notify, AlertNotifyRecordLat

AlertRuleExtra extra = alertRule.getExtra();
notify.setAlertRuleExtra(extra);
NotificationTemplate notificationTemplate = extra == null ? null : extra.getDingTalkTemplate();
if (notificationTemplate == null) {
Long alertNotificationTemplateId = alertRule.getAlertNotificationTemplateId();
if (alertNotificationTemplateId != null) {
AlertNotificationTemplate alertNotificationTemplate =
this.alertNotificationTemplateMapper.selectById(alertNotificationTemplateId);
if (alertNotificationTemplate != null) {
AlertNotificationTemplateDTO templateDTO =
this.alertNotificationTemplateConverter.doToDTO(alertNotificationTemplate);
notificationTemplate = templateDTO.templateConfig;
}
}
}
notify.setNotificationTemplate(notificationTemplate);

PluginContext pluginContext = buildNotifyContext(traceId, notify);
RecordSucOrFailNotify.alertNotifyProcessSuc(GATEWAY, "send alert notify",
notify.getAlertNotifyRecord());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,18 +222,23 @@ public Integer ruleSize(String ruleType) {

protected List<AlarmRule> getAlarmRuleListByPage() {
List<AlarmRule> alarmRules = new ArrayList<>();
List<AlarmRule> ruleAlerts =
getAlertRule("rule", this.rulePageNum.get(), this.rulePageSize.get());
List<AlarmRule> aiAlerts = getAlertRule("ai", this.aiPageNum.get(), this.aiPageSize.get());
List<AlarmRule> pqlAlerts = getAlertRule("pql", this.pqlPageNum.get(), this.pqlPageSize.get());
if (!CollectionUtils.isEmpty(ruleAlerts)) {
alarmRules.addAll(ruleAlerts);
}
if (!CollectionUtils.isEmpty(aiAlerts)) {
alarmRules.addAll(aiAlerts);
}
if (!CollectionUtils.isEmpty(pqlAlerts)) {
alarmRules.addAll(pqlAlerts);
int retry = 0;
while (retry++ < 3 && CollectionUtils.isEmpty(alarmRules)) {
List<AlarmRule> ruleAlerts =
getAlertRule("rule", this.rulePageNum.get(), this.rulePageSize.get());
List<AlarmRule> aiAlerts = getAlertRule("ai", this.aiPageNum.get(), this.aiPageSize.get());
List<AlarmRule> pqlAlerts =
getAlertRule("pql", this.pqlPageNum.get(), this.pqlPageSize.get());
if (!CollectionUtils.isEmpty(ruleAlerts)) {
alarmRules.addAll(ruleAlerts);
}
if (!CollectionUtils.isEmpty(aiAlerts)) {
alarmRules.addAll(aiAlerts);
}
if (!CollectionUtils.isEmpty(pqlAlerts)) {
alarmRules.addAll(pqlAlerts);
}
LOGGER.info("try to get AlarmRuleList retry {}, size {}", retry, alarmRules.size());
}
return alarmRules;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ public class AlertNotificationTemplate {
public String workspace;
public String creator;
public String modifier;
public String description;

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ public class AlertNotificationTemplateDTO {
public String workspace;
public String creator;
public String modifier;
public String description;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,11 @@ public class AlertRuleExtra {
public Map<String, String> tagAlias;
public boolean isRecord;
public AlertSilenceConfig alertSilenceConfig;

public NotificationTemplate getDingTalkTemplate() {
if (notificationConfig == null || notificationConfig.dingtalkTemplate == null) {
return null;
}
return notificationConfig.dingtalkTemplate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @author masaimu
* @version 2023-02-21 15:59:00
*/
public class NotificationTemplate {

public LinkedHashMap<String, AlertTemplateField> fieldMap = new LinkedHashMap<>();
public LinkedHashMap<String, String> tagMap = new LinkedHashMap<>();
public LinkedHashMap<String /* alias */, AlertTemplateField> fieldMap = new LinkedHashMap<>();
public LinkedHashMap<String /* alias */, String /* tagk */> tagMap = new LinkedHashMap<>();
public String text;

public static NotificationTemplate defaultWebhookTemplate() {
NotificationTemplate template = new NotificationTemplate();
Expand All @@ -37,6 +40,7 @@ public static NotificationTemplate defaultWebhookTemplate() {
template.fieldMap.put("aggregationNum", AlertTemplateField.aggregationNum);
template.fieldMap.put("alarmUrl", AlertTemplateField.ruleUrl);
template.fieldMap.put("link", AlertTemplateField.LINK);
template.text = template.getTemplateJson();
return template;
}

Expand All @@ -51,7 +55,7 @@ public static NotificationTemplate defaultDingtalkTemplate() {
template.fieldMap.put("此次评估触发时间", AlertTemplateField.alarmTime);
template.fieldMap.put("告警触发数值", AlertTemplateField.ALERT_VALUE);
template.fieldMap.put("日志内容", AlertTemplateField.LOG_CONTENT);

template.text = template.getTemplateJson();
return template;
}

Expand All @@ -73,7 +77,7 @@ public static NotificationTemplate defaultMiniappDingtalkTemplate(TemplateValue
if (templateValue != null && StringUtils.isNotEmpty(templateValue.getLogContent())) {
template.fieldMap.put("日志内容", AlertTemplateField.LOG_CONTENT);
}

template.text = template.getTemplateJson();
return template;
}

Expand All @@ -84,6 +88,12 @@ public String getTemplateJson() {
LinkedHashMap<String, String> templateMap = new LinkedHashMap<>();
for (Map.Entry<String, AlertTemplateField> entry : this.fieldMap.entrySet()) {
templateMap.put(entry.getKey(), String.format("${%s}", entry.getValue().getFieldName()));
if (entry.getValue() == AlertTemplateField.ALERT_SCOPE && !CollectionUtils.isEmpty(tagMap)) {
for (Map.Entry<String, String> tagItem : tagMap.entrySet()) {
templateMap.put(tagItem.getKey(),
String.format("${%s.%s}", entry.getValue().getFieldName(), tagItem.getValue()));
}
}
}
return J.toJson(templateMap);
}
Expand Down Expand Up @@ -141,6 +151,9 @@ private String getValue(TemplateValue templateValue, AlertTemplateField field,
return templateValue.metric;
case ALERT_PRIORITY:
case alarmLevel:
if (templateValue.alarmLevel == null) {
return StringUtils.EMPTY;
}
return templateValue.alarmLevel.getDesc();
case ALERT_QUERY:
return templateValue.alertQuery;
Expand Down Expand Up @@ -204,17 +217,35 @@ private String buildTagValue(String alarmTags, boolean tagMarkdown) {
if (alarmTags.startsWith("[")) {
return alarmTags;
}
Map<String /* tagk */, String /* alias */> reverseTagMap = reverseTagMap();
Map<String, Object> tags = J.toMap(alarmTags);
StringBuilder msg = new StringBuilder();
for (Map.Entry<String, Object> entry : tags.entrySet()) {
msg.append(String.format(" - %s: %s \n\n ", entry.getKey(), entry.getValue()));
if (!CollectionUtils.isEmpty(reverseTagMap)) {
if (reverseTagMap.containsKey(entry.getKey())) {
msg.append(String.format(" - %s: %s \n\n ", reverseTagMap.get(entry.getKey()),
entry.getValue()));
}
} else {
msg.append(String.format(" - %s: %s \n\n ", entry.getKey(), entry.getValue()));
}
}
return msg.toString();
} else {
return alarmTags;
}
}

private Map<String, String> reverseTagMap() {
Map<String /* tagk */, String /* alias */> reverseTagMap = new LinkedHashMap<>();
for (Map.Entry<String /* alias */, String /* tagk */> tagItem : tagMap.entrySet()) {
String alias = tagItem.getKey();
String tagk = tagItem.getValue();
reverseTagMap.put(tagk, alias);
}
return reverseTagMap;
}

public static String getAlertValue(Double alertValue) {
return String.format("%.4f", alertValue);
}
Expand Down Expand Up @@ -244,4 +275,35 @@ private String buildAlertTitle(InspectConfig ruleConfig) {
}
return ruleConfig.getRule().getTriggers().get(0).getTriggerTitle();
}

private static final Pattern pattern = Pattern.compile("\\$\\{(.*?)\\}");

public boolean parseText() {
if (StringUtils.isEmpty(this.text)) {
return true;
}
try {
Map<String, Object> map = J.toMap(this.text);
for (Map.Entry<String, Object> entry : map.entrySet()) {
String alias = entry.getKey();
String fieldName = (String) entry.getValue();
if (StringUtils.isBlank(fieldName)) {
continue;
}
Matcher matcher = pattern.matcher(fieldName);
if (matcher.find()) {
fieldName = matcher.group(1); // group(1) corresponds to (.*?), which is the content
// within ${}
}
if (fieldName.startsWith("ALERT_SCOPE.") || fieldName.startsWith("alarmTags.")) {
tagMap.put(alias, fieldName.split("\\.", 2)[1]);
} else {
fieldMap.put(alias, AlertTemplateField.valueOf(fieldName));
}
}
} catch (Exception e) {
return false;
}
return true;
}
}
Loading
Loading