You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dolphinscheduler.apache.org by GitBox <gi...@apache.org> on 2021/11/18 06:35:32 UTC

[GitHub] [dolphinscheduler] kezhenxu94 commented on a change in pull request #6897: [Feature][Enterprise WeChat Alert]add a alert plugin of the enterprise wechat's robot

kezhenxu94 commented on a change in pull request #6897:
URL: https://github.com/apache/dolphinscheduler/pull/6897#discussion_r751940713



##########
File path: dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-ewechat/pom.xml
##########
@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.
+  -->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <artifactId>dolphinscheduler-alert-plugins</artifactId>
+        <groupId>org.apache.dolphinscheduler</groupId>
+        <version>2.0.0-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+    <artifactId>dolphinscheduler-alert-ewechat</artifactId>
+    <packaging>jar</packaging>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.httpcomponents</groupId>
+            <artifactId>httpclient</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.google.guava</groupId>
+            <artifactId>guava</artifactId>
+        </dependency>
+    </dependencies>
+
+

Review comment:
       ```suggestion
   ```

##########
File path: dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-ewechat/pom.xml
##########
@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.
+  -->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <artifactId>dolphinscheduler-alert-plugins</artifactId>
+        <groupId>org.apache.dolphinscheduler</groupId>
+        <version>2.0.0-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+    <artifactId>dolphinscheduler-alert-ewechat</artifactId>
+    <packaging>jar</packaging>

Review comment:
       ```suggestion
   ```

