-
Notifications
You must be signed in to change notification settings - Fork 941
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[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 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
1 parent
f450e2f
commit 5ecce73
Showing
10 changed files
with
231 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
...main/java/com/usthe/manager/component/alerter/impl/TelegramBotAlertNotifyHandlerImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package com.usthe.manager.component.alerter.impl; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
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.support.exception.AlertNoticeException; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
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.util.StringUtils; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.time.Instant; | ||
import java.time.ZoneId; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.ResourceBundle; | ||
|
||
/** | ||
* Send alarm information by Telegram Bot | ||
* 通过Telegram Bot发送告警信息 | ||
* | ||
* @author <a href="mailto:gcwm99@gmail.com">gcdd1993</a> | ||
* @version 2.1 | ||
* Created by Musk.Chen on 2023/1/16 | ||
*/ | ||
@Component | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
final class TelegramBotAlertNotifyHandlerImpl implements AlertNotifyHandler { | ||
private final RestTemplate restTemplate; | ||
private final ResourceBundle bundle = ResourceBundleUtil.getBundle("alerter"); | ||
private static final DateTimeFormatter DTF = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); | ||
private static final String TG_BOT_URL_TEMPLATE = "https://api.telegram.org/bot%s/sendMessage"; | ||
|
||
@Override | ||
public void send(NoticeReceiver receiver, Alert alert) throws AlertNoticeException { | ||
String url = String.format(TG_BOT_URL_TEMPLATE, receiver.getTgBotToken()); | ||
TelegramBotNotifyDTO notifyBody = TelegramBotNotifyDTO.builder() | ||
.chatId(receiver.getTgUserId()) | ||
.text(buildMessage(alert)) | ||
.disableWebPagePreview(true) | ||
.build(); | ||
try { | ||
ResponseEntity<TelegramBotNotifyResponse> entity = restTemplate.postForEntity(url, notifyBody, TelegramBotNotifyResponse.class); | ||
if (entity.getStatusCode() == HttpStatus.OK && entity.getBody() != null) { | ||
TelegramBotNotifyResponse body = entity.getBody(); | ||
if (body.ok) { | ||
log.debug("Send Telegram Bot Success"); | ||
} else { | ||
log.warn("Send Telegram Bot Failed: {}, error_code: {}", body.description, body.errorCode); | ||
throw new AlertNoticeException(body.description); | ||
} | ||
} else { | ||
log.warn("Send Telegram Bot Failed {}", entity.getBody()); | ||
throw new AlertNoticeException("Http StatusCode " + entity.getStatusCode()); | ||
} | ||
} catch (Exception e) { | ||
throw new AlertNoticeException("[Telegram Bot Notify Error] " + e.getMessage()); | ||
} | ||
} | ||
|
||
@Override | ||
public byte type() { | ||
return 7; | ||
} | ||
|
||
private String buildMessage(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); | ||
} | ||
StringBuilder content = new StringBuilder(); | ||
content.append("[").append(bundle.getString("alerter.notify.title")).append("]\n") | ||
.append(bundle.getString("alerter.notify.target")).append(" : ").append(alert.getTarget()).append("\n"); | ||
if (StringUtils.hasText(monitorId)) { | ||
content.append(bundle.getString("alerter.notify.monitorId")).append(" : ") | ||
.append(monitorId).append("\n"); | ||
} | ||
if (StringUtils.hasText(monitorName)) { | ||
content.append(bundle.getString("alerter.notify.monitorName")).append(" : ") | ||
.append(monitorName).append("\n"); | ||
} | ||
if (alert.getPriority() < CommonConstants.ALERT_PRIORITY_CODE_WARNING) { | ||
content.append(bundle.getString("alerter.notify.priority")).append(" : ") | ||
.append(bundle.getString("alerter.priority." + alert.getPriority())).append("\n"); | ||
} else { | ||
content.append(bundle.getString("alerter.notify.priority")).append(" : ") | ||
.append(bundle.getString("alerter.priority." + alert.getPriority())).append("\n"); | ||
} | ||
String triggerTime = DTF.format(Instant.ofEpochMilli(alert.getLastTriggerTime()).atZone(ZoneId.systemDefault()).toLocalDateTime()); | ||
content.append(bundle.getString("alerter.notify.triggerTime")).append(" : ") | ||
.append(triggerTime).append("\n"); | ||
content.append(bundle.getString("alerter.notify.content")).append(" : ").append(alert.getContent()); | ||
|
||
return content.toString(); | ||
} | ||
|
||
@Data | ||
@Builder | ||
private static class TelegramBotNotifyDTO { | ||
@JsonProperty("chat_id") | ||
private String chatId; | ||
private String text; | ||
@JsonProperty("disable_web_page_preview") | ||
private Boolean disableWebPagePreview; | ||
} | ||
|
||
@NoArgsConstructor | ||
@Data | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
private static class TelegramBotNotifyResponse { | ||
private boolean ok; | ||
@JsonProperty("error_code") | ||
private Integer errorCode; | ||
private String description; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
.../java/com/usthe/manager/component/alerter/impl/TelegramBotAlertNotifyHandlerImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author <a href="mailto:gcwm99@gmail.com">gcdd1993</a> | ||
* @version 2.1 | ||
* Created by Musk.Chen on 2023/1/16 | ||
*/ | ||
@Slf4j | ||
class TelegramBotAlertNotifyHandlerImplTest extends AbstractSpringIntegrationTest { | ||
|
||
@Resource | ||
private TelegramBotAlertNotifyHandlerImpl telegramBotAlertNotifyHandler; | ||
|
||
@Test | ||
void send() { | ||
String tgBotToken = System.getenv("TG_BOT_TOKEN"); | ||
String tgUserId = System.getenv("TG_USER_ID"); | ||
if (!StringUtils.hasText(tgBotToken) || !StringUtils.hasText(tgUserId)) { | ||
log.warn("Please provide environment variables TG_BOT_TOKEN, TG_USER_ID"); | ||
return; | ||
} | ||
NoticeReceiver receiver = new NoticeReceiver(); | ||
receiver.setId(1L); | ||
receiver.setName("Mock 告警"); | ||
receiver.setTgBotToken(tgBotToken); | ||
receiver.setTgUserId(tgUserId); | ||
Alert alert = new Alert(); | ||
alert.setId(1L); | ||
alert.setTarget("Mock Target"); | ||
Map<String, String> map = new HashMap<>(); | ||
map.put(CommonConstants.TAG_MONITOR_ID, "Mock monitor id"); | ||
map.put(CommonConstants.TAG_MONITOR_NAME, "Mock monitor name"); | ||
alert.setTags(map); | ||
alert.setContent("mock content"); | ||
alert.setPriority((byte) 0); | ||
alert.setLastTriggerTime(System.currentTimeMillis()); | ||
|
||
telegramBotAlertNotifyHandler.send(receiver, alert); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters