add last_sent_time for alert_cur_event

This commit is contained in:
UlricQin 2022-01-29 13:46:10 +08:00
parent 8408220870
commit 9beef8f36a
2 changed files with 15 additions and 10 deletions

View File

@ -37,7 +37,8 @@ type AlertCurEvent struct {
TagsMap map[string]string `json:"-" gorm:"-"` // for internal usage TagsMap map[string]string `json:"-" gorm:"-"` // for internal usage
IsRecovered bool `json:"is_recovered" gorm:"-"` // for notify.py IsRecovered bool `json:"is_recovered" gorm:"-"` // for notify.py
NotifyUsersObj []*User `json:"notify_users_obj" gorm:"-"` // for notify.py NotifyUsersObj []*User `json:"notify_users_obj" gorm:"-"` // for notify.py
LastEvalTime int64 `json:"last_eval_time" gorm:"-"` // for notify.py LastEvalTime int64 `json:"last_eval_time" gorm:"-"` // for notify.py 上次计算的时间
LastSentTime int64 `json:"last_sent_time" gorm:"-"` // 上次发送时间
} }
func (e *AlertCurEvent) TableName() string { func (e *AlertCurEvent) TableName() string {

View File

@ -256,7 +256,6 @@ func (r RuleEval) judge(vectors []Vector) {
event.NotifyChannelsJSON = r.rule.NotifyChannelsJSON event.NotifyChannelsJSON = r.rule.NotifyChannelsJSON
event.NotifyGroups = r.rule.NotifyGroups event.NotifyGroups = r.rule.NotifyGroups
event.NotifyGroupsJSON = r.rule.NotifyGroupsJSON event.NotifyGroupsJSON = r.rule.NotifyGroupsJSON
event.NotifyRepeatNext = now + int64(r.rule.NotifyRepeatStep*60)
event.TargetIdent = string(targetIdent) event.TargetIdent = string(targetIdent)
event.TargetNote = targetNote event.TargetNote = targetNote
event.TriggerValue = readableValue(vectors[i].Value) event.TriggerValue = readableValue(vectors[i].Value)
@ -301,7 +300,7 @@ func (r RuleEval) handleNewEvent(event *models.AlertCurEvent) {
_, has := r.pendings[event.Hash] _, has := r.pendings[event.Hash]
if has { if has {
r.pendings[event.Hash].LastEvalTime = event.TriggerTime r.pendings[event.Hash].LastEvalTime = event.LastEvalTime
} else { } else {
r.pendings[event.Hash] = event r.pendings[event.Hash] = event
} }
@ -313,19 +312,19 @@ func (r RuleEval) handleNewEvent(event *models.AlertCurEvent) {
func (r RuleEval) fireEvent(event *models.AlertCurEvent) { func (r RuleEval) fireEvent(event *models.AlertCurEvent) {
if fired, has := r.fires[event.Hash]; has { if fired, has := r.fires[event.Hash]; has {
r.fires[event.Hash].LastEvalTime = event.LastEvalTime
if r.rule.NotifyRepeatStep == 0 { if r.rule.NotifyRepeatStep == 0 {
// 说明不想重复通知那就直接返回了nothing to do // 说明不想重复通知那就直接返回了nothing to do
return return
} }
// 之前发送过告警了,这次是否要继续发送,要看是否过了通道静默时间 // 之前发送过告警了,这次是否要继续发送,要看是否过了通道静默时间
if event.LastEvalTime > fired.LastEvalTime+int64(r.rule.NotifyRepeatStep)*60 { if event.LastEvalTime > fired.LastSentTime+int64(r.rule.NotifyRepeatStep)*60 {
r.fires[event.Hash] = event r.pushEventToQueue(event)
pushEventToQueue(event)
} }
} else { } else {
r.fires[event.Hash] = event r.pushEventToQueue(event)
pushEventToQueue(event)
} }
} }
@ -372,12 +371,17 @@ func (r RuleEval) recoverRule(alertingKeys map[string]struct{}, now int64) {
event.NotifyChannelsJSON = r.rule.NotifyChannelsJSON event.NotifyChannelsJSON = r.rule.NotifyChannelsJSON
event.NotifyGroups = r.rule.NotifyGroups event.NotifyGroups = r.rule.NotifyGroups
event.NotifyGroupsJSON = r.rule.NotifyGroupsJSON event.NotifyGroupsJSON = r.rule.NotifyGroupsJSON
pushEventToQueue(event) r.pushEventToQueue(event)
} }
} }
} }
func pushEventToQueue(event *models.AlertCurEvent) { func (r RuleEval) pushEventToQueue(event *models.AlertCurEvent) {
if !event.IsRecovered {
event.LastSentTime = event.LastEvalTime
r.fires[event.Hash] = event
}
promstat.CounterAlertsTotal.WithLabelValues(config.C.ClusterName).Inc() promstat.CounterAlertsTotal.WithLabelValues(config.C.ClusterName).Inc()
logEvent(event, "push_queue") logEvent(event, "push_queue")
if !EventQueue.PushFront(event) { if !EventQueue.PushFront(event) {