Skip to content
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

[alarm] refactor new alarm #2902

Open
wants to merge 51 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
efba1f6
[improve] update backend monitor tags to label
tomsun28 Dec 17, 2024
62e84cf
[improve] update alarm fra
tomsun28 Dec 19, 2024
c8eec91
Merge branch 'master' into new-alarm
tomsun28 Dec 24, 2024
ac5f1b3
[improve] update alarm stru
tomsun28 Dec 24, 2024
6b781c4
[improve] update alarm
tomsun28 Dec 25, 2024
e15418e
[improve] update alarm
tomsun28 Dec 25, 2024
469ec93
[improve] update alarm
tomsun28 Dec 25, 2024
5a790f4
[improve] update alarm
tomsun28 Dec 25, 2024
557fa93
[improve] update alarm
tomsun28 Dec 25, 2024
5bd9d21
[improve] update alarm
tomsun28 Dec 25, 2024
bf3f6d6
[improve] update alarm
tomsun28 Dec 25, 2024
78db1b1
[improve] update alarm
tomsun28 Dec 25, 2024
4741cc9
[improve] update alarm
tomsun28 Dec 25, 2024
ef49b72
[webapp] update alert pojo
tomsun28 Dec 25, 2024
604de18
[webapp] update alert pojo
tomsun28 Dec 25, 2024
a7438fb
[improve] update alarm
tomsun28 Dec 25, 2024
d7a4e92
[improve] update alarm
tomsun28 Dec 25, 2024
f5acbfe
[improve] update alarm
tomsun28 Dec 25, 2024
d97830c
[improve] update alarm
tomsun28 Dec 25, 2024
dc247d0
[improve] update alarm
tomsun28 Dec 25, 2024
cae868d
[improve] update alarm
tomsun28 Dec 25, 2024
bcd8e76
[improve] update alarm
tomsun28 Dec 25, 2024
e3b5de7
[improve] update alarm
tomsun28 Dec 25, 2024
107581a
[improve] update alarm
tomsun28 Dec 25, 2024
d7794f5
[improve] update alarm
tomsun28 Dec 25, 2024
e7122b9
[improve] update alarm
tomsun28 Dec 25, 2024
81934a0
[improve] update alarm
tomsun28 Dec 25, 2024
17798de
[improve] update alarm
tomsun28 Dec 25, 2024
46c5271
Update hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/cal…
tomsun28 Dec 25, 2024
ff4b4fa
Update hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/red…
tomsun28 Dec 25, 2024
fa3d43b
Update hertzbeat-common/src/main/java/org/apache/hertzbeat/common/ent…
tomsun28 Dec 25, 2024
8f321c0
Update hertzbeat-base/pom.xml
tomsun28 Dec 25, 2024
7cbd5ce
Update hertzbeat-alerter/src/test/java/org/apache/hertzbeat/alert/red…
tomsun28 Dec 25, 2024
79196c6
Update hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/cal…
tomsun28 Dec 25, 2024
a8b6204
Update hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/ser…
tomsun28 Dec 25, 2024
da6be30
Update hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/ser…
tomsun28 Dec 25, 2024
be6724e
Update hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/ser…
tomsun28 Dec 25, 2024
1555cf1
Update hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/ser…
tomsun28 Dec 25, 2024
abd9dd1
Update hertzbeat-alerter/src/test/java/org/apache/hertzbeat/alert/red…
tomsun28 Dec 25, 2024
48ba387
Update hertzbeat-base/pom.xml
tomsun28 Dec 25, 2024
0c3e0bb
Update hertzbeat-common/src/main/java/org/apache/hertzbeat/common/ent…
tomsun28 Dec 25, 2024
0753a7e
Update hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/cal…
tomsun28 Dec 25, 2024
fdc667d
Update hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/cal…
tomsun28 Dec 25, 2024
0e1901a
Update hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/red…
tomsun28 Dec 25, 2024
0089821
Merge branch 'master' into new-alarm
tomsun28 Dec 25, 2024
201c6cf
[improve] update alarm
tomsun28 Dec 26, 2024
5e36d75
[improve] update alarm
tomsun28 Dec 26, 2024
2df0abb
[improve] update alarm
tomsun28 Dec 26, 2024
238a2b8
[improve] update alarm
tomsun28 Dec 26, 2024
8333382
Merge branch 'master' into new-alarm
Calvin979 Dec 27, 2024
2a3a61c
Merge branch 'master' into new-alarm
tomsun28 Dec 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[improve] update alarm
Signed-off-by: tomsun28 <[email protected]>
  • Loading branch information
