DingtalkHookCallback implements AlarmCallback { @Override public void doAlarm(List<AlarmMessage> alarmMessages) { if (this.alarmRulesWatcher.getDingtalkSettings() == null || this.alarmRulesWatcher.getDingtalkSettings().getWebhooks().isEmpty()) { return; } CloseableHttpClient httpClient = HttpClients.custom().build(); try { DingtalkSettings dingtalkSettings = this.alarmRulesWatcher.getDingtalkSettings(); dingtalkSettings.getWebhooks().forEach(webHookUrl -> { alarmMessages.forEach(alarmMessage -> { String url = getUrl(webHookUrl); String requestBody = String.format( this.alarmRulesWatcher.getDingtalkSettings().getTextTemplate(), alarmMessage.getAlarmMessage() ); sendAlarmMessage(httpClient, url, requestBody); }); }); } finally { try { httpClient.close(); } catch (IOException e) { log.error(e.getMessage(), e); } } } DingtalkHookCallback(final AlarmRulesWatcher alarmRulesWatcher); @Override void doAlarm(List<AlarmMessage> alarmMessages);  }
@Test public void testDingtalkWebhookWithoutSign() { List<DingtalkSettings.WebHookUrl> webHooks = new ArrayList<>(); webHooks.add(new DingtalkSettings.WebHookUrl("", "http: Rules rules = new Rules(); String template = "{\"msgtype\":\"text\",\"text\":{\"content\":\"Skywaling alarm: %s\"}}"; rules.setDingtalks(DingtalkSettings.builder().webhooks(webHooks).textTemplate(template).build()); AlarmRulesWatcher alarmRulesWatcher = new AlarmRulesWatcher(rules, null); DingtalkHookCallback dingtalkCallBack = new DingtalkHookCallback(alarmRulesWatcher); List<AlarmMessage> alarmMessages = new ArrayList<>(2); AlarmMessage alarmMessage = new AlarmMessage(); alarmMessage.setScopeId(DefaultScopeDefine.ALL); alarmMessage.setRuleName("service_resp_time_rule"); alarmMessage.setAlarmMessage("alarmMessage with [DefaultScopeDefine.All]"); alarmMessages.add(alarmMessage); AlarmMessage anotherAlarmMessage = new AlarmMessage(); anotherAlarmMessage.setRuleName("service_resp_time_rule_2"); anotherAlarmMessage.setScopeId(DefaultScopeDefine.ENDPOINT); anotherAlarmMessage.setAlarmMessage("anotherAlarmMessage with [DefaultScopeDefine.Endpoint]"); alarmMessages.add(anotherAlarmMessage); dingtalkCallBack.doAlarm(alarmMessages); Assert.assertTrue(isSuccess); }
@Test public void testDingtalkWebhookWithSign() { checkSign = true; List<DingtalkSettings.WebHookUrl> webHooks = new ArrayList<>(); webHooks.add(new DingtalkSettings.WebHookUrl(secret, "http: Rules rules = new Rules(); String template = "{\"msgtype\":\"text\",\"text\":{\"content\":\"Skywaling alarm: %s\"}}"; rules.setDingtalks(DingtalkSettings.builder().webhooks(webHooks).textTemplate(template).build()); AlarmRulesWatcher alarmRulesWatcher = new AlarmRulesWatcher(rules, null); DingtalkHookCallback dingtalkCallBack = new DingtalkHookCallback(alarmRulesWatcher); List<AlarmMessage> alarmMessages = new ArrayList<>(2); AlarmMessage alarmMessage = new AlarmMessage(); alarmMessage.setScopeId(DefaultScopeDefine.ALL); alarmMessage.setRuleName("service_resp_time_rule"); alarmMessage.setAlarmMessage("alarmMessage with [DefaultScopeDefine.All]"); alarmMessages.add(alarmMessage); AlarmMessage anotherAlarmMessage = new AlarmMessage(); anotherAlarmMessage.setRuleName("service_resp_time_rule_2"); anotherAlarmMessage.setScopeId(DefaultScopeDefine.ENDPOINT); anotherAlarmMessage.setAlarmMessage("anotherAlarmMessage with [DefaultScopeDefine.Endpoint]"); alarmMessages.add(anotherAlarmMessage); dingtalkCallBack.doAlarm(alarmMessages); Assert.assertTrue(isSuccess); }