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

This commit is contained in:
旺阳 2023-11-18 22:28:29 +08:00 committed by GitHub
parent 36f72056a9
commit a158f1403f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 43 additions and 8 deletions

View File

@ -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

View File

@ -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");
}

View File

@ -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);

View File

@ -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

View File

@ -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);
}

View File

@ -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());

View File

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

View File

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