This is an automated email from the ASF dual-hosted git repository. gongchao pushed a commit to branch fix-third-alarm in repository https://gitbox.apache.org/repos/asf/hertzbeat.git
commit 43d5e58ce6f535560773cef3b3f8dd764ea0bb21 Author: tomsun28 <tomsu...@outlook.com> AuthorDate: Sun Jan 12 17:46:58 2025 +0800 [bugfix] fix prometheus extern alert integrate error Signed-off-by: tomsun28 <tomsu...@outlook.com> --- .../alert/controller/AlertReportController.java | 28 ++++++++-- .../alert/notice/impl/DbAlertStoreHandlerImpl.java | 4 +- .../impl/AlertManagerExternAlertService.java | 1 + .../service/impl/PrometheusExternAlertService.java | 63 ++++++++++++---------- 4 files changed, 62 insertions(+), 34 deletions(-) diff --git a/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/controller/AlertReportController.java b/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/controller/AlertReportController.java index 17d335b665..cf81040abb 100644 --- a/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/controller/AlertReportController.java +++ b/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/controller/AlertReportController.java @@ -17,7 +17,6 @@ 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.tags.Tag; import java.util.List; @@ -31,7 +30,6 @@ import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** @@ -39,7 +37,6 @@ import org.springframework.web.bind.annotation.RestController; */ @Tag(name = "Extern Alarm Manage API") @RestController -@RequestMapping(path = "/api/alerts/report", produces = {APPLICATION_JSON_VALUE}) @Slf4j public class AlertReportController { @@ -49,7 +46,7 @@ public class AlertReportController { this.externAlertServiceList = externAlertServiceList; } - @PostMapping("/{source}") + @PostMapping("/api/alerts/report/{source}") @Operation(summary = "Api for receive external alarm information") public ResponseEntity<Message<Void>> receiveExternAlert(@PathVariable(value = "source") String source, @RequestBody String content) { @@ -74,7 +71,7 @@ public class AlertReportController { .body(Message.fail(CommonConstants.FAIL_CODE, "Not support the " + source + " source alert")); } - @PostMapping + @PostMapping("/api/alerts/report") @Operation(summary = "Api for receive default external alarm information") public ResponseEntity<Message<Void>> receiveDefaultExternAlert(@RequestBody String content) { log.info("Receive default extern alert content: {}", content); @@ -94,4 +91,25 @@ public class AlertReportController { return ResponseEntity.status(HttpStatus.BAD_REQUEST) .body(Message.success("Not support the default source alert")); } + + @PostMapping("/api/v2/alerts") + @Operation(summary = "Api for receive external alarm information") + public ResponseEntity<Message<Void>> receivePrometheusServerAlert(@RequestBody String content) { + log.info("Receive prometheus server alert, content: {}", content); + ExternAlertService externAlertService = externAlertServiceList.stream() + .filter(item -> "prometheus".equals(item.supportSource())).findFirst().orElse(null); + if (externAlertService != null) { + try { + externAlertService.addExternAlert(content); + return ResponseEntity.ok(Message.success("Add extern alert success")); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST) + .body(Message.fail(CommonConstants.FAIL_CODE, + "Add extern alert failed: " + e.getMessage())); + } + } + log.error("Not support prometheus extern alert"); + return ResponseEntity.status(HttpStatus.BAD_REQUEST) + .body(Message.success("Not support the prometheus source alert")); + } } diff --git a/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/notice/impl/DbAlertStoreHandlerImpl.java b/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/notice/impl/DbAlertStoreHandlerImpl.java index ff65988eb6..b6b5ecca05 100644 --- a/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/notice/impl/DbAlertStoreHandlerImpl.java +++ b/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/notice/impl/DbAlertStoreHandlerImpl.java @@ -20,6 +20,7 @@ package org.apache.hertzbeat.alert.notice.impl; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; @@ -66,7 +67,8 @@ final class DbAlertStoreHandlerImpl implements AlertStoreHandler { if (CommonConstants.ALERT_STATUS_FIRING.equals(singleAlert.getStatus())) { if (!CommonConstants.ALERT_STATUS_RESOLVED.equals(existAlert.getStatus())) { singleAlert.setStartAt(existAlert.getStartAt()); - singleAlert.setTriggerTimes(existAlert.getTriggerTimes() + singleAlert.getTriggerTimes()); + int triggerTimes = Optional.ofNullable(existAlert.getTriggerTimes()).orElse(1) + Optional.ofNullable(singleAlert.getTriggerTimes()).orElse(1); + singleAlert.setTriggerTimes(triggerTimes); } } else if (CommonConstants.ALERT_STATUS_RESOLVED.equals(singleAlert.getStatus())) { // Transition to resolved state diff --git a/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertManagerExternAlertService.java b/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertManagerExternAlertService.java index ae8e5dc83a..ee88c6c96d 100644 --- a/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertManagerExternAlertService.java +++ b/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertManagerExternAlertService.java @@ -79,6 +79,7 @@ public class AlertManagerExternAlertService implements ExternAlertService { .endAt(prometheusAlert.getEndsAt() != null ? prometheusAlert.getEndsAt().getEpochSecond() : null) .labels(prometheusAlert.getLabels()) .annotations(prometheusAlert.getAnnotations()) + .triggerTimes(1) .build(); alarmCommonReduce.reduceAndSendAlarm(singleAlert); diff --git a/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/PrometheusExternAlertService.java b/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/PrometheusExternAlertService.java index f8ba25f966..f0e796dc6a 100644 --- a/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/PrometheusExternAlertService.java +++ b/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/PrometheusExternAlertService.java @@ -17,12 +17,15 @@ package org.apache.hertzbeat.alert.service.impl; +import com.fasterxml.jackson.core.type.TypeReference; import java.util.HashMap; +import java.util.List; import java.util.Map; import lombok.extern.slf4j.Slf4j; import org.apache.hertzbeat.alert.dto.PrometheusExternAlert; import org.apache.hertzbeat.alert.reduce.AlarmCommonReduce; import org.apache.hertzbeat.alert.service.ExternAlertService; +import org.apache.hertzbeat.common.constants.CommonConstants; import org.apache.hertzbeat.common.entity.alerter.SingleAlert; import org.apache.hertzbeat.common.util.JsonUtil; import org.springframework.beans.factory.annotation.Autowired; @@ -42,38 +45,42 @@ public class PrometheusExternAlertService implements ExternAlertService { @Override public void addExternAlert(String content) { - - PrometheusExternAlert alert = JsonUtil.fromJson(content, PrometheusExternAlert.class); - if (alert == null) { + + TypeReference<List<PrometheusExternAlert>> typeReference = new TypeReference<>() {}; + List<PrometheusExternAlert> alerts = JsonUtil.fromJson(content, typeReference); + if (alerts == null || alerts.isEmpty()) { log.warn("parse prometheus extern alert content failed! content: {}", content); return; } - Map<String, String> annotations = alert.getAnnotations(); - if (annotations == null) { - annotations = new HashMap<>(8); - } - if (StringUtils.hasText(alert.getGeneratorURL())) { - annotations.put("generatorURL", alert.getGeneratorURL()); - } - String description = annotations.get("description"); - if (description == null) { - description = annotations.get("summary"); - } - if (description == null) { - description = annotations.values().stream().findFirst().orElse(""); + for (PrometheusExternAlert alert : alerts) { + Map<String, String> annotations = alert.getAnnotations(); + if (annotations == null) { + annotations = new HashMap<>(8); + } + if (StringUtils.hasText(alert.getGeneratorURL())) { + annotations.put("generatorURL", alert.getGeneratorURL()); + } + String description = annotations.get("description"); + if (description == null) { + description = annotations.get("summary"); + } + if (description == null) { + description = annotations.values().stream().findFirst().orElse(""); + } + String status = alert.getStatus() == null ? CommonConstants.ALERT_STATUS_FIRING : alert.getStatus(); + SingleAlert singleAlert = SingleAlert.builder() + .content(description) + .status(status) + .activeAt(alert.getActiveAt() != null ? alert.getActiveAt().getEpochSecond() : null) + .startAt(alert.getStartsAt() != null ? alert.getStartsAt().getEpochSecond() : null) + .endAt(alert.getEndsAt() != null ? alert.getEndsAt().getEpochSecond() : null) + .labels(alert.getLabels()) + .annotations(alert.getAnnotations()) + .triggerTimes(1) + .build(); + + alarmCommonReduce.reduceAndSendAlarm(singleAlert); } - - SingleAlert singleAlert = SingleAlert.builder() - .content(description) - .status(alert.getStatus()) - .activeAt(alert.getActiveAt() != null ? alert.getActiveAt().getEpochSecond() : null) - .startAt(alert.getStartsAt() != null ? alert.getStartsAt().getEpochSecond() : null) - .endAt(alert.getEndsAt() != null ? alert.getEndsAt().getEpochSecond() : null) - .labels(alert.getLabels()) - .annotations(alert.getAnnotations()) - .build(); - - alarmCommonReduce.reduceAndSendAlarm(singleAlert); } @Override --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@hertzbeat.apache.org For additional commands, e-mail: notifications-h...@hertzbeat.apache.org