From 14908a25e8ee5fc6bb4477a0c38dac58b5bc823a Mon Sep 17 00:00:00 2001 From: wangyang Date: Tue, 25 Jan 2022 10:04:21 +0800 Subject: [PATCH] [Feature-7992][Alert] Support PagerDuty Plugin (#8120) Co-authored-by: qingwli --- .../dolphinscheduler-alert-pagerduty/pom.xml | 41 ++++++ .../pagerduty/PagerDutyAlertChannel.java | 38 ++++++ .../PagerDutyAlertChannelFactory.java | 51 +++++++ .../pagerduty/PagerDutyParamsConstants.java | 30 ++++ .../alert/pagerduty/PagerDutySender.java | 129 ++++++++++++++++++ .../PagerDutyAlertChannelFactoryTest.java | 43 ++++++ .../alert/pagerduty/PagerDutySenderTest.java | 41 ++++++ .../dolphinscheduler-alert-plugins/pom.xml | 1 + .../dolphinscheduler-alert-server/pom.xml | 4 + pom.xml | 5 + 10 files changed, 383 insertions(+) create mode 100644 dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-pagerduty/pom.xml create mode 100644 dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-pagerduty/src/main/java/org/apache/dolphinscheduler/plugin/alert/pagerduty/PagerDutyAlertChannel.java create mode 100644 dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-pagerduty/src/main/java/org/apache/dolphinscheduler/plugin/alert/pagerduty/PagerDutyAlertChannelFactory.java create mode 100644 dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-pagerduty/src/main/java/org/apache/dolphinscheduler/plugin/alert/pagerduty/PagerDutyParamsConstants.java create mode 100644 dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-pagerduty/src/main/java/org/apache/dolphinscheduler/plugin/alert/pagerduty/PagerDutySender.java create mode 100644 dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-pagerduty/src/test/java/org/apache/dolphinscheduler/plugin/alert/pagerduty/PagerDutyAlertChannelFactoryTest.java create mode 100644 dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-pagerduty/src/test/java/org/apache/dolphinscheduler/plugin/alert/pagerduty/PagerDutySenderTest.java diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-pagerduty/pom.xml b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-pagerduty/pom.xml new file mode 100644 index 0000000000..7b7593a9f9 --- /dev/null +++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-pagerduty/pom.xml @@ -0,0 +1,41 @@ + + + + + dolphinscheduler-alert-plugins + org.apache.dolphinscheduler + 2.0.0-SNAPSHOT + + 4.0.0 + dolphinscheduler-alert-pagerduty + jar + + + + com.google.guava + guava + + + + org.apache.httpcomponents + httpclient + + + diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-pagerduty/src/main/java/org/apache/dolphinscheduler/plugin/alert/pagerduty/PagerDutyAlertChannel.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-pagerduty/src/main/java/org/apache/dolphinscheduler/plugin/alert/pagerduty/PagerDutyAlertChannel.java new file mode 100644 index 0000000000..6ad4c296c1 --- /dev/null +++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-pagerduty/src/main/java/org/apache/dolphinscheduler/plugin/alert/pagerduty/PagerDutyAlertChannel.java @@ -0,0 +1,38 @@ +/* + * 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.pagerduty; + +import org.apache.dolphinscheduler.alert.api.AlertChannel; +import org.apache.dolphinscheduler.alert.api.AlertData; +import org.apache.dolphinscheduler.alert.api.AlertInfo; +import org.apache.dolphinscheduler.alert.api.AlertResult; + +import java.util.Map; + +public final class PagerDutyAlertChannel implements AlertChannel { + @Override + public AlertResult process(AlertInfo alertInfo) { + AlertData alertData = alertInfo.getAlertData(); + Map alertParams = alertInfo.getAlertParams(); + if (alertParams == null || alertParams.size() == 0) { + return new AlertResult("false", "PagerDuty alert params is empty"); + } + + return new PagerDutySender(alertParams).sendPagerDutyAlter(alertData.getTitle(), alertData.getContent()); + } +} diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-pagerduty/src/main/java/org/apache/dolphinscheduler/plugin/alert/pagerduty/PagerDutyAlertChannelFactory.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-pagerduty/src/main/java/org/apache/dolphinscheduler/plugin/alert/pagerduty/PagerDutyAlertChannelFactory.java new file mode 100644 index 0000000000..24840f730e --- /dev/null +++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-pagerduty/src/main/java/org/apache/dolphinscheduler/plugin/alert/pagerduty/PagerDutyAlertChannelFactory.java @@ -0,0 +1,51 @@ +/* + * 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.pagerduty; + +import com.google.auto.service.AutoService; +import org.apache.dolphinscheduler.alert.api.AlertChannel; +import org.apache.dolphinscheduler.alert.api.AlertChannelFactory; +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 java.util.Collections; +import java.util.List; + +@AutoService(AlertChannelFactory.class) +public final class PagerDutyAlertChannelFactory implements AlertChannelFactory { + @Override + public String name() { + return "PagerDuty"; + } + + @Override + public List params() { + InputParam integrationKey = InputParam.newBuilder(PagerDutyParamsConstants.NAME_PAGER_DUTY_INTEGRATION_KEY_NAME, PagerDutyParamsConstants.PAGER_DUTY_INTEGRATION_KEY) + .addValidate(Validate.newBuilder() + .setRequired(true) + .build()) + .build(); + + return Collections.singletonList(integrationKey); + } + + @Override + public AlertChannel create() { + return new PagerDutyAlertChannel(); + } +} diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-pagerduty/src/main/java/org/apache/dolphinscheduler/plugin/alert/pagerduty/PagerDutyParamsConstants.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-pagerduty/src/main/java/org/apache/dolphinscheduler/plugin/alert/pagerduty/PagerDutyParamsConstants.java new file mode 100644 index 0000000000..a90d302504 --- /dev/null +++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-pagerduty/src/main/java/org/apache/dolphinscheduler/plugin/alert/pagerduty/PagerDutyParamsConstants.java @@ -0,0 +1,30 @@ +/* + * 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.pagerduty; + +public final class PagerDutyParamsConstants { + public static final String NAME_PAGER_DUTY_INTEGRATION_KEY_NAME = "IntegrationKey"; + public static final String PAGER_DUTY_INTEGRATION_KEY = "integrationKey"; + public static final String PAGER_DUTY_EVENT_ACTION_TRIGGER = "trigger"; + public static final String PAGER_DUTY_EVENT_API = "https://events.pagerduty.com/v2/enqueue"; + public static final String PAGER_DUTY_EVENT_SOURCE = "DolphinScheduler"; + + private PagerDutyParamsConstants() { + throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); + } +} diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-pagerduty/src/main/java/org/apache/dolphinscheduler/plugin/alert/pagerduty/PagerDutySender.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-pagerduty/src/main/java/org/apache/dolphinscheduler/plugin/alert/pagerduty/PagerDutySender.java new file mode 100644 index 0000000000..3dd0b840c0 --- /dev/null +++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-pagerduty/src/main/java/org/apache/dolphinscheduler/plugin/alert/pagerduty/PagerDutySender.java @@ -0,0 +1,129 @@ +/* + * 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.pagerduty; + +import com.google.common.base.Preconditions; +import org.apache.dolphinscheduler.alert.api.AlertResult; +import org.apache.dolphinscheduler.spi.utils.JSONUtils; +import org.apache.http.HttpStatus; +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.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.slf4j.Logger; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.*; + +public final class PagerDutySender { + private static final Logger log = org.slf4j.LoggerFactory.getLogger(PagerDutySender.class); + + private final String integrationKey; + + public PagerDutySender(Map config) { + integrationKey = config.get(PagerDutyParamsConstants.NAME_PAGER_DUTY_INTEGRATION_KEY_NAME); + Preconditions.checkArgument(!Objects.isNull(integrationKey), "PagerDuty integration key can not be null"); + } + + public AlertResult sendPagerDutyAlter(String title, String content){ + AlertResult alertResult = new AlertResult(); + alertResult.setStatus("false"); + alertResult.setMessage("send pager duty alert fail."); + + try { + sendPagerDutyAlterV2(alertResult, title, content); + } catch (Exception e) { + log.info("send pager duty alert exception : {}", e.getMessage()); + } + + return alertResult; + } + + private AlertResult sendPagerDutyAlterV2(AlertResult alertResult, String title, String content) throws IOException { + String requestBody = textToJsonStringV2(title, content); + return send(alertResult, PagerDutyParamsConstants.PAGER_DUTY_EVENT_API, requestBody); + } + + private AlertResult send(AlertResult alertResult, String url, String requestBody) throws IOException { + HttpPost httpPost = constructHttpPost(url, requestBody); + CloseableHttpClient httpClient = HttpClients.createDefault(); + + try { + CloseableHttpResponse response = httpClient.execute(httpPost); + + int statusCode = response.getStatusLine().getStatusCode(); + try { + if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_ACCEPTED) { + alertResult.setStatus("true"); + alertResult.setMessage("send pager duty alert success"); + }else { + log.info("send pager duty alert fail, statusCode : {}", statusCode); + } + }finally { + response.close(); + } + } catch (IOException e) { + log.info("send pager duty alert exception : {}", e.getMessage()); + } finally { + httpClient.close(); + } + + return alertResult; + } + + private String textToJsonStringV2(String title, String content) { + Map items = new HashMap<>(); + items.put("routing_key", integrationKey); + items.put("event_action", PagerDutyParamsConstants.PAGER_DUTY_EVENT_ACTION_TRIGGER); + Map payload = new HashMap<>(); + payload.put("summary", title); + payload.put("source", PagerDutyParamsConstants.PAGER_DUTY_EVENT_SOURCE); + payload.put("severity", "critical"); + payload.put("custom_details", formatContent(content)); + items.put("payload", payload); + return JSONUtils.toJsonString(items); + } + + private static HttpPost constructHttpPost(String url, String requestBody) { + HttpPost post = new HttpPost(url); + StringEntity entity = new StringEntity(requestBody, StandardCharsets.UTF_8); + post.setEntity(entity); + post.addHeader("Content-Type", "application/json; charset=utf-8"); + return post; + } + + public static String formatContent(String content) { + List list = JSONUtils.toList(content, Map.class); + if (list.isEmpty()) { + return content; + } + + StringBuilder contents = new StringBuilder(100); + for (Map map : list) { + for (Map.Entry entry : (Iterable>) map.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue().toString(); + contents.append(key + ":" + value); + contents.append("\n"); + } + } + + return contents.toString(); + } +} diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-pagerduty/src/test/java/org/apache/dolphinscheduler/plugin/alert/pagerduty/PagerDutyAlertChannelFactoryTest.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-pagerduty/src/test/java/org/apache/dolphinscheduler/plugin/alert/pagerduty/PagerDutyAlertChannelFactoryTest.java new file mode 100644 index 0000000000..1f3315cb4f --- /dev/null +++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-pagerduty/src/test/java/org/apache/dolphinscheduler/plugin/alert/pagerduty/PagerDutyAlertChannelFactoryTest.java @@ -0,0 +1,43 @@ +/* + * 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.pagerduty; + +import org.apache.dolphinscheduler.alert.api.AlertChannel; +import org.apache.dolphinscheduler.spi.params.base.PluginParams; +import org.apache.dolphinscheduler.spi.utils.JSONUtils; +import java.util.List; +import org.junit.Assert; +import org.junit.Test; + +public class PagerDutyAlertChannelFactoryTest { + + @Test + public void testGetParams() { + PagerDutyAlertChannelFactory pagerDutyAlertChannelFactory = new PagerDutyAlertChannelFactory(); + List params = pagerDutyAlertChannelFactory.params(); + JSONUtils.toJsonString(params); + Assert.assertEquals(1, params.size()); + } + + @Test + public void testCreate() { + PagerDutyAlertChannelFactory pagerDutyAlertChannelFactory = new PagerDutyAlertChannelFactory(); + AlertChannel alertChannel = pagerDutyAlertChannelFactory.create(); + Assert.assertNotNull(alertChannel); + } +} diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-pagerduty/src/test/java/org/apache/dolphinscheduler/plugin/alert/pagerduty/PagerDutySenderTest.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-pagerduty/src/test/java/org/apache/dolphinscheduler/plugin/alert/pagerduty/PagerDutySenderTest.java new file mode 100644 index 0000000000..2f16c1a436 --- /dev/null +++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-pagerduty/src/test/java/org/apache/dolphinscheduler/plugin/alert/pagerduty/PagerDutySenderTest.java @@ -0,0 +1,41 @@ +/* + * 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.pagerduty; + +import org.apache.dolphinscheduler.alert.api.AlertResult; +import java.util.HashMap; +import java.util.Map; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class PagerDutySenderTest { + private static final Map pagerDutyConfig = new HashMap<>(); + + @Before + public void initDingTalkConfig() { + pagerDutyConfig.put(PagerDutyParamsConstants.NAME_PAGER_DUTY_INTEGRATION_KEY_NAME, "test"); + } + + @Test + public void testSend() { + PagerDutySender pagerDutySender = new PagerDutySender(pagerDutyConfig); + AlertResult alertResult = pagerDutySender.sendPagerDutyAlter("pagerduty test title", "pagerduty test content"); + Assert.assertEquals("false", alertResult.getStatus()); + } +} diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/pom.xml b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/pom.xml index d1e4ec0357..e709ede1dd 100644 --- a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/pom.xml +++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/pom.xml @@ -35,6 +35,7 @@ dolphinscheduler-alert-http dolphinscheduler-alert-feishu dolphinscheduler-alert-slack + dolphinscheduler-alert-pagerduty diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-server/pom.xml b/dolphinscheduler-alert/dolphinscheduler-alert-server/pom.xml index bac21d2c18..603df78bbf 100644 --- a/dolphinscheduler-alert/dolphinscheduler-alert-server/pom.xml +++ b/dolphinscheduler-alert/dolphinscheduler-alert-server/pom.xml @@ -66,6 +66,10 @@ org.apache.dolphinscheduler dolphinscheduler-alert-wechat + + org.apache.dolphinscheduler + dolphinscheduler-alert-pagerduty + org.apache.dolphinscheduler diff --git a/pom.xml b/pom.xml index 008f07c58e..c59e0f5647 100644 --- a/pom.xml +++ b/pom.xml @@ -345,6 +345,11 @@ dolphinscheduler-alert-wechat ${project.version} + + org.apache.dolphinscheduler + dolphinscheduler-alert-pagerduty + ${project.version} + org.apache.dolphinscheduler dolphinscheduler-registry-api