From cfa8bb6f2783cf295c477f36bf74ea4abb8b27d4 Mon Sep 17 00:00:00 2001 From: tomsun28 Date: Mon, 26 Jun 2023 20:06:06 +0800 Subject: [PATCH] bugfix alert tag match missing (#1038) --- .../hertzbeat/alert/dao/AlertMonitorDao.java | 12 +++++++++- .../alert/reduce/AlarmCommonReduce.java | 23 +++++++++++++++++++ .../alerter/impl/DbAlertStoreHandlerImpl.java | 8 ------- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/alerter/src/main/java/org/dromara/hertzbeat/alert/dao/AlertMonitorDao.java b/alerter/src/main/java/org/dromara/hertzbeat/alert/dao/AlertMonitorDao.java index 1a68f79b3be..7be33e0f3eb 100644 --- a/alerter/src/main/java/org/dromara/hertzbeat/alert/dao/AlertMonitorDao.java +++ b/alerter/src/main/java/org/dromara/hertzbeat/alert/dao/AlertMonitorDao.java @@ -18,8 +18,11 @@ package org.dromara.hertzbeat.alert.dao; import org.dromara.hertzbeat.common.entity.manager.Monitor; +import org.dromara.hertzbeat.common.entity.manager.Tag; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import java.util.List; @@ -44,5 +47,12 @@ public interface AlertMonitorDao extends JpaRepository, JpaSpecif * @return Monitor the list | 监控列表 */ List findMonitorsByStatus(Byte status); - + + /** + * find monitor bind tags by monitorId + * @param monitorId monitorId + * @return bind tags + */ + @Query("select tag from Tag tag join TagMonitorBind bind on bind.tagId = tag.id where bind.monitorId = :monitorId") + List findMonitorIdBindTags(@Param(value = "monitorId") Long monitorId); } diff --git a/alerter/src/main/java/org/dromara/hertzbeat/alert/reduce/AlarmCommonReduce.java b/alerter/src/main/java/org/dromara/hertzbeat/alert/reduce/AlarmCommonReduce.java index 97ce2d38979..0e085ef3112 100644 --- a/alerter/src/main/java/org/dromara/hertzbeat/alert/reduce/AlarmCommonReduce.java +++ b/alerter/src/main/java/org/dromara/hertzbeat/alert/reduce/AlarmCommonReduce.java @@ -1,10 +1,17 @@ package org.dromara.hertzbeat.alert.reduce; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.dromara.hertzbeat.alert.dao.AlertMonitorDao; +import org.dromara.hertzbeat.common.constants.CommonConstants; import org.dromara.hertzbeat.common.entity.alerter.Alert; +import org.dromara.hertzbeat.common.entity.manager.Tag; import org.dromara.hertzbeat.common.queue.CommonDataQueue; import org.springframework.stereotype.Service; +import java.util.List; +import java.util.Map; + /** * reduce alarm and send alert data * @@ -12,6 +19,7 @@ */ @Service @RequiredArgsConstructor +@Slf4j public class AlarmCommonReduce { private final AlarmSilenceReduce alarmSilenceReduce; @@ -20,8 +28,23 @@ public class AlarmCommonReduce { private final CommonDataQueue dataQueue; + private final AlertMonitorDao alertMonitorDao; + public void reduceAndSendAlarm(Alert alert) { alert.setTimes(1); + Map tags = alert.getTags(); + String monitorIdStr = tags.get(CommonConstants.TAG_MONITOR_ID); + if (monitorIdStr == null) { + log.error("alert tags monitorId can not be null: {}.", alert); + return; + } + long monitorId = Long.parseLong(monitorIdStr); + List tagList = alertMonitorDao.findMonitorIdBindTags(monitorId); + tagList.forEach(tag -> { + if (!tags.containsKey(tag.getName())) { + tags.put(tag.getName(), tag.getValue()); + } + }); // converge -> silence if (alarmConvergeReduce.filterConverge(alert) && alarmSilenceReduce.filterSilence(alert)) { dataQueue.sendAlertsData(alert); diff --git a/manager/src/main/java/org/dromara/hertzbeat/manager/component/alerter/impl/DbAlertStoreHandlerImpl.java b/manager/src/main/java/org/dromara/hertzbeat/manager/component/alerter/impl/DbAlertStoreHandlerImpl.java index 54d4d39211b..8db49e68cf2 100644 --- a/manager/src/main/java/org/dromara/hertzbeat/manager/component/alerter/impl/DbAlertStoreHandlerImpl.java +++ b/manager/src/main/java/org/dromara/hertzbeat/manager/component/alerter/impl/DbAlertStoreHandlerImpl.java @@ -57,14 +57,6 @@ public void store(Alert alert) { log.warn("Dispatch alarm the monitorId: {} not existed, ignored.", monitorId); return; } - if (monitor.getTags() != null) { - monitor.getTags().forEach(item -> { - tags.put(item.getName(), item.getValue()); - }); - } - if (!tags.containsKey(CommonConstants.TAG_MONITOR_NAME)) { - tags.put(CommonConstants.TAG_MONITOR_NAME, monitor.getName()); - } if (monitor.getStatus() == CommonConstants.UN_MANAGE_CODE) { // When monitoring is not managed, ignore and silence its alarm messages // 当监控未管理时 忽略静默其告警信息