tomsun28 committed Dec 25, 2024
commit 557fa93facfe0fd531052793d111d68fd12a38ee
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public AlerterWorkerPool() {
private void initWorkExecutor() {
ThreadFactory threadFactory = new ThreadFactoryBuilder()
.setUncaughtExceptionHandler((thread, throwable) -> {
log.error("workerExecutor has uncaughtException.");
log.error("Alerter workerExecutor has uncaughtException.");
log.error(throwable.getMessage(), throwable);
})
.setDaemon(true)
Expand All @@ -62,7 +62,7 @@ private void initWorkExecutor() {
private void initNotifyExecutor() {
ThreadFactory threadFactory = new ThreadFactoryBuilder()
.setUncaughtExceptionHandler((thread, throwable) -> {
log.error("notifyExecutor has uncaughtException.");
log.error("Alerter notifyExecutor has uncaughtException.");
log.error(throwable.getMessage(), throwable);
})
.setDaemon(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public List<SingleAlert> calculate(AlertDefine rule) {
if (!rule.isEnabled() || StringUtils.isEmpty(rule.getExpr())) {
return Collections.emptyList();
}

// todo: implement the following logic
try {
// 执行查询
// Execute query
List<Map<String, Object>> queryResults = dataSourceService.query(
rule.getDatasource(),
rule.getExpr()
Expand All @@ -43,13 +43,13 @@ public List<SingleAlert> calculate(AlertDefine rule) {
return Collections.emptyList();
}

// 对查询结果执行表达式计算
// Execute expression calculation on query results
List<SingleAlert> newAlerts = queryResults.stream()
.filter(result -> execAlertExpression(result, rule.getExpr()))
.map(result -> buildAlert(rule, result))
.collect(Collectors.toList());

// 处理恢复通知
// Handle recovery notification
if (newAlerts.isEmpty()) {
return handleAlertRecover(rule);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
@Slf4j
public class PeriodicAlertRuleScheduler {

// todo implement the following logic
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,30 +49,30 @@ public class AlertGroupConvergeController {
private AlertGroupConvergeService alertGroupConvergeService;

@PostMapping
@Operation(summary = "New Alarm Converge", description = "Added an alarm Converge")
@Operation(summary = "New Alarm Group Converge", description = "Added an alarm Group Converge")
public ResponseEntity<Message<Void>> addNewAlertGroupConverge(@Valid @RequestBody AlertGroupConverge alertGroupConverge) {
alertGroupConvergeService.validate(alertGroupConverge, false);
alertGroupConvergeService.addAlertGroupConverge(alertGroupConverge);
return ResponseEntity.ok(Message.success("Add success"));
}

@PutMapping
@Operation(summary = "Modifying an Alarm Converge", description = "Modify an existing alarm Converge")
@Operation(summary = "Modifying an Alarm Group Converge", description = "Modify an existing alarm Group Converge")
public ResponseEntity<Message<Void>> modifyAlertGroupConverge(@Valid @RequestBody AlertGroupConverge alertGroupConverge) {
alertGroupConvergeService.validate(alertGroupConverge, true);
alertGroupConvergeService.modifyAlertGroupConverge(alertGroupConverge);
return ResponseEntity.ok(Message.success("Modify success"));
}

@GetMapping(path = "/{id}")
@Operation(summary = "Querying Alarm Converge",
description = "You can obtain alarm Converge information based on the alarm Converge ID")
@Operation(summary = "Querying Alarm Group Converge",
description = "You can obtain alarm Group Converge information based on the alarm Converge ID")
public ResponseEntity<Message<AlertGroupConverge>> getAlertGroupConverge(
@Parameter(description = "Alarm Converge ID", example = "6565463543") @PathVariable("id") long id) {
AlertGroupConverge alertGroupConverge = alertGroupConvergeService.getAlertGroupConverge(id);

return Objects.isNull(alertGroupConverge)
? ResponseEntity.ok(Message.fail(MONITOR_NOT_EXIST_CODE, "AlertGroupConverge not exist."))
? ResponseEntity.ok(Message.fail(MONITOR_NOT_EXIST_CODE, "Alert Group Converge not exist."))
: ResponseEntity.ok(Message.success(alertGroupConverge));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public class AlertGroupConvergesController {
private AlertGroupConvergeService alertGroupConvergeService;

@GetMapping
@Operation(summary = "Query the alarm converge list",
description = "You can obtain the list of alarm converge by querying filter items")
@Operation(summary = "Query the alarm group converge list",
description = "You can obtain the list of alarm group group converge by querying filter items")
public ResponseEntity<Message<Page<AlertGroupConverge>>> getAlertGroupConverges(
@Parameter(description = "Alarm Converge ID", example = "6565463543") @RequestParam(required = false) List<Long> ids,
@Parameter(description = "Search Name", example = "x") @RequestParam(required = false) String search,
Expand All @@ -61,8 +61,8 @@ public ResponseEntity<Message<Page<AlertGroupConverge>>> getAlertGroupConverges(
}

@DeleteMapping
@Operation(summary = "Delete alarm converge in batches",
description = "Delete alarm converge in batches based on the alarm converge ID list")
@Operation(summary = "Delete alarm group converge in batches",
description = "Delete alarm group converge in batches based on the alarm group converge ID list")
public ResponseEntity<Message<Void>> deleteAlertDefines(
@Parameter(description = "Alarm Converge IDs", example = "6565463543") @RequestParam(required = false) List<Long> ids
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* 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.controller;

import static org.apache.hertzbeat.common.constants.CommonConstants.MONITOR_NOT_EXIST_CODE;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import java.util.Objects;
import org.apache.hertzbeat.alert.service.AlertInhibitService;
import org.apache.hertzbeat.common.entity.alerter.AlertInhibit;
import org.apache.hertzbeat.common.entity.dto.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* Alarm Silence management API
*/
@Tag(name = "Alert Silence API")
@RestController
@RequestMapping(path = "/api/alert/inhibit", produces = {APPLICATION_JSON_VALUE})
public class AlertInhibitController {

@Autowired
private AlertInhibitService alertInhibitService;

@PostMapping
@Operation(summary = "New Alarm Silence", description = "Added an alarm Silence")
public ResponseEntity<Message<Void>> addNewAlertInhibit(@Valid @RequestBody AlertInhibit alertInhibit) {
alertInhibitService.validate(alertInhibit, false);
alertInhibitService.addAlertInhibit(alertInhibit);
return ResponseEntity.ok(Message.success("Add success"));
}

@PutMapping
@Operation(summary = "Modifying an Alarm Silence", description = "Modify an existing alarm Silence")
public ResponseEntity<Message<Void>> modifyAlertInhibit(@Valid @RequestBody AlertInhibit alertInhibit) {
alertInhibitService.validate(alertInhibit, true);
alertInhibitService.modifyAlertInhibit(alertInhibit);
return ResponseEntity.ok(Message.success("Modify success"));
}

@GetMapping(path = "/{id}")
@Operation(summary = "Querying Alarm Silence",
description = "You can obtain alarm Silence information based on the alarm Silence ID")
public ResponseEntity<Message<AlertInhibit>> getAlertInhibit(
@Parameter(description = "Alarm Silence ID", example = "6565463543") @PathVariable("id") long id) {
AlertInhibit alertInhibit = alertInhibitService.getAlertInhibit(id);

return Objects.isNull(alertInhibit)
? ResponseEntity.ok(Message.fail(MONITOR_NOT_EXIST_CODE, "AlertInhibit not exist."))
: ResponseEntity.ok(Message.success(alertInhibit));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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.controller;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.HashSet;
import java.util.List;
import org.apache.hertzbeat.alert.service.AlertInhibitService;
import org.apache.hertzbeat.common.entity.alerter.AlertInhibit;
import org.apache.hertzbeat.common.entity.dto.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
* Silence the batch API for alarms
*/
@Tag(name = "Alert Silence Batch API")
@RestController
@RequestMapping(path = "/api/alert/inhibits", produces = {APPLICATION_JSON_VALUE})
public class AlertInhibitsController {

@Autowired
private AlertInhibitService alertInhibitService;

@GetMapping
@Operation(summary = "Query the alarm inhibit list",
description = "You can obtain the list of alarm inhibit by querying filter items")
public ResponseEntity<Message<Page<AlertInhibit>>> getAlertInhibits(
@Parameter(description = "Alarm Silence ID", example = "6565463543") @RequestParam(required = false) List<Long> ids,
@Parameter(description = "Search Name", example = "x") @RequestParam(required = false) String search,
@Parameter(description = "Sort field, default id", example = "id") @RequestParam(defaultValue = "id") String sort,
@Parameter(description = "Sort mode: asc: ascending, desc: descending", example = "desc") @RequestParam(defaultValue = "desc") String order,
@Parameter(description = "List current page", example = "0") @RequestParam(defaultValue = "0") int pageIndex,
@Parameter(description = "Number of list pages", example = "8") @RequestParam(defaultValue = "8") int pageSize) {
Page<AlertInhibit> alertInhibitPage = alertInhibitService.getAlertInhibits(ids, search, sort, order, pageIndex, pageSize);
return ResponseEntity.ok(Message.success(alertInhibitPage));
}

@DeleteMapping
@Operation(summary = "Delete alarm inhibit in batches",
description = "Delete alarm inhibit in batches based on the alarm inhibit ID list")
public ResponseEntity<Message<Void>> deleteAlertDefines(
@Parameter(description = "Alarm Silence IDs", example = "6565463543") @RequestParam(required = false) List<Long> ids
) {
if (ids != null && !ids.isEmpty()) {
alertInhibitService.deleteAlertInhibits(new HashSet<>(ids));
}
Message<Void> message = Message.success();
return ResponseEntity.ok(message);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.hertzbeat.alert.dao;

import java.util.List;
import java.util.Set;
import org.apache.hertzbeat.common.entity.alerter.AlertGroupConverge;
import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -34,5 +35,11 @@ public interface AlertGroupConvergeDao extends JpaRepository<AlertGroupConverge,
*/
@Modifying
void deleteAlertGroupConvergesByIdIn(Set<Long> ids);

/**
* Query the enable true group alarm converge list
* @return group alarm converge list
*/
List<AlertGroupConverge> findAlertGroupConvergesByEnableIsTrue();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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 java.util.List;
import java.util.Set;
import org.apache.hertzbeat.common.entity.alerter.AlertInhibit;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;

/**
* AlertInhibit Dao
*/
public interface AlertInhibitDao extends JpaRepository<AlertInhibit, Long>, JpaSpecificationExecutor<AlertInhibit> {

/**
* Delete alarm inhibit based on the ID list
*
* @param ids alert inhibit id list
*/
@Modifying
void deleteAlertInhibitsByIdIn(Set<Long> ids);

/**
* Query the enable true alarm inhibit list
* @return alarm inhibit list
*/
List<AlertInhibit> findAlertInhibitsByEnableIsTrue();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.hertzbeat.alert.dao.AlertGroupConvergeDao;
import org.apache.hertzbeat.common.entity.alerter.AlertGroupConverge;
import org.apache.hertzbeat.common.entity.alerter.GroupAlert;
import org.apache.hertzbeat.common.entity.alerter.SingleAlert;
Expand All @@ -23,7 +23,6 @@
*/
@Service
@Slf4j
@RequiredArgsConstructor
public class AlarmGroupReduce {

/**
Expand All @@ -48,20 +47,28 @@ public class AlarmGroupReduce {
* key: rule name
* value: group rule configuration
*/
private final Map<String, AlertGroupConverge> groupDefines = new HashMap<>();
private final Map<String, AlertGroupConverge> groupDefines;

/**
* Alert cache grouped by labels
* key: groupDefineKey:groupKey
* value: GroupAlertCache
*/
private final Map<String, GroupAlertCache> groupCacheMap = new ConcurrentHashMap<>(16);

private final Map<String, GroupAlertCache> groupCacheMap;

public AlarmGroupReduce(AlarmInhibitReduce alarmInhibitReduce, AlertGroupConvergeDao alertGroupConvergeDao) {
this.alarmInhibitReduce = alarmInhibitReduce;
this.groupDefines = new ConcurrentHashMap<>(8);
this.groupCacheMap = new ConcurrentHashMap<>(8);
List<AlertGroupConverge> groupConverges = alertGroupConvergeDao.findAlertGroupConvergesByEnableIsTrue();
refreshGroupDefines(groupConverges);
}

/**
* Configure group define rules
* @param groupDefines map of group rule configurations
*/
public void configureGroupDefines(List<AlertGroupConverge> groupDefines) {
public void refreshGroupDefines(List<AlertGroupConverge> groupDefines) {
this.groupDefines.clear();
groupDefines.forEach(define -> this.groupDefines.put(define.getName(), define));
}
Expand Down
Loading