Skip to content

Commit 62e47fb

Browse files
yhabteabjulianbrost
authored andcommitted
Introduce Rule#Eval() & Escalation#Eval() methods
1 parent 29eb3f3 commit 62e47fb

File tree

3 files changed

+36
-29
lines changed

3 files changed

+36
-29
lines changed

internal/incident/incident.go

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -360,21 +360,19 @@ func (i *Incident) evaluateRules(ctx context.Context, tx *sqlx.Tx, eventID int64
360360
}
361361

362362
if _, ok := i.Rules[r.ID]; !ok {
363-
if r.ObjectFilter != nil {
364-
matched, err := r.ObjectFilter.Eval(i.Object)
365-
if err != nil {
366-
i.logger.Warnw("Failed to evaluate object filter", zap.Object("rule", r), zap.Error(err))
367-
}
363+
matched, err := r.Eval(i.Object)
364+
if err != nil {
365+
i.logger.Warnw("Failed to evaluate object filter", zap.Object("rule", r), zap.Error(err))
366+
}
368367

369-
if err != nil || !matched {
370-
continue
371-
}
368+
if err != nil || !matched {
369+
continue
372370
}
373371

374372
i.Rules[r.ID] = struct{}{}
375373
i.logger.Infow("Rule matches", zap.Object("rule", r))
376374

377-
err := i.AddRuleMatched(ctx, tx, r)
375+
err = i.AddRuleMatched(ctx, tx, r)
378376
if err != nil {
379377
i.logger.Errorw("Failed to upsert incident rule", zap.Object("rule", r), zap.Error(err))
380378

@@ -429,27 +427,16 @@ func (i *Incident) evaluateEscalations(eventTime time.Time) ([]*rule.Escalation,
429427
// Check if new escalation stages are reached
430428
for _, escalation := range r.Escalations {
431429
if _, ok := i.EscalationState[escalation.ID]; !ok {
432-
matched := false
433-
434-
if escalation.Condition == nil {
435-
matched = true
430+
matched, err := escalation.Eval(filterContext)
431+
if err != nil {
432+
i.logger.Warnw(
433+
"Failed to evaluate escalation condition", zap.Object("rule", r),
434+
zap.Object("escalation", escalation), zap.Error(err),
435+
)
436+
} else if !matched {
437+
incidentAgeFilter := filterContext.ReevaluateAfter(escalation.Condition)
438+
retryAfter = min(retryAfter, incidentAgeFilter)
436439
} else {
437-
var err error
438-
matched, err = escalation.Condition.Eval(filterContext)
439-
if err != nil {
440-
i.logger.Warnw(
441-
"Failed to evaluate escalation condition", zap.Object("rule", r),
442-
zap.Object("escalation", escalation), zap.Error(err),
443-
)
444-
445-
matched = false
446-
} else if !matched {
447-
incidentAgeFilter := filterContext.ReevaluateAfter(escalation.Condition)
448-
retryAfter = min(retryAfter, incidentAgeFilter)
449-
}
450-
}
451-
452-
if matched {
453440
escalations = append(escalations, escalation)
454441
}
455442
}

internal/rule/escalation.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ func (e *Escalation) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
4141
return nil
4242
}
4343

44+
// Eval evaluates the configured escalation filter for the provided filter.
45+
// Returns always true if there are no configured escalation conditions.
46+
func (e *Escalation) Eval(filterable *EscalationFilter) (bool, error) {
47+
if e.Condition == nil {
48+
return true, nil
49+
}
50+
51+
return e.Condition.Eval(filterable)
52+
}
53+
4454
func (e *Escalation) DisplayName() string {
4555
if e.NameRaw.Valid && e.NameRaw.String != "" {
4656
return e.NameRaw.String

internal/rule/rule.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ func (r *Rule) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
3535
return nil
3636
}
3737

38+
// Eval evaluates the configured object filter for the provided filterable.
39+
// Returns always true if the current rule doesn't have a configured object filter.
40+
func (r *Rule) Eval(filterable filter.Filterable) (bool, error) {
41+
if r.ObjectFilter == nil {
42+
return true, nil
43+
}
44+
45+
return r.ObjectFilter.Eval(filterable)
46+
}
47+
3848
// ContactChannels stores a set of channel IDs for each set of individual contacts.
3949
type ContactChannels map[*recipient.Contact]map[int64]bool
4050

0 commit comments

Comments
 (0)