Compare commits

...

2 Commits

Author SHA1 Message Date
ning
103708f321 code refactor 2024-12-06 20:19:19 +08:00
ning
09f4bec229 fix: eval interval 2024-12-06 19:11:12 +08:00
2 changed files with 15 additions and 4 deletions

View File

@@ -104,9 +104,17 @@ func NewAlertRuleWorker(rule *models.AlertRule, datasourceId int64, Processor *p
Processor.ScheduleEntry = arw.Scheduler.Entry(entryID)
Processor.PromEvalInterval = getPromEvalInterval(Processor.ScheduleEntry.Schedule)
return arw
}
func getPromEvalInterval(schedule cron.Schedule) int {
now := time.Now()
next1 := schedule.Next(now)
next2 := schedule.Next(next1)
return int(next2.Sub(next1).Seconds())
}
func (arw *AlertRuleWorker) Key() string {
return common.RuleKey(arw.DatasourceId, arw.Rule.Id)
}
@@ -130,7 +138,10 @@ func (arw *AlertRuleWorker) Start() {
}
func (arw *AlertRuleWorker) Eval() {
arw.Processor.EvalStart = time.Now().Unix()
if arw.Processor.PromEvalInterval == 0 {
arw.Processor.PromEvalInterval = getPromEvalInterval(arw.Processor.ScheduleEntry.Schedule)
}
cachedRule := arw.Rule
if cachedRule == nil {
// logger.Errorf("rule_eval:%s Rule not found", arw.Key())

View File

@@ -80,8 +80,8 @@ type Processor struct {
HandleRecoverEventHook HandleEventFunc
EventMuteHook EventMuteHookFunc
ScheduleEntry cron.Entry
EvalStart int64
ScheduleEntry cron.Entry
PromEvalInterval int
}
func (p *Processor) Key() string {
@@ -424,6 +424,7 @@ func (p *Processor) handleEvent(events []*models.AlertCurEvent) {
p.pendingsUseByRecover.Set(event.Hash, event)
}
event.PromEvalInterval = p.PromEvalInterval
if p.rule.PromForDuration == 0 {
fireEvents = append(fireEvents, event)
if severity > event.Severity {
@@ -442,7 +443,6 @@ func (p *Processor) handleEvent(events []*models.AlertCurEvent) {
preTriggerTime = event.TriggerTime
}
event.PromEvalInterval = int(p.ScheduleEntry.Schedule.Next(time.Unix(p.EvalStart, 0)).Unix() - p.EvalStart)
if event.LastEvalTime-preTriggerTime+int64(event.PromEvalInterval) >= int64(p.rule.PromForDuration) {
fireEvents = append(fireEvents, event)
if severity > event.Severity {