Browse Source

[Improvement][Alert] Add timeout params for HTTP plugin (#15174)

augit-log
旺阳 6 months ago committed by GitHub
parent
commit
a158f1403f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannelFactory.java
  2. 6
      dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertConstants.java
  3. 20
      dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpSender.java
  4. 2
      dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/test/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannelFactoryTest.java
  5. 6
      dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/test/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannelTest.java
  6. 4
      dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/test/java/org/apache/dolphinscheduler/plugin/alert/http/HttpSenderTest.java
  7. 1
      dolphinscheduler-ui/src/locales/en_US/security.ts
  8. 1
      dolphinscheduler-ui/src/locales/zh_CN/security.ts

11
dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannelFactory.java

@ -23,6 +23,7 @@ import org.apache.dolphinscheduler.alert.api.AlertInputTips;
import org.apache.dolphinscheduler.spi.params.base.PluginParams;
import org.apache.dolphinscheduler.spi.params.base.Validate;
import org.apache.dolphinscheduler.spi.params.input.InputParam;
import org.apache.dolphinscheduler.spi.params.input.number.InputNumberParam;
import java.util.Arrays;
import java.util.List;
@ -79,7 +80,15 @@ public final class HttpAlertChannelFactory implements AlertChannelFactory {
.build())
.build();
return Arrays.asList(url, requestType, headerParams, bodyParams, contentField);
InputNumberParam timeout =
InputNumberParam.newBuilder(HttpAlertConstants.NAME_TIMEOUT, HttpAlertConstants.TIMEOUT)
.setValue(HttpAlertConstants.DEFAULT_TIMEOUT)
.addValidate(Validate.newBuilder()
.setRequired(false)
.build())
.build();
return Arrays.asList(url, requestType, headerParams, bodyParams, contentField, timeout);
}
@Override

6
dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertConstants.java

@ -39,6 +39,12 @@ public final class HttpAlertConstants {
public static final String NAME_REQUEST_TYPE = "requestType";
public static final String TIMEOUT = "$t('timeout')";
public static final String NAME_TIMEOUT = "timeout";
public static final int DEFAULT_TIMEOUT = 120;
private HttpAlertConstants() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}

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

@ -23,6 +23,7 @@ import org.apache.dolphinscheduler.common.utils.JSONUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
@ -32,7 +33,6 @@ 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.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
@ -63,6 +63,7 @@ public final class HttpSender {
private final String bodyParams;
private final String contentField;
private final String requestType;
private final int timeout;
private String url;
private HttpRequestBase httpRequest;
@ -73,6 +74,9 @@ public final class HttpSender {
bodyParams = paramsMap.get(HttpAlertConstants.NAME_BODY_PARAMS);
contentField = paramsMap.get(HttpAlertConstants.NAME_CONTENT_FIELD);
requestType = paramsMap.get(HttpAlertConstants.NAME_REQUEST_TYPE);
timeout = StringUtils.isNotBlank(paramsMap.get(HttpAlertConstants.NAME_TIMEOUT))
? Integer.parseInt(paramsMap.get(HttpAlertConstants.NAME_TIMEOUT))
: HttpAlertConstants.DEFAULT_TIMEOUT;
}
public AlertResult send(String msg) {
@ -107,9 +111,17 @@ public final class HttpSender {
return alertResult;
}
public String getResponseString(HttpRequestBase httpRequest) throws IOException {
CloseableHttpClient httpClient =
HttpClients.custom().setRetryHandler(HttpServiceRetryStrategy.retryStrategy).build();
public String getResponseString(HttpRequestBase httpRequest) throws Exception {
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(timeout * 1000)
.setConnectionRequestTimeout(timeout * 1000)
.setSocketTimeout(timeout * 1000)
.build();
CloseableHttpClient httpClient = HttpClients.custom()
.setDefaultRequestConfig(requestConfig)
.setRetryHandler(HttpServiceRetryStrategy.retryStrategy).build();
CloseableHttpResponse response = httpClient.execute(httpRequest);
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity, DEFAULT_CHARSET);

2
dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/test/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannelFactoryTest.java

@ -39,7 +39,7 @@ public class HttpAlertChannelFactoryTest {
public void getParamsTest() {
List<PluginParams> pluginParamsList = httpAlertChannelFactory.params();
Assertions.assertEquals(5, pluginParamsList.size());
Assertions.assertEquals(6, pluginParamsList.size());
}
@Test

6
dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/test/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannelTest.java

@ -98,11 +98,17 @@ public class HttpAlertChannelTest {
.addValidate(Validate.newBuilder().setRequired(true).build())
.build();
InputParam timeout = InputParam.newBuilder("timeout", "timeout")
.setValue(120)
.addValidate(Validate.newBuilder().setRequired(true).build())
.build();
paramsList.add(urlParam);
paramsList.add(headerParams);
paramsList.add(bodyParams);
paramsList.add(content);
paramsList.add(requestType);
paramsList.add(timeout);
return JSONUtils.toJsonString(paramsList);
}

4
dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/test/java/org/apache/dolphinscheduler/plugin/alert/http/HttpSenderTest.java

@ -23,7 +23,6 @@ import static org.mockito.Mockito.spy;
import org.apache.dolphinscheduler.alert.api.AlertResult;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@ -33,7 +32,7 @@ import org.junit.jupiter.api.Test;
public class HttpSenderTest {
@Test
public void sendTest() throws IOException {
public void sendTest() throws Exception {
Map<String, String> paramsMap = new HashMap<>();
String url = "https://www.dolphinscheduler-not-exists-web.com:12345";
String contentField = "content";
@ -42,6 +41,7 @@ public class HttpSenderTest {
paramsMap.put(HttpAlertConstants.NAME_HEADER_PARAMS, "{\"Content-Type\":\"application/json\"}");
paramsMap.put(HttpAlertConstants.NAME_BODY_PARAMS, "{\"number\":\"123456\"}");
paramsMap.put(HttpAlertConstants.NAME_CONTENT_FIELD, contentField);
paramsMap.put(HttpAlertConstants.NAME_TIMEOUT, String.valueOf(HttpAlertConstants.DEFAULT_TIMEOUT));
HttpSender httpSender = spy(new HttpSender(paramsMap));
doReturn("success").when(httpSender).getResponseString(any());

1
dolphinscheduler-ui/src/locales/en_US/security.ts

@ -249,6 +249,7 @@ export default {
headerParams: 'Headers',
bodyParams: 'Body',
contentField: 'Content Field',
timeout: 'Timeout(s)',
Keyword: 'Keyword',
userParams: 'User Params',
path: 'Script Path',

1
dolphinscheduler-ui/src/locales/zh_CN/security.ts

@ -246,6 +246,7 @@ export default {
headerParams: '请求头',
bodyParams: '请求体',
contentField: '内容字段',
timeout: '超时时间(秒)',
Keyword: '关键词',
userParams: '自定义参数',
path: '脚本路径',

Loading…
Cancel
Save