Skip to content

Commit

Permalink
[alert]feature: support alert message i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsun28 committed May 15, 2022
1 parent 3dc1264 commit 382769a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
20 changes: 10 additions & 10 deletions alerter/src/main/java/com/usthe/alert/calculate/CalculateAlarm.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
Expand All @@ -45,6 +42,8 @@ public class CalculateAlarm {
*/
private Map<String, Alert> triggeredAlertMap;

private ResourceBundle bundle;

public CalculateAlarm (AlerterWorkerPool workerPool, AlerterDataQueue dataQueue,
AlertDefineService alertDefineService, MetricsDataExporter dataExporter,
AlertMonitorDao monitorDao, AlerterProperties alerterProperties) {
Expand All @@ -53,6 +52,7 @@ public CalculateAlarm (AlerterWorkerPool workerPool, AlerterDataQueue dataQueue,
this.dataExporter = dataExporter;
this.alertDefineService = alertDefineService;
this.alerterProperties = alerterProperties;
this.bundle = ResourceBundle.getBundle("alerter", Locale.getDefault());
this.triggeredAlertMap = new ConcurrentHashMap<>(128);
// 初始化stateAlertMap
List<Monitor> monitors = monitorDao.findMonitorsByStatusIn(Arrays.asList(CommonConstants.UN_AVAILABLE_CODE,
Expand All @@ -67,13 +67,13 @@ public CalculateAlarm (AlerterWorkerPool workerPool, AlerterDataQueue dataQueue,
.priority(CommonConstants.ALERT_PRIORITY_CODE_EMERGENCY)
.status(CommonConstants.ALERT_STATUS_CODE_PENDING)
.target(CommonConstants.AVAILABLE)
.content("Monitoring Availability Emergency Alert: " + CollectRep.Code.UN_AVAILABLE.name())
.content(this.bundle.getString("alerter.availability.emergency") + ": " + CollectRep.Code.UN_AVAILABLE.name())
.firstTriggerTime(System.currentTimeMillis())
.lastTriggerTime(System.currentTimeMillis());
if (monitor.getStatus() == CommonConstants.UN_REACHABLE_CODE) {
alertBuilder
.target(CommonConstants.REACHABLE)
.content("Monitoring Reachability Emergency Alert: " + CollectRep.Code.UN_REACHABLE.name());
.content(this.bundle.getString("alerter.reachability.emergency") + ": " + CollectRep.Code.UN_REACHABLE.name());
}
this.triggeredAlertMap.put(String.valueOf(monitor.getId()), alertBuilder.build());
}
Expand Down Expand Up @@ -130,10 +130,10 @@ private void calculate(CollectRep.MetricsData metricsData) {
tags.put(CommonConstants.TAG_MONITOR_ID, String.valueOf(monitorId));
tags.put(CommonConstants.TAG_MONITOR_APP, app);
String target = CommonConstants.AVAILABLE;
String content = "Availability Alert Resolved, Monitor Status Normal Now";
String content = this.bundle.getString("alerter.availability.resolved");
if (CommonConstants.REACHABLE.equals(preAlert.getTarget())) {
target = CommonConstants.REACHABLE;
content = "Reachability Alert Resolved, Monitor Status Normal Now";
content = this.bundle.getString("alerter.reachability.resolved");
}
Alert resumeAlert = Alert.builder()
.tags(tags)
Expand Down Expand Up @@ -248,15 +248,15 @@ private void handlerMonitorStatusAlert(String monitorId, String app, CollectRep.
.priority(CommonConstants.ALERT_PRIORITY_CODE_EMERGENCY)
.status(CommonConstants.ALERT_STATUS_CODE_PENDING)
.target(CommonConstants.AVAILABLE)
.content("Monitoring Availability Emergency Alert: " + code.name())
.content(this.bundle.getString("alerter.availability.emergency") + ": " + code.name())
.firstTriggerTime(currentTimeMill)
.lastTriggerTime(currentTimeMill)
.nextEvalInterval(alerterProperties.getAlertEvalIntervalBase())
.times(1);
if (code == CollectRep.Code.UN_REACHABLE) {
alertBuilder
.target(CommonConstants.REACHABLE)
.content("Monitoring Reachability Emergency Alert: " + code.name());
.content(this.bundle.getString("alerter.reachability.emergency") + ": " + code.name());
}
Alert alert = alertBuilder.build();
dataQueue.addAlertData(alert.clone());
Expand Down
4 changes: 4 additions & 0 deletions alerter/src/main/resources/alerter_en_US.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
alerter.availability.emergency = Monitoring Availability Emergency Alert
alerter.reachability.emergency = Monitoring Reachability Emergency Alert
alerter.availability.resolved = Availability Alert Resolved, Monitor Status Normal Now
alerter.reachability.resolved = Reachability Alert Resolved, Monitor Status Normal Now
4 changes: 4 additions & 0 deletions alerter/src/main/resources/alerter_zh_CN.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
alerter.availability.emergency = 监控紧急可用性告警
alerter.reachability.emergency = 监控紧急可达性告警
alerter.availability.resolved = 可用性告警恢复通知, 监控状态已恢复正常
alerter.reachability.resolved = 可达性告警恢复通知, 监控状态已恢复正常
1 change: 1 addition & 0 deletions manager/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
<filtering>true</filtering>
<includes>
<include>application.yml</include>
<include>*.properties</include>
<include>sureness.yml</include>
<include>banner.txt</include>
<include>define/**</include>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public String buildAlertHtmlTemplate(final Alert alert) {
monitorId = alert.getTags().get(CommonConstants.TAG_MONITOR_ID);
monitorName = alert.getTags().get(CommonConstants.TAG_MONITOR_NAME);
}
monitorId = monitorId == null? "External alarm, no ID" : monitorId;
monitorName = monitorName == null? "External alarm, no Name" : monitorName;
monitorId = monitorId == null? "External Alarm, No ID" : monitorId;
monitorName = monitorName == null? "External Alarm, No Name" : monitorName;
// Introduce thymeleaf context parameters to render pages
// 引入thymeleaf上下文参数渲染页面
Context context = new Context();
Expand Down

0 comments on commit 382769a

Please sign in to comment.