This is an automated email from the ASF dual-hosted git repository. Aias00 pushed a commit to branch 2.0.0 in repository https://gitbox.apache.org/repos/asf/hertzbeat.git
commit b7f716707010870b27bd5a0f5f2204522e99188d Author: liuhy <[email protected]> AuthorDate: Fri Jun 5 14:25:54 2026 +0800 fix(alerter): add null guards for commonLabels/commonAnnotations across SMS clients - UniSmsClientImpl: add null check for getCommonLabels() and getCommonAnnotations() to match pattern used by other 4 SMS clients (Alibaba, Aws, Tencent, Twilio) - All SMS clients: initialize instance/priority with safe defaults before null-guarded access, use getOrDefault for cleaner null handling - SmsLocalSmsClientImpl: add null guards for getCommonAnnotations() and getAlerts() - NoticeConfigServiceImpl: add null check for getCommonLabels() in rule filter These maps can legitimately be null when alerts originate from external sources that don't populate labels/annotations. Co-Authored-By: Claude Opus 4.8 <[email protected]> --- .../alert/service/impl/AlibabaSmsClientImpl.java | 14 ++++++++------ .../alert/service/impl/AwsSmsClientImpl.java | 10 ++++++---- .../alert/service/impl/NoticeConfigServiceImpl.java | 3 ++- .../alert/service/impl/SmsLocalSmsClientImpl.java | 12 ++++++++++-- .../alert/service/impl/TencentSmsClientImpl.java | 14 ++++++++------ .../alert/service/impl/TwilioSmsClientImpl.java | 12 ++++++------ .../alert/service/impl/UniSmsClientImpl.java | 19 +++++++++++++------ 7 files changed, 53 insertions(+), 31 deletions(-) diff --git a/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlibabaSmsClientImpl.java b/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlibabaSmsClientImpl.java index 34b6ebe1d0..08b361da70 100644 --- a/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlibabaSmsClientImpl.java +++ b/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlibabaSmsClientImpl.java @@ -84,12 +84,14 @@ public class AlibabaSmsClientImpl implements SmsClient { @Override public void sendMessage(NoticeReceiver receiver, NoticeTemplate noticeTemplate, GroupAlert alert) { // Extract alert info - String instance = null; - String priority = null; + String instance = alert.getGroupKey(); + String priority = "unknown"; String content = null; if (alert.getCommonLabels() != null) { - instance = alert.getCommonLabels().get("instance"); - priority = alert.getCommonLabels().get("priority"); + instance = alert.getCommonLabels().getOrDefault("instance", alert.getGroupKey()); + priority = alert.getCommonLabels().getOrDefault("priority", "unknown"); + } + if (alert.getCommonAnnotations() != null) { content = alert.getCommonAnnotations().get("summary"); content = content == null ? alert.getCommonAnnotations().get("description") : content; if (content == null) { @@ -99,8 +101,8 @@ public class AlibabaSmsClientImpl implements SmsClient { // Build template parameters Map<String, String> templateParam = new HashMap<>(); - templateParam.put("instance", instance == null ? alert.getGroupKey() : instance); - templateParam.put("priority", priority == null ? "unknown" : priority); + templateParam.put("instance", instance); + templateParam.put("priority", priority); templateParam.put("content", content); sendSms(receiver.getPhone(), JsonUtil.toJson(templateParam)); diff --git a/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AwsSmsClientImpl.java b/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AwsSmsClientImpl.java index 6e9dd601c6..fad3befc18 100644 --- a/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AwsSmsClientImpl.java +++ b/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AwsSmsClientImpl.java @@ -79,12 +79,14 @@ public class AwsSmsClientImpl implements SmsClient { @Override public void sendMessage(NoticeReceiver receiver, NoticeTemplate noticeTemplate, GroupAlert alert) { // Extract alert info - String instance = null; - String priority = null; + String instance = alert.getGroupKey(); + String priority = "unknown"; String content = null; if (alert.getCommonLabels() != null) { - instance = alert.getCommonLabels().get("instance") == null ? alert.getGroupKey() : alert.getCommonLabels().get("instance"); - priority = alert.getCommonLabels().get("priority") == null ? "unknown" : alert.getCommonLabels().get("priority"); + instance = alert.getCommonLabels().getOrDefault("instance", alert.getGroupKey()); + priority = alert.getCommonLabels().getOrDefault("priority", "unknown"); + } + if (alert.getCommonAnnotations() != null) { content = alert.getCommonAnnotations().get("summary"); content = content == null ? alert.getCommonAnnotations().get("description") : content; if (content == null) { diff --git a/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/NoticeConfigServiceImpl.java b/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/NoticeConfigServiceImpl.java index f7f62ab8b7..3d6f6abd76 100644 --- a/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/NoticeConfigServiceImpl.java +++ b/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/NoticeConfigServiceImpl.java @@ -218,7 +218,8 @@ public class NoticeConfigServiceImpl implements NoticeConfigService, CommandLine // filter labels if (rule.getLabels() != null && !rule.getLabels().isEmpty()) { boolean labelMatch = rule.getLabels().entrySet().stream().allMatch(labelItem -> { - if (!alert.getCommonLabels().containsKey(labelItem.getKey())) { + if (alert.getCommonLabels() == null + || !alert.getCommonLabels().containsKey(labelItem.getKey())) { return false; } String alertLabelValue = alert.getCommonLabels().get(labelItem.getKey()); diff --git a/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/SmsLocalSmsClientImpl.java b/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/SmsLocalSmsClientImpl.java index bcb16f6abb..b032a56feb 100644 --- a/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/SmsLocalSmsClientImpl.java +++ b/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/SmsLocalSmsClientImpl.java @@ -66,8 +66,16 @@ public class SmsLocalSmsClientImpl implements SmsClient { } try (CloseableHttpClient httpClient = HttpClients.createDefault()) { - String content = alert.getCommonAnnotations().get("summary"); - if (Objects.isNull(content) || Objects.isNull(alert.getCommonAnnotations().get("description"))) { + String content = null; + if (alert.getCommonAnnotations() != null) { + content = alert.getCommonAnnotations().get("summary"); + // Use fallback if summary is null OR description is null (original logic) + if (Objects.isNull(content) || Objects.isNull(alert.getCommonAnnotations().get("description"))) { + content = alert.getAlerts() != null && !alert.getAlerts().isEmpty() + ? alert.getAlerts().get(0).getContent() + : null; + } + } else if (alert.getAlerts() != null && !alert.getAlerts().isEmpty()) { content = alert.getAlerts().get(0).getContent(); } SmsMessage smsMessage = new SmsMessage(FROM, receiver.getPhone(), content); diff --git a/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/TencentSmsClientImpl.java b/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/TencentSmsClientImpl.java index 90ceefe5f8..bd5bfd6a96 100644 --- a/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/TencentSmsClientImpl.java +++ b/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/TencentSmsClientImpl.java @@ -79,12 +79,14 @@ public class TencentSmsClientImpl implements SmsClient { @Override public void sendMessage(NoticeReceiver receiver, NoticeTemplate noticeTemplate, GroupAlert alert) { // todo limit the number of words - String instance = null; - String priority = null; + String instance = alert.getGroupKey(); + String priority = "unknown"; String content = null; if (alert.getCommonLabels() != null) { - instance = alert.getCommonLabels().get("instance"); - priority = alert.getCommonLabels().get("priority"); + instance = alert.getCommonLabels().getOrDefault("instance", alert.getGroupKey()); + priority = alert.getCommonLabels().getOrDefault("priority", "unknown"); + } + if (alert.getCommonAnnotations() != null) { content = alert.getCommonAnnotations().get("summary"); content = content == null ? alert.getCommonAnnotations().get("description") : content; if (content == null) { @@ -93,8 +95,8 @@ public class TencentSmsClientImpl implements SmsClient { } String[] templateValues = new String[3]; - templateValues[0] = instance == null ? alert.getGroupKey() : instance; - templateValues[1] = priority == null ? "unknown" : priority; + templateValues[0] = instance; + templateValues[1] = priority; templateValues[2] = content; String[] phones = new String[1]; diff --git a/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/TwilioSmsClientImpl.java b/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/TwilioSmsClientImpl.java index 55e1fd355f..32bcf348cc 100644 --- a/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/TwilioSmsClientImpl.java +++ b/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/TwilioSmsClientImpl.java @@ -70,14 +70,14 @@ public class TwilioSmsClientImpl implements SmsClient { @Override public void sendMessage(NoticeReceiver receiver, NoticeTemplate noticeTemplate, GroupAlert alert) { - String instance = null; - String priority = null; + String instance = alert.getGroupKey(); + String priority = "unknown"; String content = null; if (alert.getCommonLabels() != null) { - instance = alert.getCommonLabels().get("instance") == null ? alert.getGroupKey() - : alert.getCommonLabels().get("instance"); - priority = alert.getCommonLabels().get("priority") == null ? "unknown" - : alert.getCommonLabels().get("priority"); + instance = alert.getCommonLabels().getOrDefault("instance", alert.getGroupKey()); + priority = alert.getCommonLabels().getOrDefault("priority", "unknown"); + } + if (alert.getCommonAnnotations() != null) { content = alert.getCommonAnnotations().get("summary"); content = content == null ? alert.getCommonAnnotations().get("description") : content; if (content == null) { diff --git a/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/UniSmsClientImpl.java b/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/UniSmsClientImpl.java index d4a414e12c..ef1ff0f380 100644 --- a/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/UniSmsClientImpl.java +++ b/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/UniSmsClientImpl.java @@ -73,12 +73,19 @@ public class UniSmsClientImpl implements SmsClient { // build template data Map<String, String> templateData = new HashMap<>(); - String instance = alert.getCommonLabels().getOrDefault("instance", alert.getGroupKey()); - String priority = alert.getCommonLabels().getOrDefault("priority", "unknown"); - String content = alert.getCommonAnnotations().get("summary"); - content = content == null ? alert.getCommonAnnotations().get("description") : content; - if (content == null) { - content = alert.getCommonAnnotations().values().stream().findFirst().orElse(null); + String instance = alert.getGroupKey(); + String priority = "unknown"; + String content = null; + if (alert.getCommonLabels() != null) { + instance = alert.getCommonLabels().getOrDefault("instance", alert.getGroupKey()); + priority = alert.getCommonLabels().getOrDefault("priority", "unknown"); + } + if (alert.getCommonAnnotations() != null) { + content = alert.getCommonAnnotations().get("summary"); + content = content == null ? alert.getCommonAnnotations().get("description") : content; + if (content == null) { + content = alert.getCommonAnnotations().values().stream().findFirst().orElse(null); + } } templateData.put("instance", instance); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
