Skip to content

Commit

Permalink
bugfix webhook post body error and alarm recover exception (#1327)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsun28 authored Nov 10, 2023
1 parent 4429068 commit 363b9d3
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import static org.dromara.hertzbeat.common.constants.CommonConstants.ALERT_STATUS_CODE_PENDING;
import static org.dromara.hertzbeat.common.constants.CommonConstants.ALERT_STATUS_CODE_SOLVED;
import static org.dromara.hertzbeat.common.constants.CommonConstants.*;

/**
* Calculate alarms based on the alarm definition rules and collected data
Expand Down Expand Up @@ -101,7 +100,12 @@ public CalculateAlarm(AlerterWorkerPool workerPool, CommonDataQueue dataQueue,
List<Monitor> monitors = monitorDao.findMonitorsByStatus(CommonConstants.UN_AVAILABLE_CODE);
if (monitors != null) {
for (Monitor monitor : monitors) {
this.notRecoveredAlertMap.put(monitor.getId() + CommonConstants.AVAILABILITY, Alert.builder().build());
HashMap<String, String> tags = new HashMap<>(8);
tags.put(TAG_MONITOR_ID, String.valueOf(monitor.getId()));
tags.put(TAG_MONITOR_NAME, monitor.getName());
tags.put(TAG_MONITOR_APP, monitor.getApp());
this.notRecoveredAlertMap.put(monitor.getId() + CommonConstants.AVAILABILITY,
Alert.builder().tags(tags).target(AVAILABILITY).status(UN_AVAILABLE_CODE).build());
}
}
startCalculate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
* Test case for {@link TelnetCollectImpl}
*/
class TelnetCollectImplTest {

@Test

void telnet() {
TelnetClient telnetClient = null;
try {
Expand All @@ -51,4 +50,4 @@ void telnet() {
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public class NoticeTemplate {
"${triggerTimeLabel} : ${triggerTime}\n" +
"${contentLabel} : ${content}", accessMode = READ_WRITE)
@Length(max = 60000)
@Lob
@NotBlank
private String content;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public void send(NoticeReceiver receiver, NoticeTemplate noticeTemplate, Alert a
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
String webhookJson = renderContent(noticeTemplate, alert);
webhookJson = webhookJson.replace(",\n }", "\n }");
HttpEntity<String> alertHttpEntity = new HttpEntity<>(webhookJson, headers);
ResponseEntity<String> entity = restTemplate.postForEntity(receiver.getHookUrl(), alertHttpEntity, String.class);
if (entity.getStatusCode().value() < HttpStatus.BAD_REQUEST.value()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.dromara.hertzbeat.manager.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hertzbeat.alert.dao.AlertDefineBindDao;
import org.dromara.hertzbeat.common.constants.CommonConstants;
Expand Down Expand Up @@ -85,8 +84,6 @@ public class MonitorServiceImpl implements MonitorService {
public static final String PATTERN_HTTP = "(?i)http://";
public static final String PATTERN_HTTPS = "(?i)https://";

private static final Gson GSON = new Gson();

@Autowired
private AppService appService;

Expand Down Expand Up @@ -734,8 +731,10 @@ public void copyMonitors(List<Long> ids) {

monitorOpt.ifPresentOrElse(monitor -> {
// deep copy original monitor to achieve persist in JPA
Monitor newMonitor = GSON.fromJson(GSON.toJson(monitor), Monitor.class);
copyMonitor(newMonitor, params);
Monitor newMonitor = JsonUtil.fromJson(JsonUtil.toJson(monitor), Monitor.class);
if (newMonitor != null) {
copyMonitor(newMonitor, params);
}
}, () -> log.warn("can not find the monitor for id :{}", id));
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class NoticeConfigServiceImpl implements NoticeConfigService, CommandLine

private static final String ALERT_TEST_TARGET = "Test Target";

private static final String ALERT_TEST_CONTENT = "test send msg! \n This is the test data. It is proved that it can be received successfully";
private static final String ALERT_TEST_CONTENT = "test send msg! \\n This is the test data. It is proved that it can be received successfully";

private static final Map<Byte, NoticeTemplate> PRESET_TEMPLATE = new HashMap<>(16);

Expand Down Expand Up @@ -244,13 +244,21 @@ public NoticeTemplate getDefaultNoticeTemplateByType(Byte type) {

@Override
public boolean sendTestMsg(NoticeReceiver noticeReceiver) {
Map<String, String> tags = new HashMap<>(8);
tags.put(CommonConstants.TAG_MONITOR_ID, "100");
tags.put(CommonConstants.TAG_MONITOR_NAME, "100Name");
tags.put(CommonConstants.TAG_THRESHOLD_ID, "200");
Alert alert = new Alert();
alert.setTags(tags);
alert.setId(100L);
alert.setTarget(ALERT_TEST_TARGET);
alert.setPriority(CommonConstants.ALERT_PRIORITY_CODE_CRITICAL);
alert.setContent(ALERT_TEST_CONTENT);
alert.setTriggerTimes(1);
alert.setAlertDefineId(200L);
alert.setTimes(2);
alert.setStatus((byte) 0);
alert.setFirstAlarmTime(System.currentTimeMillis());
alert.setLastAlarmTime(System.currentTimeMillis());
alert.setPriority(CommonConstants.ALERT_PRIORITY_CODE_CRITICAL);
return dispatcherAlarm.sendNoticeMsg(noticeReceiver, null, alert);
}

Expand Down

0 comments on commit 363b9d3

Please sign in to comment.