Skip to content

Commit

Permalink
[manager, web-app] Notification type supports slack webhook, upgrade …
Browse files Browse the repository at this point in the history
…jdk version to JDK11 (#591)

* [web-app] fix history echarts chart tooltip only show one instance (#365)

* [workflow] upgrade issues-translator version 2.7 (#367)

* [manger] add mockito unit test

* [manger] add globalExceptionHandler mockito unit test (#368)

* [manager] impl appController unit test case (#370)

* 英文注释的补充

* Supplement of English notes

* Supplementary English notes

* Supplement of English notes

* AppControllerTest Implementation of unit tests

Co-authored-by: gaoxingcun <gxc01514416@alibaba-inc.com>

* [warehouse] support Iotdb metrics data storage (#372)

* add iotdb storage (saveData part)

* implement getHistoryMetricData

* save multiple instances at the same time
implement getHistoryIntervalMetricData

* [warehouse] reformat code

* [warehouse] refactor code

* [warehouse] refactor code

* [warehouse] refactor code

* [warehouse] fix insert node path container " '  parsed error

* [warehouse] fix insert node path container " ' *  parsed error

* [warehouse] support iotdb rpc-port

* support iotdb version 0.12
fix save data and select bug

* replace Session with SessionPool

* rule fix, 修复魔法值问题

Co-authored-by: tomsun28 <tomsun28@outlook.com>
Co-authored-by: hujiaofen <hujiaofen@2dfire.com>

* [webapp,warehouse] support historical interval aggregate data chart (#373)

* [manger] add mockito unit test

* [manger] impl unit test ParamDaoTest #360

* [manager] fix NoticeRuleDaoTest

* [manger] impl unit test ParamDefineDaoTest #361

* [manger] impl unit test TagDaoTest #362

* [manger] add manger sql dao mockito unit test (#375)

* [manger] add mockito unit test

* [manger] add mockito unit test

* [manger] impl unit test ParamDaoTest #360

* [manager] fix NoticeRuleDaoTest

* [manger] impl unit test ParamDefineDaoTest #361

* [manger] impl unit test TagDaoTest #362

* [manager] I18nControllerTest and MonitorControllerTest Unit tests are written (#376)

* I18ControllerTest Completion of the test class

* MonitorControllerTest Unit tests are written

* [manger] impl unit test AppServiceTest #355

* [hertzbeat] update use PromQL to collect metrics from promethues server (#456)

* [manager] enhancement manager-monitors-page added gmtUpdate desc (#455)

* [manager-monitors](增强)应用服务检测-网站检测-分页:添加默认name升序 (enhancement)manager-monitors-page added name asc

* [manager-monitors](增强)应用服务检测-网站检测-分页:添加默认gmtUpdate降序 (enhancement)manager-monitors-page added gmtUpdate desc

* [webapp] support monitors list sort by name, date (#458)

* [manager] bugfix the gmtUpdate time not change when update monitor params (#459)

* [alerter,collector,common,warehouse] refactor auto import component

* [manager] Notification type supports telegram

* [web-app] Notification type supports telegram

* fix PMD Failure LowerCamelCaseVariableNamingRule

* fix PMD check

* fix ESLint Error

* [manager] remove useless judgment

* [manager] use thymeleaf to render notify content

* [alerter, manager] use thymeleaf to render notify content

* [manager] refactor rename method name renderContext to renderContent

* [script, doc] upgrade jdk version to JDK11

* upgrade pmd plugin to support jdk11

* [common] fix pmd check

* [manager, web-app] Notification type supports slack webhook

Co-authored-by: tomsun28 <tomsun28@outlook.com>
Co-authored-by: 铁甲小宝 <85919258+TJxiaobao@users.noreply.github.com>
Co-authored-by: gaoxingcun <gxc01514416@alibaba-inc.com>
Co-authored-by: zcx <48920254+Ceilzcx@users.noreply.github.com>
Co-authored-by: hujiaofen <hujiaofen@2dfire.com>
Co-authored-by: luxx <58515565+luxx-lq@users.noreply.github.com>
  • Loading branch information
7 people authored Jan 17, 2023
1 parent ffd4b7a commit f77d2b8
Show file tree
Hide file tree
Showing 17 changed files with 176 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public class NoticeReceiver {
@NotNull
private String name;

@Schema(title = "Notification information method: 0-SMS 1-Email 2-webhook 3-WeChat Official Account 4-Enterprise WeChat Robot 5-DingTalk Robot 6-FeiShu Robot 7-Telegram Bot",
description = "通知信息方式: 0-手机短信 1-邮箱 2-webhook 3-微信公众号 4-企业微信机器人 5-钉钉机器人 6-飞书机器人 7-Telegram机器人",
@Schema(title = "Notification information method: 0-SMS 1-Email 2-webhook 3-WeChat Official Account 4-Enterprise WeChat Robot 5-DingTalk Robot 6-FeiShu Robot 7-Telegram Bot 8-SlackWebHook",
description = "通知信息方式: 0-手机短信 1-邮箱 2-webhook 3-微信公众号 4-企业微信机器人 5-钉钉机器人 6-飞书机器人 7-Telegram机器人 8-SlackWebHook",
accessMode = READ_WRITE)
@Min(0)
@Max(8)
Expand Down Expand Up @@ -116,6 +116,12 @@ public class NoticeReceiver {
example = "779294123", accessMode = READ_WRITE)
private String tgUserId;

@Schema(title = "URL address: The notification method is valid for Slack",
description = "URL地址 : 通知方式为Slack有效",
example = "https://hooks.slack.com/services/XXXX/XXXX/XXXX", accessMode = READ_WRITE)
@Length(max = 300)
private String slackWebHookUrl;

@Schema(title = "The creator of this record", description = "此条记录创建者", example = "tom",
accessMode = READ_ONLY)
@CreatedBy
Expand Down
4 changes: 2 additions & 2 deletions common/src/main/java/com/usthe/common/util/GsonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public static <T> T fromJson(String jsonStr, Class<T> clazz) {
return gson.fromJson(jsonStr, clazz);
}

public static <T> T fromJson(String jsonStr, Type typeOfT) {
return gson.fromJson(jsonStr, typeOfT);
public static <T> T fromJson(String jsonStr, Type type) {
return gson.fromJson(jsonStr, type);
}

public static <T> T fromJson(JsonElement element, Class<T> clazz) {
Expand Down
6 changes: 3 additions & 3 deletions home/versioned_docs/version-v1.0.0/start/package-deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ sidebar_label: 安装包方式部署
安装后命令行检查是否成功安装
```
$ java -version
openjdk version "1.8.0_312"
OpenJDK Runtime Environment (Zulu 8.58.0.13-CA-macos-aarch64) (build 1.8.0_312-b07)
OpenJDK 64-Bit Server VM (Zulu 8.58.0.13-CA-macos-aarch64) (build 25.312-b07, mixed mode)
openjdk version "11.0.11" 2021-04-20
OpenJDK Runtime Environment AdoptOpenJDK-11.0.11+9 (build 11.0.11+9)
OpenJDK 64-Bit Server VM AdoptOpenJDK-11.0.11+9 (build 11.0.11+9, mixed mode)
```
2. 下载HertzBeat安装包
下载您系统环境对应的安装包
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ abstract class AbstractAlertNotifyHandlerImpl implements AlertNotifyHandler {
@Resource
protected AlerterProperties alerterProperties;

protected String renderContext(Alert alert) {
protected String renderContent(Alert alert) {
Context context = new Context();
context.setVariable("title", "[" + bundle.getString("alerter.notify.title") + "]");
if (alert.getTags() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void send(NoticeReceiver receiver, Alert alert) {
try {
DingTalkWebHookDto dingTalkWebHookDto = new DingTalkWebHookDto();
MarkdownDTO markdownDTO = new MarkdownDTO();
markdownDTO.setText(renderContext(alert));
markdownDTO.setText(renderContent(alert));
markdownDTO.setTitle(bundle.getString("alerter.notify.title"));
dingTalkWebHookDto.setMarkdown(markdownDTO);
String webHookUrl = alerterProperties.getDingTalkWebHookUrl() + receiver.getAccessToken();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void send(NoticeReceiver receiver, Alert alert) {
List<FlyBookContent> contents1 = new ArrayList<>();
FlyBookContent flyBookContent = new FlyBookContent();
flyBookContent.setTag("text");
flyBookContent.setText(renderContext(alert));
flyBookContent.setText(renderContent(alert));
contents1.add(flyBookContent);
FlyBookContent bookContent = new FlyBookContent();
bookContent.setTag("a");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.usthe.manager.component.alerter.impl;

import com.usthe.common.entity.alerter.Alert;
import com.usthe.common.entity.manager.NoticeReceiver;
import com.usthe.manager.support.exception.AlertNoticeException;
import lombok.Builder;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Objects;

/**
* Send alarm information by Slack Webhook
* 通过Slack Webhook发送告警信息
*
* @author <a href="mailto:gcwm99@gmail.com">gcdd1993</a>
* @version 2.1
* Created by Musk.Chen on 2023/1/17
*/
@Component
@RequiredArgsConstructor
@Slf4j
final class SlackAlertNotifyHandlerImpl extends AbstractAlertNotifyHandlerImpl {
private static final String SUCCESS = "ok";
private final RestTemplate restTemplate;

@Override
public void send(NoticeReceiver receiver, Alert alert) throws AlertNoticeException {
try {
var slackNotify = SlackNotifyDTO.builder()
.text(renderContent(alert))
.build();

var entity = restTemplate.postForEntity(receiver.getSlackWebHookUrl(), slackNotify, String.class);
if (entity.getStatusCode() == HttpStatus.OK && entity.getBody() != null) {
var body = entity.getBody();
if (Objects.equals(SUCCESS, body)) {
log.debug("Send Slack Success");
} else {
log.warn("Send Slack Failed: {}", body);
throw new AlertNoticeException(body);
}
} else {
log.warn("Send Slack Failed {}", entity.getBody());
throw new AlertNoticeException("Http StatusCode " + entity.getStatusCode());
}
} catch (Exception e) {
throw new AlertNoticeException("[Slack Notify Error] " + e.getMessage());
}
}

@Override
public byte type() {
return 8;
}

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

@Data
@Builder
private static class SlackNotifyDTO {
private String text;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void send(NoticeReceiver receiver, Alert alert) throws AlertNoticeExcepti
String url = String.format(alerterProperties.getTelegramBotApiUrl(), receiver.getTgBotToken());
TelegramBotNotifyDTO notifyBody = TelegramBotNotifyDTO.builder()
.chatId(receiver.getTgUserId())
.text(renderContext(alert))
.text(renderContent(alert))
.disableWebPagePreview(true)
.build();
ResponseEntity<TelegramBotNotifyResponse> entity = restTemplate.postForEntity(url, notifyBody, TelegramBotNotifyResponse.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void send(NoticeReceiver receiver, Alert alert) {
try {
WeWorkWebHookDto weWorkWebHookDTO = new WeWorkWebHookDto();
WeWorkWebHookDto.MarkdownDTO markdownDTO = new WeWorkWebHookDto.MarkdownDTO();
markdownDTO.setContent(renderContext(alert));
markdownDTO.setContent(renderContent(alert));
weWorkWebHookDTO.setMarkdown(markdownDTO);
String webHookUrl = alerterProperties.getWeWorkWebHookUrl() + receiver.getWechatId();
ResponseEntity<CommonRobotNotifyResp> entity = restTemplate.postForEntity(webHookUrl, weWorkWebHookDTO, CommonRobotNotifyResp.class);
Expand Down
7 changes: 7 additions & 0 deletions manager/src/main/resources/templates/alertNotifySlack.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*[(${title})]*
[(${targetLabel})] : [(${target})]
[# th:if="${monitorId != null}"][(${monitorIdLabel})] : `[(${monitorId})]`[/]
[# th:if="${monitorName != null}"][(${monitorNameLabel})] : [(${monitorName})][/]
[(${priorityLabel})] : [(${priority})]
[(${triggerTimeLabel})] : [(${triggerTime})]
[(${contentLabel})] : [(${content})]
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.usthe.manager.component.alerter.impl;

import com.usthe.common.entity.alerter.Alert;
import com.usthe.common.entity.manager.NoticeReceiver;
import com.usthe.common.util.CommonConstants;
import com.usthe.manager.AbstractSpringIntegrationTest;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Map;

/**
* Test case for {@link SlackAlertNotifyHandlerImpl}
*
* @author <a href="mailto:gcwm99@gmail.com">gcdd1993</a>
* @version 2.1
* Created by Musk.Chen on 2023/1/17
*/
@Slf4j
class SlackAlertNotifyHandlerImplTest extends AbstractSpringIntegrationTest {

@Resource
private SlackAlertNotifyHandlerImpl slackAlertNotifyHandler;

@Test
void send() {
var slackWebHook = System.getenv("SLACK_WEB_HOOK");
if (!StringUtils.hasText(slackWebHook)) {
log.warn("Please provide environment variables SLACK_WEB_HOOK");
return;
}
var receiver = new NoticeReceiver();
receiver.setId(1L);
receiver.setName("Mock 告警");
receiver.setSlackWebHookUrl(slackWebHook);
var alert = new Alert();
alert.setId(1L);
alert.setTarget("Mock Target");
var map = Map.of(
CommonConstants.TAG_MONITOR_ID, "Mock monitor id",
CommonConstants.TAG_MONITOR_NAME, "Mock monitor name"
);
alert.setTags(map);
alert.setContent("mock content");
alert.setPriority((byte) 0);
alert.setLastTriggerTime(System.currentTimeMillis());

slackAlertNotifyHandler.send(receiver, alert);
}
}
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
</modules>

<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>11</java.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<slf4j.version>1.7.21</slf4j.version>
<xml.bind.version>2.3.0</xml.bind.version>
Expand Down Expand Up @@ -94,7 +94,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.8</version>
<version>3.12.0</version>
<configuration>
<linkXRef>false</linkXRef>
<rulesets>
Expand Down Expand Up @@ -124,7 +124,7 @@
<dependency>
<groupId>com.alibaba.p3c</groupId>
<artifactId>p3c-pmd</artifactId>
<version>1.3.0</version>
<version>2.0.0</version>
</dependency>
</dependencies>
</plugin>
Expand Down
1 change: 1 addition & 0 deletions web-app/src/app/pojo/NoticeReceiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export class NoticeReceiver {
email!: string;
tgBotToken!: string;
tgUserId!: string;
slackWebHookUrl!: string;
hookUrl!: string;
wechatId!: string;
accessToken!: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@
<i nz-icon nzType="notification" nzTheme="outline"></i>
<span>{{ 'alert.notice.type.telegram-bot' | i18n }}</span>
</nz-tag>
<nz-tag *ngIf="data.type == 8" nzColor="orange">
<i nz-icon nzType="notification" nzTheme="outline"></i>
<span>{{ 'alert.notice.type.slack' | i18n }}</span>
</nz-tag>
</td>
<td nzAlign="center">
<span *ngIf="data.type == 0">{{ data.phone }}</span>
Expand All @@ -96,6 +100,7 @@
<span *ngIf="data.type == 5">{{ data.accessToken }}</span>
<span *ngIf="data.type == 6">{{ data.wechatId }}</span>
<span *ngIf="data.type == 7">{{ data.tgBotToken }}<br />{{ data.tgUserId }}</span>
<span *ngIf="data.type == 8">{{ data.slackWebHookUrl }}</span>
</td>
<td nzAlign="center">{{ (data.gmtUpdate ? data.gmtUpdate : data.gmtCreate) | date : 'YYYY-MM-dd HH:mm:ss' }}</td>
<td nzAlign="center">
Expand Down Expand Up @@ -225,6 +230,7 @@
<nz-option [nzValue]="5" [nzLabel]="'alert.notice.type.ding' | i18n"></nz-option>
<nz-option [nzValue]="6" [nzLabel]="'alert.notice.type.fei-shu' | i18n"></nz-option>
<nz-option [nzValue]="7" [nzLabel]="'alert.notice.type.telegram-bot' | i18n"></nz-option>
<nz-option [nzValue]="8" [nzLabel]="'alert.notice.type.slack' | i18n"></nz-option>
</nz-select>
</nz-form-control>
</nz-form-item>
Expand Down Expand Up @@ -291,7 +297,15 @@
'alert.notice.type.telegram-bot-user-id' | i18n
}}</nz-form-label>
<nz-form-control [nzSpan]="12" [nzErrorTip]="'validation.required' | i18n">
<input [(ngModel)]="receiver.tgUserId" nz-input [required]="receiver.type === 7" name="wechatId" type="text" />
<input [(ngModel)]="receiver.tgUserId" nz-input [required]="receiver.type === 7" name="tgUserId" type="text" />
</nz-form-control>
</nz-form-item>
<nz-form-item *ngIf="receiver.type === 8">
<nz-form-label [nzSpan]="7" nzFor="slackWebHookUrl" [nzRequired]="receiver.type === 8">{{
'alert.notice.type.slack-webHook-url' | i18n
}}</nz-form-label>
<nz-form-control [nzSpan]="12" [nzErrorTip]="'validation.required' | i18n">
<input [(ngModel)]="receiver.slackWebHookUrl" nz-input [required]="receiver.type === 8" name="slackWebHookUrl" type="text" />
</nz-form-control>
</nz-form-item>
<nz-form-item>
Expand Down
2 changes: 2 additions & 0 deletions web-app/src/assets/i18n/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@
"alert.notice.type.telegram-bot": "Telegram Bot",
"alert.notice.type.telegram-bot-token": "Telegram Bot Token",
"alert.notice.type.telegram-bot-user-id": "Telegram User Id",
"alert.notice.type.slack": "Slack WebHook",
"alert.notice.type.slack-webHook-url": "Slack WebHook Url",
"alert.notice.rule": "Alert Notice Policy",
"alert.notice.rule.new": "New Notice Policy",
"alert.notice.rule.edit": "Edit Notice Policy",
Expand Down
2 changes: 2 additions & 0 deletions web-app/src/assets/i18n/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@
"alert.notice.type.telegram-bot": "Telegram机器人",
"alert.notice.type.telegram-bot-token": "Telegram Bot Token",
"alert.notice.type.telegram-bot-user-id": "Telegram User Id",
"alert.notice.type.slack": "Slack WebHook",
"alert.notice.type.slack-webHook-url": "Slack WebHook地址",
"alert.notice.rule": "告警通知策略",
"alert.notice.rule.new": "新增通知策略",
"alert.notice.rule.edit": "编辑通知策略",
Expand Down
2 changes: 2 additions & 0 deletions web-app/src/assets/i18n/zh-TW.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@
"alert.notice.type.telegram-bot": "Telegram機器人",
"alert.notice.type.telegram-bot-token": "Telegram Bot Token",
"alert.notice.type.telegram-bot-user-id": "Telegram User Id",
"alert.notice.type.slack": "Slack WebHook",
"alert.notice.type.slack-webHook-url": "Slack WebHook地址",
"alert.notice.rule": "告警通知策略",
"alert.notice.rule.new": "新增通知策略",
"alert.notice.rule.edit": "編輯通知策略",
Expand Down

0 comments on commit f77d2b8

Please sign in to comment.