Skip to content

Commit

Permalink
feat(alarm): 优化告警历史数据结构和处理逻辑
Browse files Browse the repository at this point in the history
- 移除 AlertHistoryItem 中的 MetricLevel 和 EventLevel 字段- 在 AlertHistoryItem 中添加多个策略级别字段,包括 MetricLevel、EventLevel、PortLevel、HttpLevel 和 DomainLevel
- 更新 history.go 中的处理逻辑,根据策略类型动态解析级别信息
- 在 alarm_history.go 中添加 RawInfo 字段和 GetRawInfo 方法,用于获取告警原始数据
  • Loading branch information
aide-cloud committed Jan 16, 2025
1 parent 3d69e67 commit 321b996
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
14 changes: 10 additions & 4 deletions api/admin/model.proto
Original file line number Diff line number Diff line change
Expand Up @@ -701,8 +701,6 @@ message AlarmHistoryItem{
string endsAt = 3;
// 告警状态
AlertStatus alertStatus = 4;
// Metric告警级别
StrategyMetricLevelItem metricLevel = 5;
// 告警策略
StrategyItem strategy = 7;
// 告警详情
Expand All @@ -723,8 +721,16 @@ message AlarmHistoryItem{
string summary = 15;
// 持续时间
string duration = 16;
// MQ告警级别
StrategyEventLevelItem eventLevel = 17;
// 根据策略等级配置的详细策略, key为策略等级ID
StrategyMetricLevelItem metricLevel = 21;
// MQ等级
StrategyEventLevelItem eventLevel = 22;
// 端口监控策略
StrategyPortLevelItem portLevel = 23;
// HTTP监控策略
StrategyHTTPLevelItem httpLevel = 24;
// 域名监控策略
StrategyDomainLevelItem domainLevel = 25;
}

message NoticeUserMessage {
Expand Down
36 changes: 31 additions & 5 deletions cmd/server/palace/internal/service/builder/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/aide-family/moon/pkg/palace/model/bizmodel"
"github.com/aide-family/moon/pkg/util/types"
"github.com/aide-family/moon/pkg/vobj"
"github.com/go-kratos/kratos/v2/log"
)

var _ IAlarmHistoryModuleBuilder = (*alarmHistoryModuleBuilder)(nil)
Expand Down Expand Up @@ -72,17 +73,21 @@ func (a *doAlarmHistoryBuilder) ToAPI(history *alarmmodel.AlarmHistory) *admin.A
StartsAt: history.StartsAt,
EndsAt: history.EndsAt,
AlertStatus: api.AlertStatus(history.AlertStatus),
MetricLevel: nil,
Strategy: nil,
Description: history.Description,
Expr: history.Expr,
Datasource: nil,
Fingerprint: history.Fingerprint,
RawInfo: "",
RawInfo: history.GetRawInfo().RawInfo,
Labels: history.Labels.Map(),
Annotations: history.Annotations.Map(),
Summary: history.Summary,
Duration: types.NewTimeByString(endAt).Sub(types.NewTimeByString(history.StartsAt).Time).String(),
MetricLevel: nil,
EventLevel: nil,
PortLevel: nil,
HttpLevel: nil,
DomainLevel: nil,
}

details := history.HistoryDetails
Expand All @@ -95,9 +100,30 @@ func (a *doAlarmHistoryBuilder) ToAPI(history *alarmmodel.AlarmHistory) *admin.A
_ = strategy.UnmarshalBinary([]byte(details.Strategy))
resItem.Strategy = NewParamsBuild(a.ctx).StrategyModuleBuilder().DoStrategyBuilder().ToAPI(strategy)

level := &bizmodel.StrategyMetricLevel{}
_ = level.UnmarshalBinary([]byte(details.Level))
resItem.MetricLevel = NewParamsBuild(a.ctx).StrategyModuleBuilder().DoStrategyLevelBuilder().ToMetricAPI(level)
switch strategy.StrategyType {
case vobj.StrategyTypeMetric:
level := &bizmodel.StrategyMetricLevel{}
_ = level.UnmarshalBinary([]byte(details.Level))
resItem.MetricLevel = NewParamsBuild(a.ctx).StrategyModuleBuilder().DoStrategyLevelBuilder().ToMetricAPI(level)
case vobj.StrategyTypeEvent:
level := &bizmodel.StrategyEventLevel{}
_ = level.UnmarshalBinary([]byte(details.Level))
resItem.EventLevel = NewParamsBuild(a.ctx).StrategyModuleBuilder().DoStrategyLevelBuilder().ToEventAPI(level)
case vobj.StrategyTypeDomainPort:
level := &bizmodel.StrategyPortLevel{}
_ = level.UnmarshalBinary([]byte(details.Level))
resItem.PortLevel = NewParamsBuild(a.ctx).StrategyModuleBuilder().DoStrategyLevelBuilder().ToPortAPI(level)
case vobj.StrategyTypeDomainCertificate:
level := &bizmodel.StrategyDomainLevel{}
_ = level.UnmarshalBinary([]byte(details.Level))
resItem.DomainLevel = NewParamsBuild(a.ctx).StrategyModuleBuilder().DoStrategyLevelBuilder().ToDomainAPI(level)
case vobj.StrategyTypeHTTP:
level := &bizmodel.StrategyHTTPLevel{}
_ = level.UnmarshalBinary([]byte(details.Level))
resItem.HttpLevel = NewParamsBuild(a.ctx).StrategyModuleBuilder().DoStrategyLevelBuilder().ToHTTPLevelAPI(level)
default:
log.Warnf("unknown strategy type: %s", strategy.StrategyType)
}
}

return resItem
Expand Down
10 changes: 10 additions & 0 deletions pkg/palace/model/alarmmodel/alarm_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ type AlarmHistory struct {
RawInfoID uint32 `gorm:"column:raw_info_id;type:int;comment:告警原始数据id;uniqueIndex:idx__history__notice__raw_info_id,priority:1" json:"rawInfoId"`
// 附加信息
HistoryDetails *HistoryDetails `gorm:"foreignKey:AlarmHistoryID;comment:附加信息"`
// 告警原始数据
RawInfo *AlarmRaw `gorm:"foreignKey:RawInfoID;comment:告警原始数据"`
}

// String json string
Expand Down Expand Up @@ -72,3 +74,11 @@ func (a *AlarmHistory) GetHistoryDetails() *HistoryDetails {
}
return a.HistoryDetails
}

// GetRawInfo 获取告警原始数据
func (a *AlarmHistory) GetRawInfo() *AlarmRaw {
if types.IsNil(a) || types.IsNil(a.RawInfo) {
return &AlarmRaw{}
}
return a.RawInfo
}

0 comments on commit 321b996

Please sign in to comment.