Skip to content

Commit

Permalink
告警支持钉钉
Browse files Browse the repository at this point in the history
  • Loading branch information
lujiali authored and lujiali committed Dec 20, 2023
1 parent 43409a7 commit 32c5b6a
Show file tree
Hide file tree
Showing 2 changed files with 205 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<!--
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>datavines-notification-plugins</artifactId>
<groupId>io.datavines</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>datavines-notification-plugin-dingtalk</artifactId>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>

<dependency>
<groupId>io.datavines</groupId>
<artifactId>datavines-common</artifactId>
</dependency>
<dependency>
<groupId>io.datavines</groupId>
<artifactId>datavines-notification-api</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* 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 io.datavines.notification.plugin.dingtalk;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.datavines.common.param.form.ParamsOptions;
import io.datavines.common.param.form.PluginParams;
import io.datavines.common.param.form.Validate;
import io.datavines.common.param.form.type.InputParam;
import io.datavines.common.param.form.type.RadioParam;
import io.datavines.common.utils.JSONUtils;
import io.datavines.notification.api.entity.*;
import io.datavines.notification.api.spi.SlasHandlerPlugin;
import io.datavines.notification.plugin.dingtalk.entity.ReceiverConfig;
import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
public class DingTalkSlasHandlerPlugin implements SlasHandlerPlugin {

private final String STRING_YES = "YES";

private final String STRING_NO = "NO";

private final String STRING_TRUE = "TRUE";

private final String STRING_FALSE = "FALSE";

@Override
public SlaNotificationResult notify(SlaNotificationMessage slaNotificationMessage, Map<SlaSenderMessage, Set<SlaConfigMessage>> config) {
Set<SlaSenderMessage> dingTalkSenderSet = config.keySet().stream().filter(x -> "dingtalk".equals(x.getType())).collect(Collectors.toSet());
SlaNotificationResult result = new SlaNotificationResult();
ArrayList<SlaNotificationResultRecord> records = new ArrayList<>();
result.setStatus(true);
String subject = slaNotificationMessage.getSubject();
String message = slaNotificationMessage.getMessage();
for (SlaSenderMessage senderMessage: dingTalkSenderSet) {
DingTalkSender dingTalkSender = new DingTalkSender(senderMessage);
Set<SlaConfigMessage> slaConfigMessageSet = config.get(senderMessage);
HashSet<ReceiverConfig> toReceivers = new HashSet<>();
for (SlaConfigMessage receiver: slaConfigMessageSet) {
String receiverConfigStr = receiver.getConfig();
ReceiverConfig receiverConfig = JSONUtils.parseObject(receiverConfigStr, ReceiverConfig.class);
toReceivers.add(receiverConfig);
}

SlaNotificationResultRecord record = dingTalkSender.sendCardMsg(toReceivers, subject, message);
if (record.getStatus().equals(false)) {
record.setMessage(record.getMessage());
result.setStatus(false);
}
records.add(record);
}
result.setRecords(records);
return result;
}

@Override
public String getConfigSenderJson() {

List<PluginParams> paramsList = new ArrayList<>();

InputParam webHook = InputParam.newBuilder("webHook", "webHook")
.addValidate(Validate.newBuilder().setRequired(true).build())
.build();
InputParam keyWord = InputParam.newBuilder("keyWord", "keyWord")
.addValidate(Validate.newBuilder().setRequired(true).build())
.build();

paramsList.add(webHook);
paramsList.add(keyWord);

ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
String result = null;

try {
result = mapper.writeValueAsString(paramsList);
} catch (JsonProcessingException e) {
log.error("json parse error : {}", e.getMessage(), e);
}

return result;
}

@Override
public String getConfigJson() {

List<PluginParams> paramsList = new ArrayList<>();

InputParam atMobiles = InputParam.newBuilder("atMobiles", "atMobiles")
.addValidate(Validate.newBuilder().setRequired(true).build())
.build();
InputParam atUserIds = InputParam.newBuilder("atUserIds", "atUserIds")
.addValidate(Validate.newBuilder().setRequired(true).build())
.build();
RadioParam isAtAll = RadioParam.newBuilder("isAtAll", "isAtAll")
.addParamsOptions(new ParamsOptions(STRING_YES, STRING_TRUE, false))
.addParamsOptions(new ParamsOptions(STRING_NO, STRING_FALSE, false))
.setValue(STRING_FALSE)
.addValidate(Validate.newBuilder().setRequired(true).build())
.build();

paramsList.add(atMobiles);
paramsList.add(atUserIds);
paramsList.add(isAtAll);

ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
String result = null;

try {
result = mapper.writeValueAsString(paramsList);
} catch (JsonProcessingException e) {
log.error("json parse error : {}", e.getMessage(), e);
}

return result;
}
}

0 comments on commit 32c5b6a

Please sign in to comment.