|
| 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.prometheus; |
| 19 | + |
| 20 | +import org.apache.dolphinscheduler.alert.api.AlertData; |
| 21 | +import org.apache.dolphinscheduler.alert.api.AlertResult; |
| 22 | +import org.apache.dolphinscheduler.alert.api.HttpServiceRetryStrategy; |
| 23 | +import org.apache.dolphinscheduler.common.utils.JSONUtils; |
| 24 | + |
| 25 | +import org.apache.commons.collections4.CollectionUtils; |
| 26 | +import org.apache.http.HttpEntity; |
| 27 | +import org.apache.http.HttpStatus; |
| 28 | +import org.apache.http.client.methods.CloseableHttpResponse; |
| 29 | +import org.apache.http.client.methods.HttpPost; |
| 30 | +import org.apache.http.entity.ContentType; |
| 31 | +import org.apache.http.entity.StringEntity; |
| 32 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 33 | +import org.apache.http.impl.client.HttpClients; |
| 34 | +import org.apache.http.util.EntityUtils; |
| 35 | + |
| 36 | +import java.io.IOException; |
| 37 | +import java.text.SimpleDateFormat; |
| 38 | +import java.util.ArrayList; |
| 39 | +import java.util.Date; |
| 40 | +import java.util.HashMap; |
| 41 | +import java.util.List; |
| 42 | +import java.util.Map; |
| 43 | +import java.util.Objects; |
| 44 | + |
| 45 | +import lombok.extern.slf4j.Slf4j; |
| 46 | + |
| 47 | +@Slf4j |
| 48 | +public class PrometheusAlertSender { |
| 49 | + |
| 50 | + private String url; |
| 51 | + private String generatorURL; |
| 52 | + private String annotations; |
| 53 | + |
| 54 | + public PrometheusAlertSender(Map<String, String> config) { |
| 55 | + url = config.get(PrometheusAlertConstants.NAME_ALERT_MANAGER_URL); |
| 56 | + generatorURL = config.get(PrometheusAlertConstants.NAME_GENERATOR_URL); |
| 57 | + annotations = config.get(PrometheusAlertConstants.NAME_ALERT_MANAGER_ANNOTATIONS); |
| 58 | + } |
| 59 | + |
| 60 | + public AlertResult sendMessage(AlertData alertData) { |
| 61 | + AlertResult alertResult; |
| 62 | + try { |
| 63 | + String resp = sendMsg(alertData); |
| 64 | + return checkSendAlertManageMsgResult(resp); |
| 65 | + } catch (Exception e) { |
| 66 | + String errorMsg = String.format("send prometheus alert manager alert error, exception: %s", e.getMessage()); |
| 67 | + log.error(errorMsg); |
| 68 | + alertResult = new AlertResult(); |
| 69 | + alertResult.setStatus("false"); |
| 70 | + alertResult.setMessage(errorMsg); |
| 71 | + } |
| 72 | + return alertResult; |
| 73 | + } |
| 74 | + |
| 75 | + private String sendMsg(AlertData alertData) throws IOException { |
| 76 | + String v2Path = String.format("%s%s", this.url, PrometheusAlertConstants.ALERT_V2_API_PATH); |
| 77 | + String msg = generateContentJson(alertData); |
| 78 | + HttpPost httpPost = constructHttpPost(v2Path, msg); |
| 79 | + |
| 80 | + try (CloseableHttpClient httpClient = getDefaultClient()) { |
| 81 | + try (CloseableHttpResponse response = httpClient.execute(httpPost)) { |
| 82 | + String resp; |
| 83 | + int statusCode = response.getStatusLine().getStatusCode(); |
| 84 | + if (statusCode == HttpStatus.SC_OK) { |
| 85 | + resp = PrometheusAlertConstants.ALERT_SUCCESS; |
| 86 | + log.info("Prometheus alert manager send alert succeed, title: {} ,content: {}", |
| 87 | + alertData.getTitle(), |
| 88 | + alertData.getContent()); |
| 89 | + return resp; |
| 90 | + } |
| 91 | + |
| 92 | + HttpEntity entity = response.getEntity(); |
| 93 | + resp = EntityUtils.toString(entity, "utf-8"); |
| 94 | + EntityUtils.consume(entity); |
| 95 | + log.error( |
| 96 | + "Prometheus alert manager send alert failed, http status code: {}, title: {} ,content: {}, resp: {}", |
| 97 | + statusCode, |
| 98 | + alertData.getTitle(), |
| 99 | + alertData.getContent(), resp); |
| 100 | + |
| 101 | + return resp; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + public AlertResult checkSendAlertManageMsgResult(String resp) { |
| 107 | + AlertResult alertResult = new AlertResult(); |
| 108 | + alertResult.setStatus("false"); |
| 109 | + |
| 110 | + if (Objects.equals(resp, PrometheusAlertConstants.ALERT_SUCCESS)) { |
| 111 | + alertResult.setStatus("true"); |
| 112 | + alertResult.setMessage("prometheus alert manager send success"); |
| 113 | + return alertResult; |
| 114 | + } |
| 115 | + |
| 116 | + alertResult.setMessage(String.format("prometheus alert manager send fail, resp is %s", resp)); |
| 117 | + log.info("send prometheus alert manager msg error, resp error"); |
| 118 | + return alertResult; |
| 119 | + } |
| 120 | + |
| 121 | + public String generateContentJson(AlertData alertData) { |
| 122 | + List<HashMap> list = JSONUtils.toList(alertData.getContent(), HashMap.class); |
| 123 | + HashMap<String, String> labels = new HashMap<>(); |
| 124 | + if (CollectionUtils.isEmpty(list)) { |
| 125 | + labels.put("content", alertData.getContent()); |
| 126 | + } |
| 127 | + for (Map map : list) { |
| 128 | + for (Map.Entry<String, Object> entry : (Iterable<Map.Entry<String, Object>>) map.entrySet()) { |
| 129 | + String key = entry.getKey(); |
| 130 | + String value = entry.getValue().toString(); |
| 131 | + labels.put(key, value); |
| 132 | + } |
| 133 | + } |
| 134 | + labels.put("title", alertData.getTitle()); |
| 135 | + |
| 136 | + Map<String, Object> alert = new HashMap<>(); |
| 137 | + alert.put("labels", labels); |
| 138 | + |
| 139 | + Map<String, String> annotations = JSONUtils.toMap(this.annotations); |
| 140 | + if (annotations != null) { |
| 141 | + alert.put("annotations", annotations); |
| 142 | + } |
| 143 | + |
| 144 | + String formattedTime = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").format(new Date()); |
| 145 | + alert.put("startsAt", formattedTime); |
| 146 | + alert.put("endsAt", formattedTime); |
| 147 | + |
| 148 | + if (generatorURL != null && generatorURL.length() != 0) { |
| 149 | + alert.put("generatorURL", generatorURL); |
| 150 | + } |
| 151 | + List<Map<String, Object>> body = new ArrayList<>(); |
| 152 | + body.add(alert); |
| 153 | + return JSONUtils.toJsonString(body); |
| 154 | + } |
| 155 | + |
| 156 | + private static CloseableHttpClient getDefaultClient() { |
| 157 | + return HttpClients.custom().setRetryHandler(HttpServiceRetryStrategy.retryStrategy).build(); |
| 158 | + } |
| 159 | + |
| 160 | + private static HttpPost constructHttpPost(String url, String msg) { |
| 161 | + HttpPost post = new HttpPost(url); |
| 162 | + StringEntity entity = new StringEntity(msg, ContentType.APPLICATION_JSON); |
| 163 | + post.setEntity(entity); |
| 164 | + return post; |
| 165 | + } |
| 166 | +} |
0 commit comments