Skip to content

Commit 66548a2

Browse files
authored
[DSIP-62][Http Alert Plugin] Refactor http alert plugin (#16484)
* add dsip_62 * fix sonar * fix spotless
1 parent 57c80f2 commit 66548a2

File tree

18 files changed

+345
-233
lines changed

18 files changed

+345
-233
lines changed

docs/docs/en/guide/alert/http.md

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,16 @@ If you need to use `Http script` for alerting, create an alert instance in the a
77
| **Parameter** | **Description** |
88
|---------------|-----------------------------------------------------------------------------------------------------|
99
| URL | The `Http` request URL needs to contain protocol, host, path and parameters if the method is `GET`. |
10-
| Request Type | Select the request type from `POST` or `GET`. |
11-
| Headers | The headers of the `Http` request in JSON format. |
12-
| Body | The request body of the `Http` request in JSON format, when using `POST` method to alert. |
13-
| Content Field | The field name to place the alert information. |
10+
| Request Type | Select the request type from `POST` or `GET` or `PUT`. |
11+
| Headers | The headers of the `Http` request in JSON format. Not including content-type. |
12+
| Body | The request body of the `Http` request in JSON format, when using `POST` or `PUT` method to alert. |
13+
| Content Type | The content-type of header. |
1414

15-
## Send Type
16-
17-
Using `POST` and `GET` method to send `Http` request in the `Request Type`.
15+
> Alarm message supports variables `$msg`, which can be used in `URL`, `Headers`, and `Body`.
1816
1917
### GET HTTP
2018

2119
Send alert information by `Http` GET method.
2220
The following shows the `GET` configuration example:
2321

24-
![enterprise-wechat-app-msg-config](../../../../img/alert/http-get-example.png)
25-
26-
### POST HTTP
27-
28-
Send alert information inside `Http` body by `Http` POST method.
29-
The following shows the `POST` configuration example:
30-
31-
![enterprise-wechat-app-msg-config](../../../../img/alert/http-post-example.png)
22+
![http-alert-msg-config](../../../../img/alert/http-alert-example.png)

docs/docs/zh/guide/alert/http.md

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,19 @@
44

55
## 参数配置
66

7-
* URL
7+
| **参数** | **描述** |
8+
|--------------|------------------------------------------------|
9+
| URL | 访问的`Http`连接URL,需要包含协议、Host、路径,如果是GET方法可以添加参数 |
10+
| 请求方式 | 当前支持`GET``POST`以及`PUT`三种请求方式 |
11+
| 请求头(Headers) | `Http`请求的完整请求头,以JSON为格式(注意不包含Content-Type),非必填 |
12+
| 请求体(Body) | Http`请求的完整请求体,以JSON为格式,非必填 |
13+
| Content-Type | 请求体的`Content-Type`,默认为`application/json` |
814

9-
> 访问的`Http`连接URL,需要包含协议、Host、路径,如果是GET方法可以添加参数
10-
11-
* 请求方式
12-
13-
> 选择该请求为POST或GET方法
14-
15-
* 请求头
16-
17-
> `Http`请求的完整请求头,以JSON为格式
18-
19-
* 请求体
20-
21-
> `Http`请求的完整请求体,以JSON为格式,GET方法不需要写该参数
22-
23-
* 内容字段
24-
25-
> 放置本次告警告警信息的字段名称
26-
27-
## 发送类型
28-
29-
其中`Request Type`分别对应使用`POST`方法和`GET`方法进行`Http`告警。
15+
> 告警消息,支持变量`$msg`,可在`URL`,`请求头`,`请求体`中使用,非必填。
3016
3117
### GET Http告警
3218

3319
GET `Http`告警指将告警结果作为参数通过`Http` GET方法进行请求。
3420
下图是GET告警配置的示例:
3521

36-
![enterprise-wechat-app-msg-config](../../../../img/alert/http-get-example.png)
37-
38-
### POST Http告警
39-
40-
POST `Http`告警指将告警结果作为`BODY`参数通过`Http`POST方法进行请求。
41-
下图是POST告警配置的示例:
42-
43-
![enterprise-wechat-app-msg-config](../../../../img/alert/http-post-example.png)
22+
![http-alert-msg-config](../../../../img/alert/http-alert-example.png)

docs/img/alert/http-alert-example.png

131 KB
Loading

docs/img/alert/http-get-example.png

-15.3 KB
Binary file not shown.

docs/img/alert/http-post-example.png

-17.5 KB
Binary file not shown.

dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,17 @@
4141
<artifactId>jackson-databind</artifactId>
4242
<scope>provided</scope>
4343
</dependency>
44+
45+
<dependency>
46+
<groupId>com.squareup.okhttp3</groupId>
47+
<artifactId>mockwebserver</artifactId>
48+
<scope>test</scope>
49+
<exclusions>
50+
<exclusion>
51+
<groupId>junit</groupId>
52+
<artifactId>junit</artifactId>
53+
</exclusion>
54+
</exclusions>
55+
</dependency>
4456
</dependencies>
4557
</project>

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

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@
2020
import org.apache.dolphinscheduler.alert.api.AlertChannel;
2121
import org.apache.dolphinscheduler.alert.api.AlertChannelFactory;
2222
import org.apache.dolphinscheduler.alert.api.AlertInputTips;
23+
import org.apache.dolphinscheduler.common.model.OkHttpRequestHeaderContentType;
2324
import org.apache.dolphinscheduler.spi.params.base.DataType;
25+
import org.apache.dolphinscheduler.spi.params.base.ParamsOptions;
2426
import org.apache.dolphinscheduler.spi.params.base.PluginParams;
2527
import org.apache.dolphinscheduler.spi.params.base.Validate;
2628
import org.apache.dolphinscheduler.spi.params.input.InputParam;
2729
import org.apache.dolphinscheduler.spi.params.input.number.InputNumberParam;
30+
import org.apache.dolphinscheduler.spi.params.radio.RadioParam;
2831

2932
import java.util.Arrays;
3033
import java.util.List;
@@ -52,35 +55,42 @@ public List<PluginParams> params() {
5255
InputParam headerParams =
5356
InputParam.newBuilder(HttpAlertConstants.NAME_HEADER_PARAMS, HttpAlertConstants.HEADER_PARAMS)
5457
.setPlaceholder(AlertInputTips.HEADER.getMsg())
55-
.addValidate(Validate.newBuilder()
56-
.setRequired(true)
57-
.build())
58-
.build();
59-
60-
InputParam bodyParams =
61-
InputParam.newBuilder(HttpAlertConstants.NAME_BODY_PARAMS, HttpAlertConstants.BODY_PARAMS)
62-
.setPlaceholder(AlertInputTips.JSON_BODY.getMsg())
6358
.addValidate(Validate.newBuilder()
6459
.setRequired(false)
6560
.build())
6661
.build();
6762

68-
InputParam contentField =
69-
InputParam.newBuilder(HttpAlertConstants.NAME_CONTENT_FIELD, HttpAlertConstants.CONTENT_FIELD)
70-
.setPlaceholder(AlertInputTips.FIELD_NAME.getMsg())
63+
RadioParam contentType =
64+
RadioParam.newBuilder(HttpAlertConstants.NAME_CONTENT_TYPE, HttpAlertConstants.CONTENT_TYPE)
65+
.addParamsOptions(new ParamsOptions(OkHttpRequestHeaderContentType.APPLICATION_JSON.getValue(),
66+
OkHttpRequestHeaderContentType.APPLICATION_JSON.getValue(), false))
67+
.addParamsOptions(
68+
new ParamsOptions(OkHttpRequestHeaderContentType.APPLICATION_FORM_URLENCODED.getValue(),
69+
OkHttpRequestHeaderContentType.APPLICATION_FORM_URLENCODED.getValue(), false))
70+
.setValue(OkHttpRequestHeaderContentType.APPLICATION_JSON.getValue())
7171
.addValidate(Validate.newBuilder()
7272
.setRequired(true)
7373
.build())
7474
.build();
7575

76-
InputParam requestType =
77-
InputParam.newBuilder(HttpAlertConstants.NAME_REQUEST_TYPE, HttpAlertConstants.REQUEST_TYPE)
78-
.setPlaceholder(AlertInputTips.HTTP_METHOD.getMsg())
76+
InputParam bodyParams =
77+
InputParam.newBuilder(HttpAlertConstants.NAME_BODY_PARAMS, HttpAlertConstants.BODY_PARAMS)
78+
.setPlaceholder(AlertInputTips.JSON_BODY.getMsg())
7979
.addValidate(Validate.newBuilder()
80-
.setRequired(true)
80+
.setRequired(false)
8181
.build())
8282
.build();
8383

84+
RadioParam requestType = RadioParam
85+
.newBuilder(HttpAlertConstants.NAME_REQUEST_TYPE, HttpAlertConstants.REQUEST_TYPE)
86+
.addParamsOptions(new ParamsOptions(HttpRequestMethod.GET.name(), HttpRequestMethod.GET.name(), false))
87+
.addParamsOptions(
88+
new ParamsOptions(HttpRequestMethod.POST.name(), HttpRequestMethod.POST.name(), false))
89+
.addParamsOptions(new ParamsOptions(HttpRequestMethod.PUT.name(), HttpRequestMethod.PUT.name(), false))
90+
.setValue(HttpRequestMethod.GET.name())
91+
.addValidate(Validate.newBuilder().setRequired(true).build())
92+
.build();
93+
8494
InputNumberParam timeout =
8595
InputNumberParam.newBuilder(HttpAlertConstants.NAME_TIMEOUT, HttpAlertConstants.TIMEOUT)
8696
.setValue(HttpAlertConstants.DEFAULT_TIMEOUT)
@@ -90,7 +100,7 @@ public List<PluginParams> params() {
90100
.build())
91101
.build();
92102

93-
return Arrays.asList(url, requestType, headerParams, bodyParams, contentField, timeout);
103+
return Arrays.asList(url, requestType, headerParams, bodyParams, contentType, timeout);
94104
}
95105

96106
@Override

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
package org.apache.dolphinscheduler.plugin.alert.http;
1919

20+
import lombok.experimental.UtilityClass;
21+
22+
@UtilityClass
2023
public final class HttpAlertConstants {
2124

2225
public static final String URL = "$t('url')";
@@ -27,6 +30,10 @@ public final class HttpAlertConstants {
2730

2831
public static final String NAME_HEADER_PARAMS = "headerParams";
2932

33+
public static final String CONTENT_TYPE = "$t('contentType')";
34+
35+
public static final String NAME_CONTENT_TYPE = "contentType";
36+
3037
public static final String BODY_PARAMS = "$t('bodyParams')";
3138

3239
public static final String NAME_BODY_PARAMS = "bodyParams";
@@ -45,7 +52,5 @@ public final class HttpAlertConstants {
4552

4653
public static final int DEFAULT_TIMEOUT = 120;
4754

48-
private HttpAlertConstants() {
49-
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
50-
}
55+
public static final String MSG_PARAMS = "${msg}";
5156
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.dolphinscheduler.plugin.alert.http;
19+
20+
public enum HttpRequestMethod {
21+
GET, POST, PUT
22+
}

0 commit comments

Comments
 (0)