未验证 提交 b51dba5c 编写于 作者: X xbkaishui 提交者: GitHub

Fix null pointer bug, format log message (#5509)

Co-authored-by: wu-sheng's avatar吴晟 Wu Sheng <wu.sheng@foxmail.com>
上级 f80d8519
...@@ -63,7 +63,7 @@ public class SlackhookCallback implements AlarmCallback { ...@@ -63,7 +63,7 @@ public class SlackhookCallback implements AlarmCallback {
@Override @Override
public void doAlarm(List<AlarmMessage> alarmMessages) { public void doAlarm(List<AlarmMessage> alarmMessages) {
if (this.alarmRulesWatcher.getSlackSettings().getWebhooks().isEmpty()) { if (this.alarmRulesWatcher.getSlackSettings() == null || this.alarmRulesWatcher.getSlackSettings().getWebhooks().isEmpty()) {
return; return;
} }
...@@ -77,31 +77,26 @@ public class SlackhookCallback implements AlarmCallback { ...@@ -77,31 +77,26 @@ public class SlackhookCallback implements AlarmCallback {
StringEntity entity; StringEntity entity;
try { try {
JsonObject jsonObject = new JsonObject(); JsonObject jsonObject = new JsonObject();
JsonArray jsonElements = new JsonArray(); JsonArray jsonElements = new JsonArray();
alarmMessages.forEach(item -> { alarmMessages.forEach(item -> {
jsonElements.add(GSON.fromJson( jsonElements.add(GSON.fromJson(
String.format( String.format(
this.alarmRulesWatcher.getSlackSettings().getTextTemplate(), item.getAlarmMessage() this.alarmRulesWatcher.getSlackSettings().getTextTemplate(), item.getAlarmMessage()
), JsonObject.class)); ), JsonObject.class));
}); });
jsonObject.add("blocks", jsonElements); jsonObject.add("blocks", jsonElements);
entity = new StringEntity(GSON.toJson(jsonObject), ContentType.APPLICATION_JSON); entity = new StringEntity(GSON.toJson(jsonObject), ContentType.APPLICATION_JSON);
post.setEntity(entity); post.setEntity(entity);
CloseableHttpResponse httpResponse = httpClient.execute(post); CloseableHttpResponse httpResponse = httpClient.execute(post);
StatusLine statusLine = httpResponse.getStatusLine(); StatusLine statusLine = httpResponse.getStatusLine();
if (statusLine != null && statusLine.getStatusCode() != HttpStatus.SC_OK) { if (statusLine != null && statusLine.getStatusCode() != HttpStatus.SC_OK) {
log.error("send alarm to " + url + " failure. Response code: " + statusLine.getStatusCode()); log.error("Send slack alarm to {} failure. Response code: {}", url , statusLine.getStatusCode());
} }
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
log.error("Alarm to JSON error, " + e.getMessage(), e); log.error("Alarm to JSON error, {} ", e.getMessage(), e);
} catch (IOException e) { } catch (IOException e) {
log.error("send alarm to " + url + " failure.", e); log.error("Send slack alarm to {} failure.", url , e);
} }
}); });
} finally { } finally {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册