Skip to content

Commit

Permalink
[DSIP-35][Alert] Refactor the alert thread model
Browse files Browse the repository at this point in the history
  • Loading branch information
ruanwenjun committed Apr 30, 2024
1 parent ebcdaeb commit a0e5210
Show file tree
Hide file tree
Showing 98 changed files with 2,303 additions and 873 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public AlertResult process(AlertInfo info) {

Map<String, String> paramsMap = info.getAlertParams();
if (null == paramsMap) {
return new AlertResult("false", "aliyun-voice params is null");
return new AlertResult(false, "aliyun-voice params is null");
}
VoiceParam voiceParam = buildVoiceParam(paramsMap);
return new VoiceSender(voiceParam).send();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public VoiceSender(VoiceParam voiceParam) {

public AlertResult send() {
AlertResult alertResult = new AlertResult();
alertResult.setStatus("false");
alertResult.setSuccess(false);
try {
Client client = createClient(voiceParam.getConnection());
SingleCallByTtsRequest singleCallByTtsRequest = new SingleCallByTtsRequest()
Expand All @@ -61,7 +61,7 @@ public AlertResult send() {
}
SingleCallByTtsResponseBody body = response.getBody();
if (body.code.equalsIgnoreCase("ok")) {
alertResult.setStatus("true");
alertResult.setSuccess(true);
alertResult.setMessage(body.getCallId());
} else {
alertResult.setMessage(body.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void testSendWeChatTableMsg() {
VoiceSender weChatSender = new VoiceSender(voiceParam);

AlertResult alertResult = weChatSender.send();
Assertions.assertEquals("false", alertResult.getStatus());
Assertions.assertFalse(alertResult.isSuccess());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ public interface AlertChannel {
AlertResult process(AlertInfo info);

default @NonNull AlertResult closeAlert(AlertInfo info) {
return new AlertResult("true", "no need to close alert");
return new AlertResult(true, "no need to close alert");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,6 @@ public class AlertData {
*/
private String log;

/**
* 0 do not send warning;
* 1 send if process success;
* 2 send if process failed;
* 3 send if process ends, whatever the result;
*/
private int warnType;

/**
* AlertType#code
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,19 @@
@NoArgsConstructor
public class AlertResult {

/**
* todo: use enum
* false or true
*/
private String status;
private boolean success;

/**
* alert result message, each plugin can have its own message
*/
private String message;

public static AlertResult success() {
return new AlertResult(true, null);
}

public static AlertResult fail(String message) {
return new AlertResult(false, message);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public AlertResult process(AlertInfo alertInfo) {
AlertData alertData = alertInfo.getAlertData();
Map<String, String> paramsMap = alertInfo.getAlertParams();
if (null == paramsMap) {
return new AlertResult("false", "ding talk params is null");
return new AlertResult(false, "ding talk params is null");
}
return new DingTalkSender(paramsMap).sendDingTalkMsg(alertData.getTitle(), alertData.getContent());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private static RequestConfig getProxyConfig(String proxy, int port) {

private AlertResult checkSendDingTalkSendMsgResult(String result) {
AlertResult alertResult = new AlertResult();
alertResult.setStatus("false");
alertResult.setSuccess(false);

if (null == result) {
alertResult.setMessage("send ding talk msg error");
Expand All @@ -140,7 +140,7 @@ private AlertResult checkSendDingTalkSendMsgResult(String result) {
return alertResult;
}
if (sendMsgResponse.errcode == 0) {
alertResult.setStatus("true");
alertResult.setSuccess(true);
alertResult.setMessage("send ding talk msg success");
return alertResult;
}
Expand All @@ -164,7 +164,7 @@ public AlertResult sendDingTalkMsg(String title, String content) {
} catch (Exception e) {
log.info("send ding talk alert msg exception : {}", e.getMessage());
alertResult = new AlertResult();
alertResult.setStatus("false");
alertResult.setSuccess(false);
alertResult.setMessage("send ding talk alert fail.");
}
return alertResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void testSend() {
dingTalkConfig.put(DingTalkParamsConstants.NAME_DING_TALK_PROXY_ENABLE, "true");
dingTalkSender = new DingTalkSender(dingTalkConfig);
AlertResult alertResult = dingTalkSender.sendDingTalkMsg("title", "content test");
Assertions.assertEquals("false", alertResult.getStatus());
Assertions.assertEquals(false, alertResult.isSuccess());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,20 @@ public AlertResult process(AlertInfo info) {
AlertData alert = info.getAlertData();
Map<String, String> paramsMap = info.getAlertParams();
if (null == paramsMap) {
return new AlertResult("false", "mail params is null");
return new AlertResult(false, "mail params is null");
}
MailSender mailSender = new MailSender(paramsMap);
AlertResult alertResult = mailSender.sendMails(alert.getTitle(), alert.getContent());

boolean flag;

if (alertResult == null) {
alertResult = new AlertResult();
alertResult.setStatus("false");
alertResult.setSuccess(false);
alertResult.setMessage("alert send error.");
log.info("alert send error : {}", alertResult.getMessage());
return alertResult;
}

flag = Boolean.parseBoolean(String.valueOf(alertResult.getStatus()));

if (flag) {
if (alertResult.isSuccess()) {
log.info("alert send success");
alertResult.setMessage("email send success.");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public AlertResult sendMails(String title, String content) {
*/
public AlertResult sendMails(List<String> receivers, List<String> receiverCcs, String title, String content) {
AlertResult alertResult = new AlertResult();
alertResult.setStatus("false");
alertResult.setSuccess(false);

// if there is no receivers && no receiversCc, no need to process
if (CollectionUtils.isEmpty(receivers) && CollectionUtils.isEmpty(receiverCcs)) {
Expand Down Expand Up @@ -201,7 +201,7 @@ public AlertResult sendMails(List<String> receivers, List<String> receiverCcs, S

attachment(title, content, partContent);

alertResult.setStatus("true");
alertResult.setSuccess(true);
return alertResult;
} catch (Exception e) {
handleException(alertResult, e);
Expand Down Expand Up @@ -380,7 +380,7 @@ private AlertResult getStringObjectMap(String title, String content, AlertResult
email.setDebug(true);
email.send();

alertResult.setStatus("true");
alertResult.setSuccess(true);

return alertResult;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void testProcess() {
alertInfo.setAlertParams(paramsMap);
AlertResult alertResult = emailAlertChannel.process(alertInfo);
Assertions.assertNotNull(alertResult);
Assertions.assertEquals("false", alertResult.getStatus());
Assertions.assertFalse(alertResult.isSuccess());
}

public String getEmailAlertParams() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void testSendMails() {
AlertResult alertResult = mailSender.sendMails(
"Mysql Exception",
content);
Assertions.assertEquals("false", alertResult.getStatus());
Assertions.assertFalse(alertResult.isSuccess());
}

@Test
Expand Down Expand Up @@ -107,7 +107,7 @@ void testAuthCheck() {
emailConfig.put(MailParamsConstants.NAME_MAIL_PASSWD, "passwd");
mailSender = new MailSender(emailConfig);
AlertResult alertResult = mailSender.sendMails(title, content);
Assertions.assertEquals("false", alertResult.getStatus());
Assertions.assertFalse(alertResult.isSuccess());
}

public String list2String() {
Expand Down Expand Up @@ -142,24 +142,24 @@ public void testSendTableMail() {
emailConfig.put(AlertConstants.NAME_SHOW_TYPE, ShowType.TABLE.getDescp());
mailSender = new MailSender(emailConfig);
AlertResult alertResult = mailSender.sendMails(title, content);
Assertions.assertEquals("false", alertResult.getStatus());
Assertions.assertFalse(alertResult.isSuccess());
}

@Test
public void testAttachmentFile() throws Exception {
public void testAttachmentFile() {
String content = list2String();
emailConfig.put(AlertConstants.NAME_SHOW_TYPE, ShowType.ATTACHMENT.getDescp());
mailSender = new MailSender(emailConfig);
AlertResult alertResult = mailSender.sendMails("gaojing", content);
Assertions.assertEquals("false", alertResult.getStatus());
Assertions.assertFalse(alertResult.isSuccess());
}

@Test
public void testTableAttachmentFile() throws Exception {
public void testTableAttachmentFile() {
String content = list2String();
emailConfig.put(AlertConstants.NAME_SHOW_TYPE, ShowType.TABLE_ATTACHMENT.getDescp());
mailSender = new MailSender(emailConfig);
AlertResult alertResult = mailSender.sendMails("gaojing", content);
Assertions.assertEquals("false", alertResult.getStatus());
Assertions.assertFalse(alertResult.isSuccess());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public AlertResult process(AlertInfo alertInfo) {
AlertData alertData = alertInfo.getAlertData();
Map<String, String> paramsMap = alertInfo.getAlertParams();
if (null == paramsMap) {
return new AlertResult("false", "fei shu params is null");
return new AlertResult(false, "fei shu params is null");
}
return new FeiShuSender(paramsMap).sendFeiShuMsg(alertData);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private static String textToJsonString(AlertData alertData) {

public static AlertResult checkSendFeiShuSendMsgResult(String result) {
AlertResult alertResult = new AlertResult();
alertResult.setStatus("false");
alertResult.setSuccess(false);

if (org.apache.commons.lang3.StringUtils.isBlank(result)) {
alertResult.setMessage("send fei shu msg error");
Expand All @@ -95,7 +95,7 @@ public static AlertResult checkSendFeiShuSendMsgResult(String result) {
return alertResult;
}
if (sendMsgResponse.statusCode == 0) {
alertResult.setStatus("true");
alertResult.setSuccess(true);
alertResult.setMessage("send fei shu msg success");
return alertResult;
}
Expand Down Expand Up @@ -136,7 +136,7 @@ public AlertResult sendFeiShuMsg(AlertData alertData) {
} catch (Exception e) {
log.info("send fei shu alert msg exception : {}", e.getMessage());
alertResult = new AlertResult();
alertResult.setStatus("false");
alertResult.setSuccess(false);
alertResult.setMessage("send fei shu alert fail.");
}
return alertResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void testSend() {
alertData.setContent("feishu test content");
FeiShuSender feiShuSender = new FeiShuSender(feiShuConfig);
AlertResult alertResult = feiShuSender.sendFeiShuMsg(alertData);
Assertions.assertEquals("false", alertResult.getStatus());
Assertions.assertFalse(alertResult.isSuccess());
}

@Test
Expand Down Expand Up @@ -87,12 +87,12 @@ public void testCheckSendFeiShuSendMsgResult() {

FeiShuSender feiShuSender = new FeiShuSender(feiShuConfig);
AlertResult alertResult = feiShuSender.checkSendFeiShuSendMsgResult("");
Assertions.assertFalse(Boolean.valueOf(alertResult.getStatus()));
Assertions.assertFalse(alertResult.isSuccess());
AlertResult alertResult2 = feiShuSender.checkSendFeiShuSendMsgResult("123");
Assertions.assertEquals("send fei shu msg fail", alertResult2.getMessage());

String response = "{\"StatusCode\":\"0\",\"extra\":\"extra\",\"StatusMessage\":\"StatusMessage\"}";
AlertResult alertResult3 = feiShuSender.checkSendFeiShuSendMsgResult(response);
Assertions.assertTrue(Boolean.valueOf(alertResult3.getStatus()));
Assertions.assertTrue(alertResult3.isSuccess());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public AlertResult process(AlertInfo alertInfo) {
AlertData alertData = alertInfo.getAlertData();
Map<String, String> paramsMap = alertInfo.getAlertParams();
if (null == paramsMap) {
return new AlertResult("false", "http params is null");
return new AlertResult(false, "http params is null");
}

return new HttpSender(paramsMap).send(alertData.getContent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,18 @@ public AlertResult send(String msg) {
}

if (httpRequest == null) {
alertResult.setStatus("false");
alertResult.setSuccess(false);
alertResult.setMessage("Request types are not supported");
return alertResult;
}

try {
String resp = this.getResponseString(httpRequest);
alertResult.setStatus("true");
alertResult.setSuccess(true);
alertResult.setMessage(resp);
} catch (Exception e) {
log.error("send http alert msg exception : {}", e.getMessage());
alertResult.setStatus("false");
alertResult.setSuccess(false);
alertResult.setMessage(
String.format("Send http request alert failed: %s", e.getMessage()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ public void testProcessSuccess() {

// HttpSender(paramsMap).send(alertData.getContent()); already test in HttpSenderTest.sendTest. so we can mock
// it
doReturn(new AlertResult("true", "success")).when(alertChannel).process(any());
doReturn(new AlertResult(true, "success")).when(alertChannel).process(any());
AlertResult alertResult = alertChannel.process(alertInfo);
Assertions.assertEquals("true", alertResult.getStatus());
Assertions.assertTrue(alertResult.isSuccess());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void sendTest() throws Exception {
HttpSender httpSender = spy(new HttpSender(paramsMap));
doReturn("success").when(httpSender).getResponseString(any());
AlertResult alertResult = httpSender.send("Fault tolerance warning");
Assertions.assertEquals("true", alertResult.getStatus());
Assertions.assertTrue(alertResult.isSuccess());
Assertions.assertTrue(httpSender.getRequestUrl().contains(url));
Assertions.assertTrue(httpSender.getRequestUrl().contains(contentField));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public final class PagerDutyAlertChannel implements AlertChannel {
public AlertResult process(AlertInfo alertInfo) {
AlertData alertData = alertInfo.getAlertData();
Map<String, String> alertParams = alertInfo.getAlertParams();
if (alertParams == null || alertParams.size() == 0) {
return new AlertResult("false", "PagerDuty alert params is empty");
if (alertParams == null || alertParams.isEmpty()) {
return new AlertResult(false, "PagerDuty alert params is empty");
}

return new PagerDutySender(alertParams).sendPagerDutyAlter(alertData.getTitle(), alertData.getContent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public PagerDutySender(Map<String, String> config) {

public AlertResult sendPagerDutyAlter(String title, String content) {
AlertResult alertResult = new AlertResult();
alertResult.setStatus("false");
alertResult.setSuccess(false);
alertResult.setMessage("send pager duty alert fail.");

try {
Expand Down Expand Up @@ -83,7 +83,7 @@ private AlertResult send(AlertResult alertResult, String url, String requestBody
String responseContent = EntityUtils.toString(entity, StandardCharsets.UTF_8);
try {
if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_ACCEPTED) {
alertResult.setStatus("true");
alertResult.setSuccess(true);
alertResult.setMessage("send pager duty alert success");
} else {
alertResult.setMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ public void initDingTalkConfig() {
public void testSend() {
PagerDutySender pagerDutySender = new PagerDutySender(pagerDutyConfig);
AlertResult alertResult = pagerDutySender.sendPagerDutyAlter("pagerduty test title", "pagerduty test content");
Assertions.assertEquals("false", alertResult.getStatus());
Assertions.assertFalse(alertResult.isSuccess());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public AlertResult process(AlertInfo info) {
AlertData alertData = info.getAlertData();
Map<String, String> paramsMap = info.getAlertParams();
if (null == paramsMap) {
return new AlertResult("false", "prometheus alert manager params is null");
return new AlertResult(false, "prometheus alert manager params is null");
}
return new PrometheusAlertSender(paramsMap).sendMessage(alertData);

Expand Down
Loading

0 comments on commit a0e5210

Please sign in to comment.