Skip to content

Commit

Permalink
[alerter] Enhanced reporting of external general alert API (#1326)
Browse files Browse the repository at this point in the history
  • Loading branch information
SurryChen authored Nov 14, 2023
1 parent e2b2931 commit 5a022e7
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hertzbeat.alert.dto.CloudAlertReportAbstract;
import org.dromara.hertzbeat.alert.dto.GeneralCloudAlertReport;
import org.dromara.hertzbeat.alert.enums.CloudServiceAlarmInformationEnum;
import org.dromara.hertzbeat.alert.service.AlertService;
import org.dromara.hertzbeat.common.entity.dto.AlertReport;
Expand All @@ -13,7 +14,6 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.Optional;

Expand Down Expand Up @@ -46,7 +46,7 @@ public ResponseEntity<Message<Void>> addNewAlertReportFromCloud(@PathVariable("c
if (cloudService != null) {
try {
// 实例化对应的Class
CloudAlertReportAbstract cloudAlertReport = (CloudAlertReportAbstract) JsonUtil
CloudAlertReportAbstract cloudAlertReport = JsonUtil
.fromJson(alertReport, cloudService.getCloudServiceAlarmInformationEntity());
// 模板填充
alert = AlertReport.builder()
Expand Down Expand Up @@ -80,10 +80,16 @@ public ResponseEntity<Message<Void>> addNewAlertReportFromCloud(@PathVariable("c
}

@PostMapping
@Operation(summary = "Interface for reporting external alarm information | 对外上报告警信息 接口",
description = "对外 新增一个告警")
public ResponseEntity<Message<Void>> addNewAlertReport(@Valid @RequestBody AlertReport alertReport) {
// 校验请求数据 TODO
@Operation(summary = "Interface for reporting external and general alarm information | 对外上报告警信息 接口",
description = "对外 新增一个云服务通用告警")
public ResponseEntity<Message<Void>> addNewAlertReport(@RequestBody GeneralCloudAlertReport alertReport) {
try {
alertReport.refreshAlertTime();
} catch (Exception e) {
log.error("[AlertReportController]:" + e.getMessage() +
",请求实体:" + JsonUtil.toJson(alertReport));
throw e;
}
alertService.addNewAlertReport(alertReport);
return ResponseEntity.ok(Message.success("Add report success"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,48 +14,48 @@ public abstract class CloudAlertReportAbstract {
* 云告警转为内部告警时的告警名称
*/
@JsonIgnore
public abstract String getAlertName() throws Exception;
public abstract String getAlertName();

/**
* 云告警转为内部告警时的告警间隔
*/
@JsonIgnore
public abstract Integer getAlertDuration() throws Exception;
public abstract Integer getAlertDuration();

/**
* 云告警转为内部告警时的告警时间
*/
@JsonIgnore
public abstract long getAlertTime() throws Exception;
public abstract long getAlertTime();

/**
* 云告警转为内部告警时的告警严重程度
*/
@JsonIgnore
public abstract Integer getPriority() throws Exception;
public abstract Integer getPriority();

/**
* 云告警转为内部告警时的告警类型
*/
@JsonIgnore
public abstract Integer getReportType() throws Exception;
public abstract Integer getReportType();

/**
* 云告警转为内部告警时的告警标签信息
*/
@JsonIgnore
public abstract Map<String, String> getLabels() throws Exception;
public abstract Map<String, String> getLabels();

/**
* 云告警转为内部告警时的告警标注
*/
@JsonIgnore
public abstract Map<String, String> getAnnotations() throws Exception;
public abstract Map<String, String> getAnnotations();

/**
* 云告警转为内部告警时的告警内容
*/
@JsonIgnore
public abstract String getContent() throws Exception;
public abstract String getContent();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.dromara.hertzbeat.alert.dto;

import lombok.*;
import org.apache.commons.lang3.StringUtils;
import org.dromara.hertzbeat.alert.util.DateUtil;
import org.dromara.hertzbeat.common.entity.dto.AlertReport;

/**
* 通用云端告警实体类
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class GeneralCloudAlertReport extends AlertReport {

/**
* 告警日期时间
*/
private String alertDateTime;

/**
* 日期时间格式
*/
private String dateTimeFormat;

/**
* 可通过增强属性刷新告警时间的时间戳
*/
public void refreshAlertTime() {
// 有时间戳,取时间戳
if (getAlertTime() != 0L) {
return;
}
// 没有时间戳,判断是否有字符串配置
if (StringUtils.isNotBlank(alertDateTime)) {
Long timeStamp = null;
// 优先用户配置
if (StringUtils.isNotBlank(dateTimeFormat)) {
timeStamp = DateUtil.getTimeStampFromFormat(alertDateTime, dateTimeFormat);
}
// 默认支持日期格式
if (timeStamp == null) {
timeStamp = DateUtil.getTimeStampFromSomeFormats(alertDateTime);
}
// 解析成功
if (timeStamp != null) {
setAlertTime(timeStamp);
return;
}
}
throw new RuntimeException("告警时间解析异常");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;
import org.dromara.hertzbeat.alert.util.DateUtil;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;

/**
Expand Down Expand Up @@ -66,6 +65,7 @@ public static class AlarmPolicyInfo {
@AllArgsConstructor
@NoArgsConstructor
public static class Conditions {
// 指标告警的参数
private String metricName;
private String metricShowName;
private String calcType;
Expand All @@ -78,6 +78,12 @@ public static class Conditions {
private String periodNum;
private String alarmNotifyType;
private long alarmNotifyPeriod;

// 事件告警的参数
private String productName;
private String productShowName;
private String eventName;
private String eventShowName;
}

@Override
Expand All @@ -91,11 +97,8 @@ public Integer getAlertDuration() {
}

@Override
public long getAlertTime() throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long occurTime;
occurTime = sdf.parse(getFirstOccurTime()).getTime();
return occurTime;
public long getAlertTime() {
return DateUtil.getTimeStampFromFormat(getFirstOccurTime(), "yyyy-MM-dd HH:mm:ss");
}

@Override
Expand All @@ -118,30 +121,71 @@ public Map<String, String> getAnnotations() {
return Map.of("app", "TenCloud");
}

/**
* 事务告警
*/
private static final String EVENT = "event";

/**
* 指标告警
*/
private static final String METRIC = "metric";

/**
* 如果后续腾讯云告警类型增多的话,可以将该实体类拆分为一个父类和多个子类,然后在子类实现该方法即可
* 由于目前只有两种,暂不进行拆分
*/
@Override
public String getContent() {
StringBuilder contentBuilder = new StringBuilder();
return contentBuilder
.append("[")
.append("告警对象:地区")
.append(getAlarmObjInfo().getRegion()).append("|")
.append(getAlarmObjInfo().getNamespace())
.append("]")
.append("[")
.append("告警内容:")
.append(getAlarmPolicyInfo().getPolicyTypeCname()).append("|")
.append(getAlarmPolicyInfo().getConditions().getMetricShowName()).append("|")
.append(getAlarmPolicyInfo().getConditions().getMetricName())
.append(getAlarmPolicyInfo().getConditions().getCalcType())
.append(getAlarmPolicyInfo().getConditions().getCalcValue())
.append(getAlarmPolicyInfo().getConditions().getCalcUnit())
.append("]")
.append("[")
.append("当前数据")
.append(getAlarmPolicyInfo().getConditions().getCurrentValue())
.append(getAlarmPolicyInfo().getConditions().getCalcUnit())
.append("]")
.toString();
// 判断类型
if (EVENT.equals(getAlarmType())) {
contentBuilder
.append("[")
.append("告警状态 | ")
.append("0".equals(alarmStatus) ? "恢复" : "告警")
.append("]\n")
.append("[")
.append("告警对象信息 | ")
.append(getAlarmObjInfo().getRegion() == null ? "" : "region:" + getAlarmObjInfo().getRegion())
.append(";").append("appId:").append(getAlarmObjInfo().getAppId())
.append(";").append("uni:").append(getAlarmObjInfo().getUin())
.append(";").append("unInstanceId:").append(getAlarmObjInfo().getDimensions().getUnInstanceId())
.append("]\n")
.append("[")
.append("告警策略组信息 | ")
.append("名称:").append(getAlarmPolicyInfo().getPolicyName())
.append(";")
.append("策略类型展示名称:").append(getAlarmPolicyInfo().getConditions().getProductName())
.append(",").append(getAlarmPolicyInfo().getConditions().getProductShowName())
.append(";")
.append("事件告警名称:").append(getAlarmPolicyInfo().getConditions().getEventName())
.append(",").append(getAlarmPolicyInfo().getConditions().getEventShowName())
.append("]");
} else if (METRIC.equals(getAlarmType())) {
contentBuilder
.append("[")
.append("告警对象:")
.append(getAlarmObjInfo().getRegion() == null ? "" : getAlarmObjInfo().getRegion())
.append(getAlarmObjInfo().getRegion() == null ? "" : "|")
.append(getAlarmObjInfo().getNamespace())
.append("]")
.append("[")
.append("告警内容:")
.append(getAlarmPolicyInfo().getPolicyTypeCname()).append("|")
.append(getAlarmPolicyInfo().getConditions().getMetricShowName()).append("|")
.append(getAlarmPolicyInfo().getConditions().getMetricName())
.append(getAlarmPolicyInfo().getConditions().getCalcType())
.append(getAlarmPolicyInfo().getConditions().getCalcValue())
.append(getAlarmPolicyInfo().getConditions().getCalcUnit())
.append("]")
.append("[")
.append("当前数据")
.append(getAlarmPolicyInfo().getConditions().getCurrentValue())
.append(getAlarmPolicyInfo().getConditions().getCalcUnit())
.append("]");
}
return contentBuilder.toString();
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.dromara.hertzbeat.alert.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
* 记录一些常用的日期格式
*/
public class DateUtil {

private static final String[] DATE_FORMATS = {
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
"yyyy-MM-dd HH:mm:ss"};

/**
* 常用日期格式转时间戳
*/
public static Long getTimeStampFromSomeFormats(String date) {
SimpleDateFormat sdf;
for (String dateFormat : DATE_FORMATS) {
try {
sdf = new SimpleDateFormat(dateFormat);
return sdf.parse(date).getTime();
} catch (ParseException e) {}
}
return null;
}

/**
* 指定日期格式转换时间戳
*/
public static Long getTimeStampFromFormat(String date, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
return sdf.parse(date).getTime();
} catch (Exception e) {
throw new RuntimeException("时间格式解析异常!");
}
}

}
Loading

0 comments on commit 5a022e7

Please sign in to comment.