Skip to content

Commit

Permalink
refactor alert eval (ccfos#1616)
Browse files Browse the repository at this point in the history
  • Loading branch information
710leo authored Jul 10, 2023
1 parent ff3ea7d commit 909bbb5
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
6 changes: 4 additions & 2 deletions alert/eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,16 @@ func (arw *AlertRuleWorker) Start() {
if interval <= 0 {
interval = 10
}

ticker := time.NewTicker(time.Duration(interval) * time.Second)
go func() {
defer ticker.Stop()
for {
select {
case <-arw.quit:
return
default:
case <-ticker.C:
arw.Eval()
time.Sleep(time.Duration(interval) * time.Second)
}
}
}()
Expand Down
8 changes: 4 additions & 4 deletions alert/mute/mute.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ func TimeNonEffectiveMuteStrategy(rule *models.AlertRule, event *models.AlertCur
if !strings.Contains(enableDaysOfWeek[i], triggerWeek) {
continue
}
if enableStime[i] <= enableEtime[i] {
if enableStime[i] < enableEtime[i] {
if triggerTime < enableStime[i] || triggerTime > enableEtime[i] {
continue
}
} else {
if triggerTime < enableStime[i] && triggerTime > enableEtime[i] {
} else if enableStime[i] > enableEtime[i] {
if triggerTime < enableStime[i] && triggerTime >= enableEtime[i] {
continue
}
}
Expand Down Expand Up @@ -165,7 +165,7 @@ func matchMute(event *models.AlertCurEvent, mute *models.AlertMute, clock ...int
break
}
} else {
if triggerTime < mute.PeriodicMutesJson[i].EnableStime || triggerTime >= mute.PeriodicMutesJson[i].EnableEtime {
if triggerTime >= mute.PeriodicMutesJson[i].EnableStime || triggerTime < mute.PeriodicMutesJson[i].EnableEtime {
matchTime = true
break
}
Expand Down
2 changes: 1 addition & 1 deletion alert/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func (p *Processor) fireEvent(event *models.AlertCurEvent) {
}

// 之前发送过告警了,这次是否要继续发送,要看是否过了通道静默时间
if event.LastEvalTime > fired.LastSentTime+int64(cachedRule.NotifyRepeatStep)*60 {
if event.LastEvalTime >= fired.LastSentTime+int64(cachedRule.NotifyRepeatStep)*60 {
if cachedRule.NotifyMaxNumber == 0 {
// 最大可以发送次数如果是0,表示不想限制最大发送次数,一直发即可
event.NotifyCurNumber = fired.NotifyCurNumber + 1
Expand Down
6 changes: 4 additions & 2 deletions alert/record/prom_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,16 @@ func (rrc *RecordRuleContext) Start() {
if interval <= 0 {
interval = 10
}

ticker := time.NewTicker(time.Duration(interval) * time.Second)
go func() {
defer ticker.Stop()
for {
select {
case <-rrc.quit:
return
default:
case <-ticker.C:
rrc.Eval()
time.Sleep(time.Duration(interval) * time.Second)
}
}
}()
Expand Down

0 comments on commit 909bbb5

Please sign in to comment.