-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feature] Support Collector Alarm #2693
Open
pwallk
wants to merge
38
commits into
apache:master
Choose a base branch
from
pwallk:feat-collector-alarm
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
38e409e
collector alarm
pwallk 98ef2c7
fix eslint error
pwallk f77314a
Merge branch 'master' into feat-collector-alarm
Calvin979 201ca71
fix test error
pwallk f7a3928
Merge branch 'master' into feat-collector-alarm
Aias00 2cafeea
Merge branch 'master' into feat-collector-alarm
Aias00 d7e6868
Merge branch 'master' into feat-collector-alarm
yuluo-yx ffba083
Merge branch 'master' into feat-collector-alarm
Aias00 84ffd0a
Merge branch 'master' into feat-collector-alarm
Aias00 5e639b7
Merge branch 'master' into feat-collector-alarm
Aias00 154a8d1
Merge branch 'master' into feat-collector-alarm
Aias00 b51ed2b
Merge branch 'master' into feat-collector-alarm
Aias00 f007fb9
Merge branch 'master' into feat-collector-alarm
Aias00 5643900
Merge branch 'master' into feat-collector-alarm
Aias00 d1aaeda
reuse AlarmCommonReduce
pwallk 7a143c2
Merge branch 'feat-collector-alarm' of https://github.com/pwallk/hert…
pwallk 2ecc13d
Merge branch 'master' into feat-collector-alarm
Aias00 e79ccab
Merge branch 'master' into feat-collector-alarm
Aias00 b28a7e4
Merge branch 'master' into feat-collector-alarm
Aias00 1327ce6
Merge branch 'master' into feat-collector-alarm
yuluo-yx c71f602
Merge branch 'master' into feat-collector-alarm
Aias00 61c784b
Merge branch 'master' into feat-collector-alarm
Aias00 46a801f
Merge branch 'master' into feat-collector-alarm
Aias00 4305259
Merge branch 'master' into feat-collector-alarm
Aias00 c2be070
Merge branch 'master' into feat-collector-alarm
Aias00 a9bca1d
Merge branch 'master' into feat-collector-alarm
Aias00 3ec4a79
Merge branch 'master' into feat-collector-alarm
Aias00 e59e1f3
Merge branch 'master' into feat-collector-alarm
Aias00 1caa633
Merge branch 'master' into feat-collector-alarm
Aias00 67cb93c
Merge branch 'master' into feat-collector-alarm
Aias00 86591dc
Merge branch 'master' into feat-collector-alarm
Aias00 75196e7
Merge branch 'master' into feat-collector-alarm
Aias00 41d28bc
Merge branch 'master' into feat-collector-alarm
Aias00 f62c8e1
Merge branch 'master' into feat-collector-alarm
Aias00 4de628c
Merge remote-tracking branch 'refs/remotes/upstream/master' into feat…
pwallk ea546d8
fix conflict
pwallk 2ee7917
Merge branch 'master' into feat-collector-alarm
pwallk 87b7905
remove unused files
pwallk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
199 changes: 199 additions & 0 deletions
199
alerter/src/main/java/org/apache/hertzbeat/alert/calculate/CollectorAlarmHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.hertzbeat.alert.calculate; | ||
|
||
import jakarta.persistence.criteria.Predicate; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.hertzbeat.alert.AlerterWorkerPool; | ||
import org.apache.hertzbeat.alert.dao.AlertCollectorDao; | ||
import org.apache.hertzbeat.alert.service.AlertService; | ||
import org.apache.hertzbeat.common.constants.CommonConstants; | ||
import org.apache.hertzbeat.common.entity.alerter.Alert; | ||
import org.apache.hertzbeat.common.entity.manager.Collector; | ||
import org.apache.hertzbeat.common.support.event.CollectorDeletedEvent; | ||
import org.apache.hertzbeat.common.support.event.SystemConfigChangeEvent; | ||
import org.apache.hertzbeat.common.util.ResourceBundleUtil; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.data.jpa.domain.Specification; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.CollectionUtils; | ||
|
||
import java.util.HashMap; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.ResourceBundle; | ||
import java.util.Objects; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import static org.apache.hertzbeat.common.constants.CommonConstants.ALERT_STATUS_CODE_PENDING; | ||
|
||
/** | ||
* handle collector alarm | ||
*/ | ||
@Component | ||
@Slf4j | ||
public class CollectorAlarmHandler { | ||
|
||
private final Map<String, Alert> offlineAlertMap; | ||
|
||
private final AlertService alertService; | ||
|
||
private final AlertCollectorDao alertCollectorDao; | ||
|
||
private final AlerterWorkerPool workerPool; | ||
|
||
private ResourceBundle bundle; | ||
|
||
public CollectorAlarmHandler(AlertService alertService, AlertCollectorDao alertCollectorDao, | ||
AlerterWorkerPool workerPool) { | ||
this.offlineAlertMap = new ConcurrentHashMap<>(16); | ||
this.alertService = alertService; | ||
this.alertCollectorDao = alertCollectorDao; | ||
this.workerPool = workerPool; | ||
this.bundle = ResourceBundleUtil.getBundle("alerter"); | ||
List<Collector> collectors = this.alertCollectorDao.findCollectorsByStatus(CommonConstants.COLLECTOR_STATUS_OFFLINE); | ||
if (!CollectionUtils.isEmpty(collectors)) { | ||
for (Collector collector : collectors) { | ||
Map<String, String> tags = new HashMap<>(8); | ||
tags.put(CommonConstants.TAG_COLLECTOR_ID, String.valueOf(collector.getId())); | ||
tags.put(CommonConstants.TAG_COLLECTOR_NAME, collector.getName()); | ||
this.offlineAlertMap.put(collector.getName(), | ||
Alert.builder().tags(tags).target(CommonConstants.AVAILABILITY).status(ALERT_STATUS_CODE_PENDING).build()); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* handle collector online | ||
* | ||
* @param identity collector name | ||
*/ | ||
public void online(final String identity) { | ||
Collector collector = alertCollectorDao.findCollectorByName(identity); | ||
if (collector == null) { | ||
return; | ||
} | ||
long currentTimeMill = System.currentTimeMillis(); | ||
Alert preAlert = offlineAlertMap.remove(identity); | ||
if (preAlert != null) { | ||
Map<String, String> tags = preAlert.getTags(); | ||
tags.put(CommonConstants.TAG_COLLECTOR_HOST, collector.getIp()); | ||
String content = this.bundle.getString("alerter.availability.collector.recover"); | ||
Alert resumeAlert = Alert.builder() | ||
.tags(tags) | ||
.target(CommonConstants.AVAILABILITY_COLLECTOR) | ||
.content(content) | ||
.priority(CommonConstants.ALERT_PRIORITY_CODE_WARNING) | ||
.status(CommonConstants.ALERT_STATUS_CODE_RESTORED) | ||
.firstAlarmTime(currentTimeMill) | ||
.lastAlarmTime(preAlert.getLastAlarmTime()) | ||
.build(); | ||
workerPool.executeJob(() -> recoverAlarm(identity, resumeAlert)); | ||
} | ||
} | ||
|
||
private void recoverAlarm(String identity, Alert restoreAlert) { | ||
List<Long> alertIds = queryAvailabilityAlerts(identity, restoreAlert) | ||
.stream() | ||
.filter(alert -> Objects.equals(alert.getTags().get(CommonConstants.TAG_COLLECTOR_NAME), identity)) | ||
.map(Alert::getId) | ||
.toList(); | ||
|
||
if (!alertIds.isEmpty()) { | ||
alertService.editAlertStatus(CommonConstants.ALERT_STATUS_CODE_SOLVED, alertIds); | ||
|
||
// Recovery notifications are generated only after an alarm has occurred | ||
alertService.addAlert(restoreAlert); | ||
} | ||
} | ||
|
||
private List<Alert> queryAvailabilityAlerts(String identity, Alert restoreAlert) { | ||
//create query condition | ||
Specification<Alert> specification = (root, query, criteriaBuilder) -> { | ||
List<Predicate> andList = new ArrayList<>(); | ||
|
||
Predicate predicateTags = criteriaBuilder.like(root.get("tags").as(String.class), "%" + identity + "%"); | ||
andList.add(predicateTags); | ||
|
||
Predicate predicatePriority = criteriaBuilder.equal(root.get("priority"), CommonConstants.ALERT_PRIORITY_CODE_EMERGENCY); | ||
andList.add(predicatePriority); | ||
|
||
Predicate predicateStatus = criteriaBuilder.equal(root.get("status"), ALERT_STATUS_CODE_PENDING); | ||
andList.add(predicateStatus); | ||
|
||
Predicate predicateAlertTime = criteriaBuilder.lessThanOrEqualTo(root.get("lastAlarmTime"), restoreAlert.getLastAlarmTime()); | ||
andList.add(predicateAlertTime); | ||
|
||
Predicate[] predicates = new Predicate[andList.size()]; | ||
return criteriaBuilder.and(andList.toArray(predicates)); | ||
}; | ||
|
||
//query results | ||
return alertService.getAlerts(specification); | ||
} | ||
|
||
/** | ||
* handle collector offline | ||
* | ||
* @param identity collector name | ||
*/ | ||
public void offline(final String identity) { | ||
Collector collector = alertCollectorDao.findCollectorByName(identity); | ||
if (collector == null) { | ||
return; | ||
} | ||
long currentTimeMill = System.currentTimeMillis(); | ||
|
||
Alert preAlert = offlineAlertMap.get(identity); | ||
if (preAlert == null) { | ||
Map<String, String> tags = new HashMap<>(); | ||
tags.put(CommonConstants.TAG_COLLECTOR_ID, String.valueOf(collector.getId())); | ||
tags.put(CommonConstants.TAG_COLLECTOR_NAME, collector.getName()); | ||
tags.put(CommonConstants.TAG_COLLECTOR_HOST, collector.getIp()); | ||
tags.put(CommonConstants.TAG_CODE, "OFFLINE"); | ||
|
||
String content = this.bundle.getString("alerter.availability.collector.offline"); | ||
Alert alert = Alert.builder() | ||
.tags(tags) | ||
.priority(CommonConstants.ALERT_PRIORITY_CODE_EMERGENCY) | ||
.status(CommonConstants.ALERT_STATUS_CODE_PENDING) | ||
.target(CommonConstants.AVAILABILITY_COLLECTOR) | ||
.content(content) | ||
.firstAlarmTime(currentTimeMill) | ||
.lastAlarmTime(currentTimeMill) | ||
.times(1) | ||
.build(); | ||
this.offlineAlertMap.put(identity, alert); | ||
this.alertService.addAlert(alert); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same +1 |
||
} | ||
} | ||
|
||
|
||
@EventListener(SystemConfigChangeEvent.class) | ||
public void onSystemConfigChangeEvent(SystemConfigChangeEvent event) { | ||
log.info("calculate alarm receive system config change event: {}.", event.getSource()); | ||
this.bundle = ResourceBundleUtil.getBundle("alerter"); | ||
} | ||
|
||
@EventListener(CollectorDeletedEvent.class) | ||
public void onCollectorDeletedEvent(CollectorDeletedEvent event) { | ||
log.info("collector alarm handler receive collector {} has been deleted.", event.getIdentity()); | ||
offlineAlertMap.remove(event.getIdentity()); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
alerter/src/main/java/org/apache/hertzbeat/alert/dao/AlertCollectorDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.hertzbeat.alert.dao; | ||
|
||
import org.apache.hertzbeat.common.entity.manager.Collector; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Alert Collector Dao | ||
*/ | ||
public interface AlertCollectorDao extends JpaRepository<Collector, Long>, JpaSpecificationExecutor<Collector> { | ||
|
||
/** | ||
* Query the collector in the specified state | ||
* @param status status value | ||
* @return collector list | ||
*/ | ||
List<Collector> findCollectorsByStatus(Byte status); | ||
|
||
/** | ||
* Query collector by name | ||
* @param name collector name | ||
* @return collector | ||
*/ | ||
Collector findCollectorByName(String name); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
common/src/main/java/org/apache/hertzbeat/common/support/event/CollectorDeletedEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.hertzbeat.common.support.event; | ||
|
||
import org.springframework.context.ApplicationEvent; | ||
|
||
/** | ||
* the event for collector delete | ||
*/ | ||
public class CollectorDeletedEvent extends ApplicationEvent { | ||
|
||
private final String identity; | ||
|
||
public CollectorDeletedEvent(Object source, String identity) { | ||
super(source); | ||
this.identity = identity; | ||
} | ||
|
||
public String getIdentity() { | ||
return identity; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 hi, how about use
alarmCommonReduce.reduceAndSendAlarm(restoreAlert)
here? We send it for subsequent alarm convergence, alarm silencing, and alarm storage processing. But it needs to be tested, because before it was monitoring related alarms, I don’t know whether it is compatible with collector alarm.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, I will try to reuse AlarmCommonReduce