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

[alerter, manager] use thymeleaf to render notify content #583

Merged
merged 40 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
f5b11b4
[web-app] fix history echarts chart tooltip only show one instance (#…
tomsun28 Oct 18, 2022
edf4aef
[workflow] upgrade issues-translator version 2.7 (#367)
tomsun28 Oct 18, 2022
3f21fe5
[manger] add mockito unit test
gcdd1993 Oct 19, 2022
cf89ca7
[manger] add globalExceptionHandler mockito unit test (#368)
gcdd1993 Oct 20, 2022
4415262
[manager] impl appController unit test case (#370)
TJxiaobao Oct 20, 2022
e5f3ec5
[warehouse] support Iotdb metrics data storage (#372)
Ceilzcx Oct 20, 2022
ddb0e50
[webapp,warehouse] support historical interval aggregate data chart (…
tomsun28 Oct 20, 2022
bee8bd8
[manger] add mockito unit test
gcdd1993 Oct 20, 2022
5188491
[manger] impl unit test ParamDaoTest #360
gcdd1993 Oct 20, 2022
a5ebb9d
[manager] fix NoticeRuleDaoTest
gcdd1993 Oct 20, 2022
6e425a1
[manger] impl unit test ParamDefineDaoTest #361
gcdd1993 Oct 20, 2022
2bd4593
[manger] impl unit test TagDaoTest #362
gcdd1993 Oct 20, 2022
7bd871d
Merge pull request #1 from gcdd1993/master
gcdd1993 Oct 20, 2022
8e8a1ca
[manger] add manger sql dao mockito unit test (#375)
gcdd1993 Oct 21, 2022
ca8ce26
[manager] I18nControllerTest and MonitorControllerTest Unit tests are…
TJxiaobao Oct 21, 2022
b46cb21
Merge branch 'dromara:dev' into dev
gcdd1993 Oct 21, 2022
73e31e2
[manger] impl unit test AppServiceTest #355
gcdd1993 Oct 21, 2022
cb578f1
Merge branch 'dev' of git.zhlh6.cn:gcdd1993/hertzbeat into dev
gcdd1993 Oct 21, 2022
f58d2ad
Merge branch 'dev' of git.zhlh6.cn:dromara/hertzbeat into dev
gcdd1993 Nov 21, 2022
d341bfc
[hertzbeat] update use PromQL to collect metrics from promethues serv…
tomsun28 Nov 21, 2022
37c2e97
[manager] enhancement manager-monitors-page added gmtUpdate desc (#455)
luxx-lq Nov 22, 2022
082edb4
[webapp] support monitors list sort by name, date (#458)
tomsun28 Nov 22, 2022
8a2778c
[manager] bugfix the gmtUpdate time not change when update monitor pa…
tomsun28 Nov 22, 2022
1dc2c4f
Merge branch 'dromara:dev' into dev
gcdd1993 Nov 23, 2022
8088eba
Merge branch 'dev' of git.zhlh6.cn:dromara/hertzbeat into dev
gcdd1993 Dec 5, 2022
d4f9fcb
Merge branch 'dromara:dev' into dev
gcdd1993 Jan 14, 2023
822fe61
Merge branch 'dev' of git.zhlh6.cn:dromara/hertzbeat into dev
gcdd1993 Jan 14, 2023
a171559
[alerter,collector,common,warehouse] refactor auto import component
gcdd1993 Jan 14, 2023
c1d1e25
[manager] Notification type supports telegram
gcdd1993 Jan 16, 2023
bf76df3
[web-app] Notification type supports telegram
gcdd1993 Jan 16, 2023
c7b5b06
Merge branch 'dromara:dev' into dev
gcdd1993 Jan 16, 2023
7010b13
fix PMD Failure LowerCamelCaseVariableNamingRule
gcdd1993 Jan 16, 2023
ae98f6b
fix PMD check
gcdd1993 Jan 16, 2023
273fe08
Merge branch 'dev' of git.zhlh6.cn:gcdd1993/hertzbeat into dev
gcdd1993 Jan 16, 2023
ded1c52
fix ESLint Error
gcdd1993 Jan 16, 2023
a5eb9db
[manager] remove useless judgment
gcdd1993 Jan 16, 2023
cd2af08
Merge branch 'dev' of git.zhlh6.cn:dromara/hertzbeat into dev
gcdd1993 Jan 16, 2023
db3c4eb
[manager] use thymeleaf to render notify content
gcdd1993 Jan 16, 2023
7e574c9
[alerter, manager] use thymeleaf to render notify content
gcdd1993 Jan 16, 2023
a31e207
Merge branch 'dev' of git.zhlh6.cn:dromara/hertzbeat into dev
gcdd1993 Jan 16, 2023
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
117 changes: 13 additions & 104 deletions alerter/src/main/java/com/usthe/alert/AlerterProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package com.usthe.alert;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

Expand All @@ -28,6 +30,8 @@
*/
@Component
@ConfigurationProperties(prefix = "alerter")
@Getter
@Setter
public class AlerterProperties {

/**
Expand All @@ -51,6 +55,11 @@ public class AlerterProperties {
*/
private String flyBookWebHookUrl = "https://open.feishu.cn/open-apis/bot/v2/hook/";

/**
* Telegram Bot api url
*/
private String telegramBotApiUrl = "https://api.telegram.org/bot%s/sendMessage";

/**
* 告警评估时间间隔起始基数 每下一次乘2 单位毫秒
* base of alert eval interval time, unit:ms. The next time is 2 times the previous time.
Expand All @@ -74,90 +83,21 @@ public class AlerterProperties {
*/
private EntranceProperties entrance;

public String getConsoleUrl() {
return consoleUrl;
}

public void setConsoleUrl(String url) {
this.consoleUrl = url;
}

public String getWeWorkWebHookUrl() {
return weWorkWebHookUrl;
}

public void setWeWorkWebHookUrl(String weWorkWebHookUrl) {
this.weWorkWebHookUrl = weWorkWebHookUrl;
}

public String getDingTalkWebHookUrl() {
return dingTalkWebHookUrl;
}

public void setDingTalkWebHookUrl(String dingTalkWebHookUrl) {
this.dingTalkWebHookUrl = dingTalkWebHookUrl;
}

public String getFlyBookWebHookUrl() {
return flyBookWebHookUrl;
}

public void setFlyBookWebHookUrl(String flyBookWebHookUrl) {
this.flyBookWebHookUrl = flyBookWebHookUrl;
}

public long getAlertEvalIntervalBase() {
return alertEvalIntervalBase;
}

public void setAlertEvalIntervalBase(long alertEvalIntervalBase) {
this.alertEvalIntervalBase = alertEvalIntervalBase;
}

public long getMaxAlertEvalInterval() {
return maxAlertEvalInterval;
}

public void setMaxAlertEvalInterval(long maxAlertEvalInterval) {
this.maxAlertEvalInterval = maxAlertEvalInterval;
}

public int getSystemAlertTriggerTimes() {
return systemAlertTriggerTimes;
}

public void setSystemAlertTriggerTimes(int systemAlertTriggerTimes) {
this.systemAlertTriggerTimes = systemAlertTriggerTimes;
}

public EntranceProperties getEntrance() {
return entrance;
}

public void setEntrance(EntranceProperties entrance) {
this.entrance = entrance;
}


/**
* Data entry configuration properties 数据入口配置属性
* The entry can obtain data from messaging middleware such as kafka rabbitmq rocketmq 入口可以是从kafka rabbitmq rocketmq等消息中间件获取数据
*/
@Getter
@Setter
public static class EntranceProperties {

/**
* kafka configuration information kafka配置信息
*/
private KafkaProperties kafka;

public KafkaProperties getKafka() {
return kafka;
}

public void setKafka(KafkaProperties kafka) {
this.kafka = kafka;
}

@Getter
@Setter
public static class KafkaProperties {
/**
* Whether the kafka data entry is started kafka数据入口是否启动
Expand All @@ -177,37 +117,6 @@ public static class KafkaProperties {
*/
private String groupId;

public boolean isEnabled() {
return enabled;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

public String getServers() {
return servers;
}

public void setServers(String servers) {
this.servers = servers;
}

public String getTopic() {
return topic;
}

public void setTopic(String topic) {
this.topic = topic;
}

public String getGroupId() {
return groupId;
}

public void setGroupId(String groupId) {
this.groupId = groupId;
}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ public class NoticeReceiver {
@Length(max = 300)
private String hookUrl;

@Schema(title = "openId : The notification method is valid for WeChat official account or enterprise WeChat robot",
description = "openId : 通知方式为微信公众号或企业微信机器人有效",
@Schema(title = "openId : The notification method is valid for WeChat official account, enterprise WeChat robot or FlyBook robot",
description = "openId : 通知方式为微信公众号,企业微信机器人或飞书机器人有效",
example = "343432", accessMode = READ_WRITE)
@Length(max = 300)
private String wechatId;
Expand Down
7 changes: 7 additions & 0 deletions common/src/main/java/com/usthe/common/util/CommonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,11 @@ public static String getMessageFromThrowable(Throwable throwable) {
return message;
}

public static String removeBlankLine(String value) {
if (value == null) {
return null;
}
return value.replaceAll("(?m)^\\s*$(\\n|\\r\\n)", "");
}

}
1 change: 1 addition & 0 deletions manager/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
<include>*.xml</include>
<include>banner.txt</include>
<include>define/**</include>
<include>templates/**</include>
<include>**/*.html</include>
</includes>
</resource>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.usthe.manager.component.alerter.impl;

import com.usthe.alert.AlerterProperties;
import com.usthe.common.entity.alerter.Alert;
import com.usthe.common.util.CommonUtil;
import com.usthe.common.util.ResourceBundleUtil;
import com.usthe.manager.component.alerter.AlertNotifyHandler;
import org.springframework.web.client.RestTemplate;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.annotation.Resource;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ResourceBundle;

/**
* @author <a href="mailto:gcwm99@gmail.com">gcdd1993</a>
* @version 2.1
* Created by Musk.Chen on 2023/1/16
*/
abstract class AbstractAlertNotifyHandlerImpl implements AlertNotifyHandler {

protected final ResourceBundle bundle = ResourceBundleUtil.getBundle("alerter");
protected static final DateTimeFormatter DTF = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

@Resource
protected TemplateEngine templateEngine;

@Resource
protected RestTemplate restTemplate;

@Resource
protected AlerterProperties alerterProperties;

protected String renderContext(Alert alert) {
Context context = new Context();
context.setVariable("title", "[" + bundle.getString("alerter.notify.title") + "]");
if (alert.getTags() != null) {
alert.getTags().forEach(context::setVariable);
}
context.setVariable("monitorIdLabel", bundle.getString("alerter.notify.monitorId"));
context.setVariable("monitorNameLabel", bundle.getString("alerter.notify.monitorName"));
context.setVariable("targetLabel", bundle.getString("alerter.notify.target"));
context.setVariable("target", alert.getTarget());

context.setVariable("priorityLabel", bundle.getString("alerter.notify.priority"));
context.setVariable("priority", bundle.getString("alerter.priority." + alert.getPriority()));

context.setVariable("triggerTimeLabel", bundle.getString("alerter.notify.triggerTime"));
context.setVariable("triggerTime", DTF.format(Instant.ofEpochMilli(alert.getLastTriggerTime()).atZone(ZoneId.systemDefault()).toLocalDateTime()));

context.setVariable("contentLabel", bundle.getString("alerter.notify.content"));
context.setVariable("content", alert.getContent());

return CommonUtil.removeBlankLine(templateEngine.process(templateName(), context));
}

/**
* Get the Thymeleaf template name
* 获取Thymeleaf模板名称
*
* @return Thymeleaf模板名称
*/
protected abstract String templateName();

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,15 @@

package com.usthe.manager.component.alerter.impl;

import com.usthe.alert.AlerterProperties;
import com.usthe.common.entity.alerter.Alert;
import com.usthe.common.entity.manager.NoticeReceiver;
import com.usthe.common.util.CommonConstants;
import com.usthe.common.util.ResourceBundleUtil;
import com.usthe.manager.component.alerter.AlertNotifyHandler;
import com.usthe.manager.pojo.dto.DingTalkWebHookDto;
import com.usthe.manager.support.exception.AlertNoticeException;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ResourceBundle;

/**
* Send alarm information through DingTalk robot
Expand All @@ -46,48 +37,17 @@
@Component
@RequiredArgsConstructor
@Slf4j
final class DingTalkRobotAlertNotifyHandlerImpl implements AlertNotifyHandler {

private final RestTemplate restTemplate;

private final AlerterProperties alerterProperties;

private ResourceBundle bundle = ResourceBundleUtil.getBundle("alerter");
final class DingTalkRobotAlertNotifyHandlerImpl extends AbstractAlertNotifyHandlerImpl {

@Override
public void send(NoticeReceiver receiver, Alert alert) {
String monitorId = null;
String monitorName = null;
if (alert.getTags() != null) {
monitorId = alert.getTags().get(CommonConstants.TAG_MONITOR_ID);
monitorName = alert.getTags().get(CommonConstants.TAG_MONITOR_NAME);
}
DingTalkWebHookDto dingTalkWebHookDto = new DingTalkWebHookDto();
DingTalkWebHookDto.MarkdownDTO markdownDTO = new DingTalkWebHookDto.MarkdownDTO();
StringBuilder contentBuilder = new StringBuilder("#### [" + bundle.getString("alerter.notify.title")
+ "]\n##### **" + bundle.getString("alerter.notify.target") + "** : " +
alert.getTarget() + "\n ");
if (monitorId != null) {
contentBuilder.append("##### **").append(bundle.getString("alerter.notify.monitorId"))
.append("** : ").append(monitorId).append("\n ");
}
if (monitorName != null) {
contentBuilder.append("##### **").append(bundle.getString("alerter.notify.monitorName"))
.append("** : ").append(monitorName).append("\n ");
}
contentBuilder.append("##### **").append(bundle.getString("alerter.notify.priority"))
.append("** : ").append(bundle.getString("alerter.priority." + alert.getPriority())).append("\n ");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String triggerTime = simpleDateFormat.format(new Date(alert.getLastTriggerTime()));
contentBuilder.append("##### **").append(bundle.getString("alerter.notify.triggerTime"))
.append("** : ").append(triggerTime).append("\n ");
contentBuilder.append("##### **").append(bundle.getString("alerter.notify.content"))
.append("** : ").append(alert.getContent());
markdownDTO.setText(contentBuilder.toString());
markdownDTO.setTitle(bundle.getString("alerter.notify.title"));
dingTalkWebHookDto.setMarkdown(markdownDTO);
String webHookUrl = alerterProperties.getDingTalkWebHookUrl() + receiver.getAccessToken();
try {
DingTalkWebHookDto dingTalkWebHookDto = new DingTalkWebHookDto();
MarkdownDTO markdownDTO = new MarkdownDTO();
markdownDTO.setText(renderContext(alert));
markdownDTO.setTitle(bundle.getString("alerter.notify.title"));
dingTalkWebHookDto.setMarkdown(markdownDTO);
String webHookUrl = alerterProperties.getDingTalkWebHookUrl() + receiver.getAccessToken();
ResponseEntity<CommonRobotNotifyResp> entity = restTemplate.postForEntity(webHookUrl,
dingTalkWebHookDto, CommonRobotNotifyResp.class);
if (entity.getStatusCode() == HttpStatus.OK) {
Expand All @@ -111,4 +71,45 @@ public void send(NoticeReceiver receiver, Alert alert) {
public byte type() {
return 5;
}

@Override
protected String templateName() {
return "alertNotifyDingTalkRobot";
}

/**
* 钉钉机器人请求消息体
*
* @author 花城
* @version 1.0
* @date 2022/2/21 6:55 下午
*/
@Data
private static class DingTalkWebHookDto {
private static final String MARKDOWN = "markdown";

/**
* 消息类型
*/
private String msgtype = MARKDOWN;

/**
* markdown消息
*/
private MarkdownDTO markdown;

}

@Data
private static class MarkdownDTO {
/**
* 消息内容
*/
private String text;
/**
* 消息标题
*/
private String title;
}

}
Loading