提交 c17c3248 authored 作者: Edwin's avatar Edwin

fix: 修复告警服务

上级 bf546a32
...@@ -8,6 +8,7 @@ import com.ebaiyihui.alarm.server.util.HttpKit; ...@@ -8,6 +8,7 @@ import com.ebaiyihui.alarm.server.util.HttpKit;
import com.ebaiyihui.framework.utils.StringUtil; import com.ebaiyihui.framework.utils.StringUtil;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -16,6 +17,7 @@ import javax.crypto.Mac; ...@@ -16,6 +17,7 @@ import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -24,6 +26,7 @@ import java.util.stream.Collectors; ...@@ -24,6 +26,7 @@ import java.util.stream.Collectors;
* @author: yk * @author: yk
* @create: 2023/08/09 09:52 * @create: 2023/08/09 09:52
**/ **/
@Slf4j
@Service @Service
public class AlarmServiceImpl implements AlarmService { public class AlarmServiceImpl implements AlarmService {
...@@ -42,49 +45,107 @@ public class AlarmServiceImpl implements AlarmService { ...@@ -42,49 +45,107 @@ public class AlarmServiceImpl implements AlarmService {
@Override @Override
public String receive(List<AlarmMessage> alarmMessages) { public String receive(List<AlarmMessage> alarmMessages) {
// for (AlarmMessage alarmMessage : alarmMessages) { if (alarmMessages == null || alarmMessages.isEmpty()) {
// String name = alarmMessage.getName();
// String message = alarmMessage.getAlarmMessage();
//
// String scope = alarmMessage.getScope();
// // 过滤一下不发送服务通知逻辑
// if (SERVICE.equals(scope)) {
// if (Objects.nonNull(projProperties.getNotNotifyService()) && projProperties.getNotNotifyService().contains(name)) {
// return "success";
// }
// }
// // 过滤一下不发送服务通知接口逻辑
// if (ENDPOINT_RELATION.equalsIgnoreCase(scope)) {
// // 获取路径地址
// String tempName = name.substring(name.indexOf("/"), name.length());
// String pathUrl = tempName.substring(0, tempName.indexOf(" "));
// if (Objects.nonNull(projProperties.getNotNotifyService()) && projProperties.getNotNotifyUrl().contains(pathUrl)) {
// return "success";
// }
// }
//
// // 获取服务名称
// String serviceNme = name.substring(name.indexOf("byh"), name.length());
// List<ServiceInfo> serviceInfos = projProperties.getServiceInfo();
// for (ServiceInfo serviceInfo : serviceInfos) {
// // 相应的服务通知对应人员
// if (serviceInfo.getServiceNames().contains(serviceNme)) {
// // message += "<at user_id=\\\"ou_6cb2f35f9bf8e7d7e977483801ab13de\\\">杨凯</at>";
// message += "<at user_id=\\\"" + serviceInfo.getServiceNotifyName() + "\\\"></at>";
// alarmMessage.setAlarmMessage(message);
// }
// String sendMessage = "{\"msg_type\":\"text\",\"content\":{\"text\":\"Apache SkyWalking Alarm: \\n %s.\"}}";
// String requestBody = getRequestBody(sendMessage, alarmMessage);
// try {
// HttpKit.jsonPost(webHookUrl, requestBody);
// } catch (Exception e) {
// return "fail";
// }
// }
//
// }
return "success"; return "success";
} }
boolean hasError = false;
for (AlarmMessage alarmMessage : alarmMessages) {
try {
if (shouldSkip(alarmMessage)) {
continue;
}
String notifyMessage = buildNotifyMessage(alarmMessage);
alarmMessage.setAlarmMessage(notifyMessage);
String sendMessage = "{\"msg_type\":\"text\",\"content\":{\"text\":\"Apache SkyWalking Alarm:\\n%s\"}}";
String requestBody = getRequestBody(sendMessage, alarmMessage);
HttpKit.jsonPost(webHookUrl, requestBody);
} catch (Exception e) {
hasError = true;
log.error("Send alarm message failed, alarm={}", alarmMessage, e);
}
}
if (hasError) {
return "fail";
}
return "success";
}
private boolean shouldSkip(AlarmMessage alarmMessage) {
String scope = alarmMessage.getScope();
String name = alarmMessage.getName();
if (StringUtil.isBlank(name)) {
return false;
}
if (SERVICE.equals(scope)) {
return splitToSet(projProperties.getNotNotifyService()).contains(name);
}
if (ENDPOINT_RELATION.equalsIgnoreCase(scope)) {
String pathUrl = extractPathUrl(name);
return !StringUtil.isBlank(pathUrl) && splitToSet(projProperties.getNotNotifyUrl()).contains(pathUrl);
}
return false;
}
private String buildNotifyMessage(AlarmMessage alarmMessage) {
String message = alarmMessage.getAlarmMessage();
if (StringUtil.isBlank(message)) {
return "";
}
String serviceName = extractServiceName(alarmMessage.getName());
if (StringUtil.isBlank(serviceName)) {
return message;
}
List<ServiceInfo> serviceInfos = projProperties.getServiceInfo();
if (serviceInfos == null || serviceInfos.isEmpty()) {
return message;
}
StringBuilder notifyUsers = new StringBuilder();
for (ServiceInfo serviceInfo : serviceInfos) {
Set<String> serviceNames = splitToSet(serviceInfo.getServiceNames());
if (serviceNames.contains(serviceName) && !StringUtil.isBlank(serviceInfo.getServiceNotifyName())) {
notifyUsers.append("<at user_id=\"").append(serviceInfo.getServiceNotifyName()).append("\"></at>");
}
}
if (notifyUsers.length() > 0) {
return message + "\n" + notifyUsers;
}
return message;
}
private String extractServiceName(String name) {
if (StringUtil.isBlank(name)) {
return "";
}
int byhIndex = name.indexOf("byh");
if (byhIndex < 0) {
return "";
}
String service = name.substring(byhIndex).trim();
int blankIndex = service.indexOf(' ');
if (blankIndex > 0) {
return service.substring(0, blankIndex);
}
return service;
}
private String extractPathUrl(String name) {
int slashIndex = name.indexOf("/");
int blankIndex = name.indexOf(" ", slashIndex);
if (slashIndex < 0 || blankIndex < 0 || blankIndex <= slashIndex) {
return "";
}
return name.substring(slashIndex, blankIndex);
}
private Set<String> splitToSet(String value) {
if (StringUtil.isBlank(value)) {
return Collections.emptySet();
}
return Arrays.stream(value.split(","))
.map(String::trim)
.filter(item -> !item.isEmpty())
.collect(Collectors.toSet());
}
/** /**
* deal requestBody,if it has sign set the sign * deal requestBody,if it has sign set the sign
...@@ -131,8 +192,8 @@ public class AlarmServiceImpl implements AlarmService { ...@@ -131,8 +192,8 @@ public class AlarmServiceImpl implements AlarmService {
private String sign(final Long timestamp, String secret) throws NoSuchAlgorithmException, InvalidKeyException { private String sign(final Long timestamp, String secret) throws NoSuchAlgorithmException, InvalidKeyException {
String stringToSign = timestamp + "\n" + secret; String stringToSign = timestamp + "\n" + secret;
Mac mac = Mac.getInstance("HmacSHA256"); Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(stringToSign.getBytes(), "HmacSHA256")); mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
byte[] signData = mac.doFinal(); byte[] signData = mac.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(signData); return Base64.getEncoder().encodeToString(signData);
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论