Skip to content

Commit

Permalink
[DSIP-62][Http Alert Plugin] Refactor http alert plugin (#16484)
Browse files Browse the repository at this point in the history
* add dsip_62

* fix sonar

* fix spotless
  • Loading branch information
SbloodyS authored Aug 19, 2024
1 parent 57c80f2 commit 66548a2
Show file tree
Hide file tree
Showing 18 changed files with 345 additions and 233 deletions.
21 changes: 6 additions & 15 deletions docs/docs/en/guide/alert/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,16 @@ If you need to use `Http script` for alerting, create an alert instance in the a
| **Parameter** | **Description** |
|---------------|-----------------------------------------------------------------------------------------------------|
| URL | The `Http` request URL needs to contain protocol, host, path and parameters if the method is `GET`. |
| Request Type | Select the request type from `POST` or `GET`. |
| Headers | The headers of the `Http` request in JSON format. |
| Body | The request body of the `Http` request in JSON format, when using `POST` method to alert. |
| Content Field | The field name to place the alert information. |
| Request Type | Select the request type from `POST` or `GET` or `PUT`. |
| Headers | The headers of the `Http` request in JSON format. Not including content-type. |
| Body | The request body of the `Http` request in JSON format, when using `POST` or `PUT` method to alert. |
| Content Type | The content-type of header. |

## Send Type

Using `POST` and `GET` method to send `Http` request in the `Request Type`.
> Alarm message supports variables `$msg`, which can be used in `URL`, `Headers`, and `Body`.
### GET HTTP

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

![enterprise-wechat-app-msg-config](../../../../img/alert/http-get-example.png)

### POST HTTP

Send alert information inside `Http` body by `Http` POST method.
The following shows the `POST` configuration example:

![enterprise-wechat-app-msg-config](../../../../img/alert/http-post-example.png)
![http-alert-msg-config](../../../../img/alert/http-alert-example.png)
39 changes: 9 additions & 30 deletions docs/docs/zh/guide/alert/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,19 @@

## 参数配置

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

> 访问的`Http`连接URL,需要包含协议、Host、路径,如果是GET方法可以添加参数
* 请求方式

> 选择该请求为POST或GET方法
* 请求头

> `Http`请求的完整请求头,以JSON为格式
* 请求体

> `Http`请求的完整请求体,以JSON为格式,GET方法不需要写该参数
* 内容字段

> 放置本次告警告警信息的字段名称
## 发送类型

其中`Request Type`分别对应使用`POST`方法和`GET`方法进行`Http`告警。
> 告警消息,支持变量`$msg`,可在`URL`,`请求头`,`请求体`中使用,非必填。
### GET Http告警

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

![enterprise-wechat-app-msg-config](../../../../img/alert/http-get-example.png)

### POST Http告警

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

![enterprise-wechat-app-msg-config](../../../../img/alert/http-post-example.png)
![http-alert-msg-config](../../../../img/alert/http-alert-example.png)
Binary file added docs/img/alert/http-alert-example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed docs/img/alert/http-get-example.png
Binary file not shown.
Binary file removed docs/img/alert/http-post-example.png
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,17 @@
<artifactId>jackson-databind</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
import org.apache.dolphinscheduler.alert.api.AlertChannel;
import org.apache.dolphinscheduler.alert.api.AlertChannelFactory;
import org.apache.dolphinscheduler.alert.api.AlertInputTips;
import org.apache.dolphinscheduler.common.model.OkHttpRequestHeaderContentType;
import org.apache.dolphinscheduler.spi.params.base.DataType;
import org.apache.dolphinscheduler.spi.params.base.ParamsOptions;
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 org.apache.dolphinscheduler.spi.params.radio.RadioParam;

import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -52,35 +55,42 @@ public List<PluginParams> params() {
InputParam headerParams =
InputParam.newBuilder(HttpAlertConstants.NAME_HEADER_PARAMS, HttpAlertConstants.HEADER_PARAMS)
.setPlaceholder(AlertInputTips.HEADER.getMsg())
.addValidate(Validate.newBuilder()
.setRequired(true)
.build())
.build();

InputParam bodyParams =
InputParam.newBuilder(HttpAlertConstants.NAME_BODY_PARAMS, HttpAlertConstants.BODY_PARAMS)
.setPlaceholder(AlertInputTips.JSON_BODY.getMsg())
.addValidate(Validate.newBuilder()
.setRequired(false)
.build())
.build();

InputParam contentField =
InputParam.newBuilder(HttpAlertConstants.NAME_CONTENT_FIELD, HttpAlertConstants.CONTENT_FIELD)
.setPlaceholder(AlertInputTips.FIELD_NAME.getMsg())
RadioParam contentType =
RadioParam.newBuilder(HttpAlertConstants.NAME_CONTENT_TYPE, HttpAlertConstants.CONTENT_TYPE)
.addParamsOptions(new ParamsOptions(OkHttpRequestHeaderContentType.APPLICATION_JSON.getValue(),
OkHttpRequestHeaderContentType.APPLICATION_JSON.getValue(), false))
.addParamsOptions(
new ParamsOptions(OkHttpRequestHeaderContentType.APPLICATION_FORM_URLENCODED.getValue(),
OkHttpRequestHeaderContentType.APPLICATION_FORM_URLENCODED.getValue(), false))
.setValue(OkHttpRequestHeaderContentType.APPLICATION_JSON.getValue())
.addValidate(Validate.newBuilder()
.setRequired(true)
.build())
.build();

InputParam requestType =
InputParam.newBuilder(HttpAlertConstants.NAME_REQUEST_TYPE, HttpAlertConstants.REQUEST_TYPE)
.setPlaceholder(AlertInputTips.HTTP_METHOD.getMsg())
InputParam bodyParams =
InputParam.newBuilder(HttpAlertConstants.NAME_BODY_PARAMS, HttpAlertConstants.BODY_PARAMS)
.setPlaceholder(AlertInputTips.JSON_BODY.getMsg())
.addValidate(Validate.newBuilder()
.setRequired(true)
.setRequired(false)
.build())
.build();

RadioParam requestType = RadioParam
.newBuilder(HttpAlertConstants.NAME_REQUEST_TYPE, HttpAlertConstants.REQUEST_TYPE)
.addParamsOptions(new ParamsOptions(HttpRequestMethod.GET.name(), HttpRequestMethod.GET.name(), false))
.addParamsOptions(
new ParamsOptions(HttpRequestMethod.POST.name(), HttpRequestMethod.POST.name(), false))
.addParamsOptions(new ParamsOptions(HttpRequestMethod.PUT.name(), HttpRequestMethod.PUT.name(), false))
.setValue(HttpRequestMethod.GET.name())
.addValidate(Validate.newBuilder().setRequired(true).build())
.build();

InputNumberParam timeout =
InputNumberParam.newBuilder(HttpAlertConstants.NAME_TIMEOUT, HttpAlertConstants.TIMEOUT)
.setValue(HttpAlertConstants.DEFAULT_TIMEOUT)
Expand All @@ -90,7 +100,7 @@ public List<PluginParams> params() {
.build())
.build();

return Arrays.asList(url, requestType, headerParams, bodyParams, contentField, timeout);
return Arrays.asList(url, requestType, headerParams, bodyParams, contentType, timeout);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

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

import lombok.experimental.UtilityClass;

@UtilityClass
public final class HttpAlertConstants {

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

public static final String NAME_HEADER_PARAMS = "headerParams";

public static final String CONTENT_TYPE = "$t('contentType')";

public static final String NAME_CONTENT_TYPE = "contentType";

public static final String BODY_PARAMS = "$t('bodyParams')";

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

public static final int DEFAULT_TIMEOUT = 120;

private HttpAlertConstants() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}
public static final String MSG_PARAMS = "${msg}";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

public enum HttpRequestMethod {
GET, POST, PUT
}
Loading

0 comments on commit 66548a2

Please sign in to comment.