refactor repeater
This commit is contained in:
parent
3428b11ea8
commit
837cfab1bd
|
@ -58,15 +58,7 @@ func persist(event *models.AlertCurEvent) {
|
||||||
|
|
||||||
his := event.ToHis()
|
his := event.ToHis()
|
||||||
|
|
||||||
if has {
|
// 不管是告警还是恢复,全量告警里都要记录
|
||||||
// 数据库里有这个事件,说明之前触发过了
|
|
||||||
if event.IsRecovered {
|
|
||||||
// 本次恢复了,把未恢复的事件删除,在全量告警里添加记录
|
|
||||||
err := models.AlertCurEventDelByHash(event.Hash)
|
|
||||||
if err != nil {
|
|
||||||
logger.Errorf("event_del_cur_fail: %v hash=%s", err, event.Hash)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := his.Add(); err != nil {
|
if err := his.Add(); err != nil {
|
||||||
logger.Errorf(
|
logger.Errorf(
|
||||||
"event_persist_his_fail: %v rule_id=%d hash=%s tags=%v timestamp=%d value=%s",
|
"event_persist_his_fail: %v rule_id=%d hash=%s tags=%v timestamp=%d value=%s",
|
||||||
|
@ -78,7 +70,34 @@ func persist(event *models.AlertCurEvent) {
|
||||||
event.TriggerValue,
|
event.TriggerValue,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if has {
|
||||||
|
// 活跃告警表中有记录,删之
|
||||||
|
err = models.AlertCurEventDelByHash(event.Hash)
|
||||||
|
if err != nil {
|
||||||
|
logger.Errorf("event_del_cur_fail: %v hash=%s", err, event.Hash)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !event.IsRecovered {
|
||||||
|
// 恢复事件,从活跃告警列表彻底删掉,告警事件,要重新加进来新的event
|
||||||
|
// use his id as cur id
|
||||||
|
event.Id = his.Id
|
||||||
|
if event.Id > 0 {
|
||||||
|
if err := event.Add(); err != nil {
|
||||||
|
logger.Errorf(
|
||||||
|
"event_persist_cur_fail: %v rule_id=%d hash=%s tags=%v timestamp=%d value=%s",
|
||||||
|
err,
|
||||||
|
event.RuleId,
|
||||||
|
event.Hash,
|
||||||
|
event.TagsJSON,
|
||||||
|
event.TriggerTime,
|
||||||
|
event.TriggerValue,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,19 +106,6 @@ func persist(event *models.AlertCurEvent) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 本次是告警,alert_cur_event表里也没有数据
|
|
||||||
if err := his.Add(); err != nil {
|
|
||||||
logger.Errorf(
|
|
||||||
"event_persist_his_fail: %v rule_id=%d hash=%s tags=%v timestamp=%d value=%s",
|
|
||||||
err,
|
|
||||||
event.RuleId,
|
|
||||||
event.Hash,
|
|
||||||
event.TagsJSON,
|
|
||||||
event.TriggerTime,
|
|
||||||
event.TriggerValue,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// use his id as cur id
|
// use his id as cur id
|
||||||
event.Id = his.Id
|
event.Id = his.Id
|
||||||
if event.Id > 0 {
|
if event.Id > 0 {
|
||||||
|
|
|
@ -21,7 +21,7 @@ func Start(ctx context.Context) error {
|
||||||
go loopFilterRules(ctx)
|
go loopFilterRules(ctx)
|
||||||
|
|
||||||
// repeat notifier
|
// repeat notifier
|
||||||
go loopRepeat(ctx)
|
// go loopRepeat(ctx)
|
||||||
|
|
||||||
go reportQueueSize()
|
go reportQueueSize()
|
||||||
|
|
||||||
|
|
|
@ -288,8 +288,18 @@ func labelMapToArr(m map[string]string) []string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r RuleEval) handleNewEvent(event *models.AlertCurEvent) {
|
func (r RuleEval) handleNewEvent(event *models.AlertCurEvent) {
|
||||||
if _, has := r.fires[event.Hash]; has {
|
if fired, has := r.fires[event.Hash]; has {
|
||||||
// fired before, nothing to do
|
if r.rule.NotifyRepeatStep == 0 {
|
||||||
|
// 说明不想重复通知,那就直接返回了,nothing to do
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 之前发送过告警了,这次是否要继续发送,要看是否过了通道静默时间
|
||||||
|
if event.LastEvalTime > fired.LastEvalTime+int64(r.rule.NotifyRepeatStep) {
|
||||||
|
r.fires[event.Hash] = event
|
||||||
|
pushEventToQueue(event)
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue