xjlgod commented on code in PR #15079:
URL: 
https://github.com/apache/dolphinscheduler/pull/15079#discussion_r1380184601


##########
dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-api/src/main/java/org/apache/dolphinscheduler/alert/api/AlertInputTips.java:
##########
@@ -50,7 +50,8 @@ public enum AlertInputTips {
     SECRET("please input secret", "请输入secret"),
     WECHAT_MENTION_USERS("use `|` to separate userIds and `@all` to everyone", 
"使用`|`来分割userId或使用`@all`来提到所有人"),
     WECHAT_AGENT_ID("please input agent id or chat id", "请输入agent id或chat id"),
-    ;
+    ANNOTATION("please input annotation in json form", "请输入json格式的annotation"),
+    GENERATOR_URL("please input GeneratorURL", "请输入生成地址");

Review Comment:
   Ok.



##########
dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-prometheus/src/main/java/org/apache/dolphinscheduler/plugin/alert/prometheus/AlertManagerSender.java:
##########
@@ -0,0 +1,169 @@
+/*
+ * 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.prometheus;
+
+import org.apache.dolphinscheduler.alert.api.AlertData;
+import org.apache.dolphinscheduler.alert.api.AlertResult;
+import org.apache.dolphinscheduler.alert.api.HttpServiceRetryStrategy;
+import org.apache.dolphinscheduler.common.utils.JSONUtils;
+
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpStatus;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.ContentType;
+import org.apache.http.entity.StringEntity;
+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.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+import lombok.extern.slf4j.Slf4j;
+
+@Slf4j
+public class AlertManagerSender {
+
+    private String url;
+    private String generatorURL;
+    private String annotations;
+
+    public AlertManagerSender(Map<String, String> config) {
+        url = config.get(AlertManagerConstants.NAME_ALERT_MANAGER_URL);
+        generatorURL = config.get(AlertManagerConstants.NAME_GENERATOR_URL);
+        annotations = 
config.get(AlertManagerConstants.NAME_ALERT_MANAGER_ANNOTATIONS);
+    }
+
+    public AlertResult sendMessage(AlertData alertData) {
+        AlertResult alertResult;
+        try {
+            String resp = sendMsg(alertData);
+            return checkSendAlertManageMsgResult(resp);
+        } catch (Exception e) {
+            log.info("send prometheus alert manager alert msg  exception: {}", 
e.getMessage());
+            alertResult = new AlertResult();
+            alertResult.setStatus("false");
+            alertResult.setMessage("send prometheus alert manager alert 
fail.");
+        }
+        return alertResult;
+    }
+
+    private String sendMsg(AlertData alertData) throws IOException {
+        String v2Path = String.format("%s%s", this.url, 
AlertManagerConstants.ALERT_V2_API_PATH);
+        String msg = generateContentJson(alertData);
+        HttpPost httpPost = constructHttpPost(v2Path, msg);
+        CloseableHttpClient httpClient = getDefaultClient();
+
+        try {
+            CloseableHttpResponse response = httpClient.execute(httpPost);
+            String resp;
+            int statusCode = response.getStatusLine().getStatusCode();
+            if (statusCode == HttpStatus.SC_OK) {
+                resp = AlertManagerConstants.ALERT_SUCCESS;
+                log.info("Prometheus alert manager send alert succeed, title: 
{} ,content: {}", alertData.getTitle(),
+                        alertData.getContent());
+                return resp;
+            }
+
+            log.error("send prometheus alert manager message error, return 
http status code: {} ", statusCode);
+            try {
+                HttpEntity entity = response.getEntity();
+                resp = EntityUtils.toString(entity, "utf-8");
+                EntityUtils.consume(entity);
+            } finally {
+                response.close();
+            }
+            log.info("Prometheus alert manager send alert failed, title: {} 
,content: {}, resp: {}",
+                    alertData.getTitle(),
+                    alertData.getContent(), resp);
+
+            return resp;
+        } finally {
+            httpClient.close();
+        }
+    }
+
+    public AlertResult checkSendAlertManageMsgResult(String resp) {
+        AlertResult alertResult = new AlertResult();
+        alertResult.setStatus("false");
+
+        if (Objects.equals(resp, AlertManagerConstants.ALERT_SUCCESS)) {
+            alertResult.setStatus("true");
+            alertResult.setMessage("prometheus alert manager send success");
+            return alertResult;
+        }
+
+        alertResult.setMessage("prometheus alert manager send fail");

Review Comment:
   Ok.



##########
dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-prometheus/src/main/java/org/apache/dolphinscheduler/plugin/alert/prometheus/AlertManagerSender.java:
##########
@@ -0,0 +1,169 @@
+/*
+ * 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.prometheus;
+
+import org.apache.dolphinscheduler.alert.api.AlertData;
+import org.apache.dolphinscheduler.alert.api.AlertResult;
+import org.apache.dolphinscheduler.alert.api.HttpServiceRetryStrategy;
+import org.apache.dolphinscheduler.common.utils.JSONUtils;
+
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpStatus;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.ContentType;
+import org.apache.http.entity.StringEntity;
+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.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+import lombok.extern.slf4j.Slf4j;
+
+@Slf4j
+public class AlertManagerSender {
+
+    private String url;
+    private String generatorURL;
+    private String annotations;
+
+    public AlertManagerSender(Map<String, String> config) {
+        url = config.get(AlertManagerConstants.NAME_ALERT_MANAGER_URL);
+        generatorURL = config.get(AlertManagerConstants.NAME_GENERATOR_URL);
+        annotations = 
config.get(AlertManagerConstants.NAME_ALERT_MANAGER_ANNOTATIONS);
+    }
+
+    public AlertResult sendMessage(AlertData alertData) {
+        AlertResult alertResult;
+        try {
+            String resp = sendMsg(alertData);
+            return checkSendAlertManageMsgResult(resp);
+        } catch (Exception e) {
+            log.info("send prometheus alert manager alert msg  exception: {}", 
e.getMessage());

Review Comment:
   Ok.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to