Skip to content

Commit

Permalink
[Improvement][Alert] Alert plugin enhance fail message (apache#15024)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* update

---------

Co-authored-by: xiangzihao <[email protected]>
  • Loading branch information
qingwli and SbloodyS authored Oct 16, 2023
1 parent 33084d2 commit 154e167
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,9 @@ public void deleteFile(File file) {
*/
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()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ public AlertResult send(String msg) {
} 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
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;
Expand Down Expand Up @@ -77,11 +79,16 @@ private AlertResult send(AlertResult alertResult, String url, String requestBody
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

package org.apache.dolphinscheduler.plugin.alert.webexteams;

import lombok.Data;

@Data
public class WebexMessage {

private String roomId;
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
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;
Expand Down Expand Up @@ -82,15 +84,21 @@ private void send(AlertResult alertResult, AlertData alertData) throws IOExcepti
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 {
Expand Down

0 comments on commit 154e167

Please sign in to comment.