Skip to content

Commit

Permalink
bugfix alert tag match missing (#1038)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsun28 authored Jun 26, 2023
1 parent fb6c123 commit cfa8bb6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -44,5 +47,12 @@ public interface AlertMonitorDao extends JpaRepository<Monitor, Long>, JpaSpecif
* @return Monitor the list | 监控列表
*/
List<Monitor> 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<Tag> findMonitorIdBindTags(@Param(value = "monitorId") Long monitorId);
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
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
*
* @author tom
*/
@Service
@RequiredArgsConstructor
@Slf4j
public class AlarmCommonReduce {

private final AlarmSilenceReduce alarmSilenceReduce;
Expand All @@ -20,8 +28,23 @@ public class AlarmCommonReduce {

private final CommonDataQueue dataQueue;

private final AlertMonitorDao alertMonitorDao;

public void reduceAndSendAlarm(Alert alert) {
alert.setTimes(1);
Map<String, String> 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<Tag> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
// 当监控未管理时 忽略静默其告警信息
Expand Down

0 comments on commit cfa8bb6

Please sign in to comment.