This is an automated email from the ASF dual-hosted git repository.

gongchao pushed a commit to branch integte-extern-alarm
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git

commit 8362235a20c47df6db009b74cf92be3120a144ad
Author: tomsun28 <[email protected]>
AuthorDate: Sat Jan 11 16:39:10 2025 +0800

    [improve] init
    
    Signed-off-by: tomsun28 <[email protected]>
---
 .../alert/controller/AlertReportController.java    | 38 +++++++++++++++++++---
 .../service/impl/DefaultExternAlertService.java    |  2 +-
 .../assets/doc/alert-integration/webhook.en-US.md  |  4 +--
 .../assets/doc/alert-integration/webhook.zh-CN.md  |  4 +--
 .../assets/doc/alert-integration/webhook.zh-TW.md  |  4 +--
 5 files changed, 41 insertions(+), 11 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 58d21199de..17d335b665 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
@@ -23,7 +23,9 @@ import io.swagger.v3.oas.annotations.tags.Tag;
 import java.util.List;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.hertzbeat.alert.service.ExternAlertService;
+import org.apache.hertzbeat.common.constants.CommonConstants;
 import org.apache.hertzbeat.common.entity.dto.Message;
+import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.util.StringUtils;
 import org.springframework.web.bind.annotation.PathVariable;
@@ -49,7 +51,7 @@ public class AlertReportController {
 
     @PostMapping("/{source}")
     @Operation(summary = "Api for receive external alarm information")
-    public ResponseEntity<Message<Void>> 
receiveExternAlert(@PathVariable(value = "source", required = false) String 
source, 
+    public ResponseEntity<Message<Void>> 
receiveExternAlert(@PathVariable(value = "source") String source, 
                                                             @RequestBody 
String content) {
         log.info("Receive extern alert from source: {}, content: {}", source, 
content);
         if (!StringUtils.hasText(source)) {
@@ -57,11 +59,39 @@ public class AlertReportController {
         }
         for (ExternAlertService externAlertService : externAlertServiceList) {
             if (externAlertService.supportSource().equals(source)) {
-                externAlertService.addExternAlert(content);
-                return ResponseEntity.ok(Message.success("Add extern alert 
success"));
+                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.warn("Not support extern alert from source: {}", source);
-        return ResponseEntity.ok(Message.success("Not support the " + source + 
" source alert"));
+        return ResponseEntity.status(HttpStatus.BAD_REQUEST)
+                .body(Message.fail(CommonConstants.FAIL_CODE, "Not support the 
" + source + " source alert"));
+    }
+
+    @PostMapping
+    @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);
+        ExternAlertService externAlertService = externAlertServiceList.stream()
+                .filter(item -> 
"default".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 default extern alert");
+        return ResponseEntity.status(HttpStatus.BAD_REQUEST)
+                .body(Message.success("Not support the default source alert"));
     }
 }
diff --git 
a/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/DefaultExternAlertService.java
 
b/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/DefaultExternAlertService.java
index 1d875b93b0..c178448d9a 100644
--- 
a/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/DefaultExternAlertService.java
+++ 
b/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/DefaultExternAlertService.java
@@ -40,7 +40,7 @@ public class DefaultExternAlertService implements 
ExternAlertService {
         SingleAlert alert = JsonUtil.fromJson(content, SingleAlert.class);
         if (alert == null) {
             log.warn("parse extern alert content failed! content: {}", 
content);
-            return;
+            throw new IllegalArgumentException("parse extern alert content 
failed!");
         }
         alarmCommonReduce.reduceAndSendAlarm(alert);
     }
diff --git a/web-app/src/assets/doc/alert-integration/webhook.en-US.md 
b/web-app/src/assets/doc/alert-integration/webhook.en-US.md
index 460ee25736..f0748d18ba 100644
--- a/web-app/src/assets/doc/alert-integration/webhook.en-US.md
+++ b/web-app/src/assets/doc/alert-integration/webhook.en-US.md
@@ -24,8 +24,8 @@
   "content": "The CPU usage on instance 343483943 is critically high.",
   "status": "firing",
   "triggerTimes": 3,
-  "startAt": "2025-01-10T12:00:00.000Z",
-  "activeAt": "2025-01-10T12:05:00.000Z",
+  "startAt": 1736580031832,
+  "activeAt": 1736580039832,
   "endAt": null
 }
 ```
diff --git a/web-app/src/assets/doc/alert-integration/webhook.zh-CN.md 
b/web-app/src/assets/doc/alert-integration/webhook.zh-CN.md
index ecc9d13abe..53ebbfbc55 100644
--- a/web-app/src/assets/doc/alert-integration/webhook.zh-CN.md
+++ b/web-app/src/assets/doc/alert-integration/webhook.zh-CN.md
@@ -24,8 +24,8 @@
   "content": "The CPU usage on instance 343483943 is critically high.",
   "status": "firing",
   "triggerTimes": 3,
-  "startAt": "2025-01-10T12:00:00.000Z",
-  "activeAt": "2025-01-10T12:05:00.000Z",
+  "startAt": 1736580031832,
+  "activeAt": 1736580039832,
   "endAt": null
 }
 ```
diff --git a/web-app/src/assets/doc/alert-integration/webhook.zh-TW.md 
b/web-app/src/assets/doc/alert-integration/webhook.zh-TW.md
index 12c7e95afd..873dfbfda8 100644
--- a/web-app/src/assets/doc/alert-integration/webhook.zh-TW.md
+++ b/web-app/src/assets/doc/alert-integration/webhook.zh-TW.md
@@ -24,8 +24,8 @@
   "content": "實例 343483943 的 CPU 使用率極高。",
   "status": "firing",
   "triggerTimes": 3,
-  "startAt": "2025-01-10T12:00:00.000Z",
-  "activeAt": "2025-01-10T12:05:00.000Z",
+  "startAt": 1736580031832,
+  "activeAt": 1736580039832,
   "endAt": null
 }
 ```


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to