##########
File path: dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-ewechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/ewechat/exception/EWeChatAlertException.java
##########
@@ -0,0 +1,29 @@
+/*
+ * 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.ewechat.exception;
+
+public class EWeChatAlertException extends RuntimeException {

Review comment:
       Consider just use `RuntimeException`, providing a separate exception makes no sense in this plugin as nobody will catch this specific exception and do recover

##########
File path: dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-ewechat/src/main/java/org/apache/dolphinscheduler/plugin/alert/ewechat/EWeChatSender.java
##########
@@ -0,0 +1,259 @@
+/*
+ * 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.ewechat;
+
+import org.apache.dolphinscheduler.alert.api.AlertResult;
+import org.apache.dolphinscheduler.plugin.alert.ewechat.exception.EWeChatAlertException;
+import org.apache.dolphinscheduler.spi.utils.JSONUtils;
+
+import org.apache.commons.codec.binary.StringUtils;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpHost;
+import org.apache.http.auth.AuthScope;
+import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.CredentialsProvider;
+import org.apache.http.client.config.RequestConfig;
+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.BasicCredentialsProvider;
+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.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Enterprise WeChat Sender
+ */
+public final class EWeChatSender {
+    private static final Logger logger = LoggerFactory.getLogger(EWeChatSender.class);
+
+    private String url;
+
+    private Boolean enableProxy;
+
+    private String proxy;
+
+    private Integer port;
+
+    private String user;
+
+    private String passwd;
+
+    EWeChatSender(Map<String, String> config) {
+        url = config.get(EWeChatParamsConstants.NAME_EWE_CHAT_WEB_HOOK);
+        enableProxy = Boolean.valueOf(config.get(EWeChatParamsConstants.NAME_EWE_CHAT_PROXY_ENABLE));
+        if (Boolean.TRUE.equals(enableProxy)) {
+            port = Integer.parseInt(config.get(EWeChatParamsConstants.NAME_EWE_CHAT_PORT));
+            proxy = config.get(EWeChatParamsConstants.NAME_EWE_CHAT_PROXY);
+            user = config.get(EWeChatParamsConstants.NAME_EWE_CHAT_USER);
+            passwd = config.get(EWeChatParamsConstants.NAME_EWE_CHAT_PASSWD);
+        }
+    }
+
+    private static HttpPost constructHttpPost(String url, String msg, String charset) {
+        HttpPost post = new HttpPost(url);
+        StringEntity entity = new StringEntity(msg, charset);
+        post.setEntity(entity);
+        post.addHeader("Content-Type", "application/json; charset=utf-8");
+        return post;
+    }
+
+    private static CloseableHttpClient getProxyClient(String proxy, int port, String user, String password) {
+        HttpHost httpProxy = new HttpHost(proxy, port);
+        CredentialsProvider provider = new BasicCredentialsProvider();
+        provider.setCredentials(new AuthScope(httpProxy), new UsernamePasswordCredentials(user, password));
+        return HttpClients.custom().setDefaultCredentialsProvider(provider).build();
+    }
+
+    private static CloseableHttpClient getDefaultClient() {
+        return HttpClients.createDefault();
+    }
+
+    private static RequestConfig getProxyConfig(String proxy, int port) {
+        HttpHost httpProxy = new HttpHost(proxy, port);
+        return RequestConfig.custom().setProxy(httpProxy).build();
+    }
+
+    private static String textToJsonString(String text) {
+        Map<String, Object> items = new HashMap<>();
+        items.put("msgtype", "markdown");
+        Map<String, Object> textContent = new HashMap<>();
+        byte[] byt = StringUtils.getBytesUtf8(text);
+        String txt = StringUtils.newStringUtf8(byt);
+        textContent.put("content", txt);
+        items.put("markdown", textContent);
+        return JSONUtils.toJsonString(items);
+    }
+
+    private static AlertResult checkSendEWeChatSendMsgResult(String result) {
+        AlertResult alertResult = new AlertResult();
+        alertResult.setStatus("false");
+
+        if (null == result) {
+            alertResult.setMessage("send enterprise wechat msg error");
+            logger.info("send enterprise wechat msg error,enterprise we chat server resp is null");
+            return alertResult;
+        }
+        EWeChatSendMsgResponse sendMsgResponse = JSONUtils.parseObject(result, EWeChatSendMsgResponse.class);
+        if (null == sendMsgResponse) {
+            alertResult.setMessage("send enterprise we chat msg fail");
+            logger.info("send enterprise wechat msg error,resp error");
+            return alertResult;
+        }
+        if (sendMsgResponse.errcode == 0) {
+            alertResult.setStatus("true");
+            alertResult.setMessage("send enterprise wechat msg success");
+            return alertResult;
+        }
+        alertResult.setMessage(String.format("alert send enterprise wechat msg error : %s", sendMsgResponse.getErrmsg()));
+        logger.info("alert send enterprise wechat msg error : {}", sendMsgResponse.getErrmsg());
+        return alertResult;
+    }
+
+    public AlertResult sendEWeChatMsg(String title, String content) {
+        AlertResult alertResult;
+        try {
+            String resp = sendMsg(title, content);
+            return checkSendEWeChatSendMsgResult(resp);
+        } catch (Exception e) {
+            logger.info("send enterprise wechat alert msg  exception : {}", e.getMessage());
+            alertResult = new AlertResult();
+            alertResult.setStatus("false");
+            alertResult.setMessage("send enterprise wechat alert fail.");
+        }
+        return alertResult;
+    }
+
+    private String sendMsg(String title, String content) throws IOException {
+        String data = markdownTableByAlert(title, content);
+        String msg = textToJsonString(data);
+        HttpPost httpPost = constructHttpPost(url, msg, EWeChatAlertConstants.CHARSET);
+
+        CloseableHttpClient httpClient;
+        if (Boolean.TRUE.equals(enableProxy)) {
+            httpClient = getProxyClient(proxy, port, user, passwd);
+            RequestConfig rcf = getProxyConfig(proxy, port);
+            httpPost.setConfig(rcf);
+        } else {
+            httpClient = getDefaultClient();
+        }
+
+        try {
+            CloseableHttpResponse response = httpClient.execute(httpPost);

Review comment:
       Consider using `try-with-resources` to simplify the codes.
   
   ```suggestion
           try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
   ```
   
   and remove the `finally` block




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@dolphinscheduler.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org