in ozhera-monitor/ozhera-monitor-service/src/main/java/org/apache/ozhera/monitor/service/prometheus/AlarmService.java [397:598]
public Result editRule(AppAlarmRule rule,AlarmRuleData ruleData,AppMonitor app,String user){
/**
* modifiable field:
* cname
* expr
* for
* labels
* annotations
* group
* priority
* env
* alert_team
* alert_member
*/
JsonObject jsonObject = new JsonObject();
/**
* cname
*/
if(StringUtils.isNotBlank(rule.getCname())){
jsonObject.addProperty("cname", rule.getCname());
}
/**
* for
*/
if(StringUtils.isNotBlank(rule.getForTime())){
jsonObject.addProperty("for", rule.getForTime());
jsonObject.addProperty("forTime", rule.getForTime());
}
/**
* annotations
*/
StringBuilder title = new StringBuilder().append(app.getProjectName());
AlarmPresetMetricsPOJO metrics = alarmPresetMetricsService.getByCode(rule.getAlert());
if (metrics != null) {
title.append("&").append(metrics.getMessage());
} else {
//check tesla metrics
teslaService.checkTeslaMetrics(title, rule.getAlert());
}
JsonObject jsonSummary = new JsonObject();
jsonSummary.addProperty("title", title.toString());
if (StringUtils.isNotBlank(rule.getRemark())) {
jsonSummary.addProperty("summary", rule.getRemark());
}
if (StringUtils.isNotBlank(ruleData.getAlarmCallbackUrl())) {
jsonSummary.addProperty("callback_url", ruleData.getAlarmCallbackUrl());
}
jsonObject.add("annotations", jsonSummary);
/**
* priority
*/
if(StringUtils.isNotBlank(rule.getPriority())){
jsonObject.addProperty("priority", rule.getPriority());
}
/**
* labels
*/
JsonObject labels = new JsonObject();
labels.addProperty("exceptViewLables","detailRedirectUrl.paramType");
if(StringUtils.isNotBlank(ruleData.getAlarmDetailUrl())){
labels.addProperty("detailRedirectUrl",ruleData.getAlarmDetailUrl());
labels.addProperty("paramType","customerPromql");
}
alertUrlHelper.buildDetailRedirectUrl(user, app, rule.getAlert(), jsonSummary, labels);
labels.addProperty("send_interval",rule.getSendInterval());
labels.addProperty("app_iam_id",String.valueOf(rule.getIamId()));
labels.addProperty("project_id",String.valueOf(rule.getProjectId()));
labels.addProperty("project_name",app.getProjectName());
if (StringUtils.isNotBlank(rule.getAlert())) {
labels.addProperty("alert_key",rule.getAlert());
}
if (StringUtils.isNotBlank(rule.getOp())) {
labels.addProperty("alert_op",rule.getOp());
}
if(rule.getMetricType() == AlarmRuleMetricType.customer_promql.getCode()){
String ruleExpr = ruleData.getExpr();
Set set = new HashSet();
set.add(ruleExpr.lastIndexOf(">="));
set.add(ruleExpr.lastIndexOf("<="));
set.add(ruleExpr.lastIndexOf("!="));
set.add(ruleExpr.lastIndexOf(">"));
set.add(ruleExpr.lastIndexOf("<"));
set.add(ruleExpr.lastIndexOf("="));
List<Integer> indexSet = (List) set.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
int a = CollectionUtils.isEmpty(indexSet) ? -1 : indexSet.get(0);
log.info("add customer_promql ruleExpr :{},a:{}",ruleExpr,a);
String value = "0.0";
if (a > 0) {
try {
value = ruleExpr.substring(a + 1).trim();
} catch (NumberFormatException e) {
log.error(e.getMessage() + "ruleExpr : {} ; a : {}", ruleExpr, a, e);
}
}
labels.addProperty("alert_value",value);
}else if (rule.getValue() != null) {
labels.addProperty("alert_value",rule.getValue().toString());
}
if (metrics != null) {
labels.addProperty("calert",metrics.getMessage());
labels.addProperty("group_key",metrics.getGroupKey().getCode());
} else {
labels.addProperty("calert",rule.getAlert());
}
ReqErrorMetricsPOJO errMetrics = reqErrorMetricsService.getErrorMetricsByMetrics(rule.getAlert());
if (errMetrics != null) {
//error metric flag
labels.addProperty("metrics_flag","1");
labels.addProperty("metrics",errMetrics.getCode());
}
ReqSlowMetricsPOJO slowMetrics = reqSlowMetricsService.getSlowMetricsByMetric(rule.getAlert());
if (slowMetrics != null) {
//slow query metric flag
labels.addProperty("metrics_flag","2");
labels.addProperty("metrics",slowMetrics.getCode());
}
ResourceUsageMetrics errorMetricsByMetrics = ResourceUsageMetrics.getErrorMetricsByMetrics(rule.getAlert());
if (errorMetricsByMetrics != null) {
//resource usage flag
labels.addProperty("metrics_flag",errorMetricsByMetrics.getMetricsFlag());
labels.addProperty("metrics",errorMetricsByMetrics.getCode());
}
jsonObject.add("labels", labels);
/**
* expr
*/
if(rule.getMetricType().equals(AlarmRuleMetricType.preset.getCode())){
String evaluationIntervalS = evaluationDuration + evaluationUnit;
String expr = getExpr(rule,evaluationIntervalS,ruleData, app);
log.info("presetMetric expr===========" + expr);
if(StringUtils.isBlank(expr)){
log.error("getExpr error!rule:{},projectName:{}",rule.toString(),app.getProjectName());
return Result.fail(ErrorCode.unknownError);
}
jsonObject.addProperty("expr", expr);
rule.setExpr(expr);
}else if(rule.getMetricType().equals(AlarmRuleMetricType.customer_promql.getCode())){
log.info("customer customize expr:projectId:{},projectName:{},expr:{}",app.getProjectId(),app.getProjectName(),ruleData.getExpr());
jsonObject.addProperty("expr", ruleData.getExpr());
rule.setExpr(ruleData.getExpr());
}
/**
* alert team and alert_members
*/
String alertTeamJson = rule.getAlertTeam();
List<String> alertMembers = ruleData.getAlertMembers();
if(StringUtils.isBlank(alertTeamJson) && CollectionUtils.isEmpty(alertMembers)){
log.error("AlarmService.editRule error! invalid alarmTeam and alertMembers param!");
return Result.fail(ErrorCode.ALERT_TEAM_AND_ALERT_MEMBERS_BOTH_EMPTY);
}
if(StringUtils.isNotBlank(alertTeamJson)){
JsonArray array = new Gson().fromJson(alertTeamJson, JsonArray.class);
jsonObject.add("alert_team", array);
}
JsonArray membersArray = new JsonArray();
if(!CollectionUtils.isEmpty(alertMembers)){
membersArray = new Gson().fromJson(JSON.toJSONString(alertMembers), JsonArray.class);
}
jsonObject.add("alert_member", membersArray);
JsonArray atMembersArray = new JsonArray();
if(!CollectionUtils.isEmpty(ruleData.getAtMembers())){
atMembersArray = new Gson().fromJson(JSON.toJSONString(ruleData.getAtMembers()), JsonArray.class);
}
jsonObject.add("alert_at_people", atMembersArray);
return alertServiceAdapt.editRule(rule.getAlarmId(),jsonObject,String.valueOf(rule.getIamId()),user);
}