Browse Source

[Improvement][Alert] Alert plugin enhance fail message (#15024)

* add webex && pagerduty fail log

* update

* fix spotless

* Update dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpSender.java

Co-authored-by: xiangzihao <zihaoxiang@apache.org>

* update

---------

Co-authored-by: xiangzihao <zihaoxiang@apache.org>
旺阳 1 year ago
parent
commit
154e1679f9

+ 3 - 1
dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-email/src/main/java/org/apache/dolphinscheduler/plugin/alert/email/MailSender.java

@@ -405,7 +405,9 @@ public final class MailSender {
      */
     private void handleException(AlertResult alertResult, Exception e) {
         log.error("Send email to {} failed", receivers, e);
-        alertResult.setMessage("Send email to {" + String.join(",", receivers) + "} failed," + e.toString());
+        alertResult.setMessage(
+                String.format("Send email to: %s, failed: %s",
+                        String.join(",", receivers), e.getMessage()));
     }
 
 }

+ 2 - 1
dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpSender.java

@@ -100,7 +100,8 @@ public final class HttpSender {
         } catch (Exception e) {
             log.error("send http alert msg  exception : {}", e.getMessage());
             alertResult.setStatus("false");
-            alertResult.setMessage("send http request  alert fail.");
+            alertResult.setMessage(
+                    String.format("Send http request alert failed: %s", e.getMessage()));
         }
 
         return alertResult;

+ 7 - 0
dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-pagerduty/src/main/java/org/apache/dolphinscheduler/plugin/alert/pagerduty/PagerDutySender.java

@@ -21,12 +21,14 @@ import org.apache.dolphinscheduler.alert.api.AlertResult;
 import org.apache.dolphinscheduler.alert.api.HttpServiceRetryStrategy;
 import org.apache.dolphinscheduler.common.utils.JSONUtils;
 
+import org.apache.http.HttpEntity;
 import org.apache.http.HttpStatus;
 import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.entity.StringEntity;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
 
 import java.io.IOException;
 import java.nio.charset.StandardCharsets;
@@ -77,11 +79,16 @@ public final class PagerDutySender {
             CloseableHttpResponse response = httpClient.execute(httpPost);
 
             int statusCode = response.getStatusLine().getStatusCode();
+            HttpEntity entity = response.getEntity();
+            String responseContent = EntityUtils.toString(entity, StandardCharsets.UTF_8);
             try {
                 if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_ACCEPTED) {
                     alertResult.setStatus("true");
                     alertResult.setMessage("send pager duty alert success");
                 } else {
+                    alertResult.setMessage(
+                            String.format("send pager duty alert error, statusCode: %s, responseContent: %s",
+                                    statusCode, responseContent));
                     log.info("send pager duty alert fail, statusCode : {}", statusCode);
                 }
             } finally {

+ 3 - 48
dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-webexteams/src/main/java/org/apache/dolphinscheduler/plugin/alert/webexteams/WebexMessage.java

@@ -17,6 +17,9 @@
 
 package org.apache.dolphinscheduler.plugin.alert.webexteams;
 
+import lombok.Data;
+
+@Data
 public class WebexMessage {
 
     private String roomId;
@@ -25,52 +28,4 @@ public class WebexMessage {
     private String text;
     private String markdown;
     private String html;
-
-    public String getRoomId() {
-        return roomId;
-    }
-
-    public void setRoomId(String roomId) {
-        this.roomId = roomId;
-    }
-
-    public String getToPersonId() {
-        return toPersonId;
-    }
-
-    public void setToPersonId(String toPersonId) {
-        this.toPersonId = toPersonId;
-    }
-
-    public String getToPersonEmail() {
-        return toPersonEmail;
-    }
-
-    public void setToPersonEmail(String toPersonEmail) {
-        this.toPersonEmail = toPersonEmail;
-    }
-
-    public String getText() {
-        return text;
-    }
-
-    public void setText(String text) {
-        this.text = text;
-    }
-
-    public String getMarkdown() {
-        return markdown;
-    }
-
-    public void setMarkdown(String markdown) {
-        this.markdown = markdown;
-    }
-
-    public String getHtml() {
-        return html;
-    }
-
-    public void setHtml(String html) {
-        this.html = html;
-    }
 }

+ 9 - 1
dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-webexteams/src/main/java/org/apache/dolphinscheduler/plugin/alert/webexteams/WebexTeamsSender.java

@@ -22,12 +22,14 @@ import org.apache.dolphinscheduler.alert.api.AlertResult;
 import org.apache.dolphinscheduler.alert.api.HttpServiceRetryStrategy;
 import org.apache.dolphinscheduler.common.utils.JSONUtils;
 
+import org.apache.http.HttpEntity;
 import org.apache.http.HttpStatus;
 import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.entity.StringEntity;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
 
 import java.io.IOException;
 import java.nio.charset.StandardCharsets;
@@ -82,15 +84,21 @@ public final class WebexTeamsSender {
                 HttpClients.custom().setRetryHandler(HttpServiceRetryStrategy.retryStrategy).build();
 
         try {
-            HttpPost httpPost = constructHttpPost(getMessage(alertData), botAccessToken);
+            WebexMessage message = getMessage(alertData);
+            HttpPost httpPost = constructHttpPost(message, botAccessToken);
             CloseableHttpResponse response = httpClient.execute(httpPost);
 
             int statusCode = response.getStatusLine().getStatusCode();
+            HttpEntity entity = response.getEntity();
+            String responseContent = EntityUtils.toString(entity, StandardCharsets.UTF_8);
             try {
                 if (statusCode == HttpStatus.SC_OK) {
                     alertResult.setStatus("true");
                     alertResult.setMessage("send webex teams alert success");
                 } else {
+                    alertResult.setMessage(String.format(
+                            "send webex teams alert error, message: %s, statusCode: %s, responseContent: %s", message,
+                            statusCode, responseContent));
                     log.info("send webex teams alert fail, statusCode : {}", statusCode);
                 }
             } finally {