Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alert redirect to monitoring #144

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,10 @@ private Map<String, String> transLabel2Map(String labels) {
Map<String, String> labelMap = new HashMap<>();
try {
Arrays.stream(labels.split(",")).forEach(item -> {
String[] split = item.split("=");
String[] split = item.split("=",2);
if (split.length != 2) {
return;
}
labelMap.put(split[0], split[1]);
});
return labelMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ public class Labels {
private String namespace;
private String pod;
private String restartCounts;

private String detailRedirectUrl;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,25 @@ public static String TimeStampToISO8601UTC(long timeStamp) {
return utcTime;
}

@SneakyThrows
public static long ISO8601UTCTOTimeStamp(String utcTime) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = sdf.parse(utcTime);
return date.getTime();
}

@SneakyThrows
public static String ISO8601UTCTOCST(String utcTime) {
SimpleDateFormat sdfUTC = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
wodiwudi marked this conversation as resolved.
Show resolved Hide resolved
sdfUTC.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = sdfUTC.parse(utcTime);

SimpleDateFormat sdfCST = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdfCST.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
String cstTime = sdfCST.format(date);

return cstTime;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.xiaomi.youpin.prometheus.agent.test;

import com.xiaomi.youpin.prometheus.agent.util.DateUtil;
import org.junit.jupiter.api.Test;

import java.sql.Timestamp;
Expand All @@ -26,4 +27,11 @@ private String ValidateTime(long startTime, long endTime) {
}
return "";
}

@Test
public void testTime() {
String time = "2023-11-16T02:18:33.633Z";
System.out.println(DateUtil.ISO8601UTCTOTimeStamp(time));
System.out.println(DateUtil.ISO8601UTCTOCST(time));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ private void registerDingDingCallBack() {
}

public void sendDingDing(String content, String[] unionIds, String cardBizId) {
log.info("sendDingDing param content: {}, unionIds: {}, cardBizId: {}", content, unionIds, cardBizId);
String token = getAccessToken();
if (token == null) {
log.error("DingDingService sendDingDing token is null");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.xiaomi.youpin.prometheus.agent.service.alarmContact;

import com.xiaomi.youpin.prometheus.agent.result.alertManager.AlertManagerFireResult;
import com.xiaomi.youpin.prometheus.agent.result.alertManager.Alerts;
import com.xiaomi.youpin.prometheus.agent.util.DateUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

public abstract class BaseAlertContact {

Map<String, String> NAME_MAP = new HashMap<String, String>() {
Map<String, String> NAME_MAP = new HashMap<String, String>() {
{
put("application", "应用");

Expand All @@ -22,6 +24,8 @@ public abstract class BaseAlertContact {
put("restartCounts", "重启次数");

put("alert_key", "关键词");

put("startTime", "开始时间");
}
};
String[] ALERT_INVISIBLE_LIST = new String[]{"system", "exceptViewLables", "app_iam_id", "metrics_flag", "group_key", "job", "image"};
Expand Down Expand Up @@ -55,4 +59,23 @@ Map<String, Object> transferNames(Map<String, Object> map) {
}
return map;
}

String GenerateAlarmUrl(String prefix, Alerts alerts) {
String startsAt = alerts.getStartsAt();
//Millisecond timestamp
long alertTime = DateUtil.ISO8601UTCTOTimeStamp(startsAt);
//The alert jump url starts 10 minutes before the alarm time and the end time is 10 minutes after the alarm time
String startTime = String.valueOf(alertTime - (10 * 60 * 1000));
String endTime = String.valueOf(alertTime + (10 * 60 * 1000));
StringBuilder sb = new StringBuilder();
String ip = alerts.getLabels().getIp() == null ? "ip" : alerts.getLabels().getIp();
String serverIp = alerts.getLabels().getServerIp() == null ? "serverIp" : alerts.getLabels().getServerIp();
String pod = alerts.getLabels().getPod() == null ? "pod" : alerts.getLabels().getPod();
String serverEnv = alerts.getLabels().getServerEnv() == null ? "serverEnv" : alerts.getLabels().getServerEnv();
sb.append(prefix).append("&var-Node=").append(ip).append("&ip=").append(serverIp).append("&var-pod=").append(pod)
.append("&var-instance=").append(serverIp).append("&serverEnv=").append(serverEnv).append("&heraEnv=")
.append(serverEnv).append("&startTime=").append(startTime).append("&endTime=").append(endTime).append("&from=")
.append(startTime).append("&to=").append(endTime).append("&orgId=1").append("&refresh=10s");
return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.xiaomi.youpin.prometheus.agent.result.alertManager.GroupLabels;
import com.xiaomi.youpin.prometheus.agent.service.DingDingService;
import com.xiaomi.youpin.prometheus.agent.service.FeishuService;
import com.xiaomi.youpin.prometheus.agent.util.DateUtil;
import com.xiaomi.youpin.prometheus.agent.util.FreeMarkerUtil;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -21,11 +22,17 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
* @author zhangxiaowei6
* @Date 2023/9/13 17:24
*/

/**
* @author zhangxiaowei6
* @Date 2023/11/15 20:01
*/
//ding ding alert
@Slf4j
@Component
Expand All @@ -42,6 +49,8 @@ public class DingAlertContact extends BaseAlertContact {

public static final Gson gson = new Gson();

public static final Random random = new Random();

@Override
public void Reach(AlertManagerFireResult fireResult) {
GroupLabels groupLabels = fireResult.getGroupLabels();
Expand All @@ -67,10 +76,15 @@ public void Reach(AlertManagerFireResult fireResult) {
alertOp = "";
alertValue = "";
}
//Generate alarm jump url
String generateAlarmJumpUrl = GenerateAlarmUrl(alert.getLabels().getDetailRedirectUrl(), alert);
log.info("DingAlertContact.generateAlarmJumpUrl: {}",generateAlarmJumpUrl);
map.put("alert_op", alertOp);
map.put("alert_value", alertValue);
map.put("application", alert.getLabels().getApplication());
map.put("silence_url", silenceUrl);
map.put("detailRedirectUrl",generateAlarmJumpUrl);
map.put("startTime", DateUtil.ISO8601UTCTOCST(alert.getStartsAt()));
CommonLabels commonLabels = fireResult.getCommonLabels();
Class clazz = commonLabels.getClass();
Field[] fields = clazz.getDeclaredFields();
Expand All @@ -95,6 +109,9 @@ public void Reach(AlertManagerFireResult fireResult) {
filterName(finalMap);
finalMap.forEach(
(k, v) -> {
if (k.equals("detailRedirectUrl")) {
return;
}
sb.append("**").append(k).append("**").append(": ").append(v).append("\n");
});

Expand All @@ -110,7 +127,9 @@ public void Reach(AlertManagerFireResult fireResult) {
finalMap.put("silence1d", silencePrefix + "1d");
finalMap.put("silence3d", silencePrefix + "3d");
String freeMarkerRes = FreeMarkerUtil.getContent("/dingding", "dingdingbasicCart.ftl", finalMap);
dingDingService.sendDingDing(freeMarkerRes, principals, alert.getLabels().getAlertname() + "||" + System.currentTimeMillis());
int randomNumber = random.nextInt(1000);
dingDingService.sendDingDing(freeMarkerRes, principals, alert.getLabels().getAlertname() +
"||" + System.currentTimeMillis() + randomNumber);
} catch (Exception e) {
log.error("SendAlert.feishuReach error:{}", e);
}
Expand All @@ -120,7 +139,8 @@ public void Reach(AlertManagerFireResult fireResult) {
}

public void updateDingDingCard(String userId, String content, String expectedSilenceTime, String carBizId,String callbackTitle) {
log.info("DingAlertContact.updateDingDingCard begin userId:{},content:{},expectedSilenceTime:{},carBizId:{}", userId, content, expectedSilenceTime, carBizId);
log.info("DingAlertContact.updateDingDingCard begin userId:{},content:{},expectedSilenceTime:{},carBizId:{}",
userId, content, expectedSilenceTime, carBizId);
Map<String, Object> finalMap = new HashMap<>();
finalMap.put("content", content);
finalMap.put("callbackTitle",callbackTitle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@
"type": "button",
"label": {
"type": "text",
"text": "查看",
"id": "text_1700014087919"
},
"actionType": "openLink",
"url": {
"all": "${detailRedirectUrl}"
},
"status": "normal",
"iconCode": "icon_XDS_Todo2",
"id": "button_1700014087923"
},
{
"type": "button",
"label": {
"type": "text",
"text": "报警静默2h",
"id": "text_1696818602521"
},
Expand Down