Skip to content

Commit

Permalink
Introduce Rule#Eval() & Escalation#Eval() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab authored and julianbrost committed Jun 5, 2024
1 parent 29eb3f3 commit 62e47fb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 29 deletions.
45 changes: 16 additions & 29 deletions internal/incident/incident.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,21 +360,19 @@ func (i *Incident) evaluateRules(ctx context.Context, tx *sqlx.Tx, eventID int64
}

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

if err != nil || !matched {
continue
}
if err != nil || !matched {
continue
}

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

err := i.AddRuleMatched(ctx, tx, r)
err = i.AddRuleMatched(ctx, tx, r)
if err != nil {
i.logger.Errorw("Failed to upsert incident rule", zap.Object("rule", r), zap.Error(err))

Expand Down Expand Up @@ -429,27 +427,16 @@ func (i *Incident) evaluateEscalations(eventTime time.Time) ([]*rule.Escalation,
// Check if new escalation stages are reached
for _, escalation := range r.Escalations {
if _, ok := i.EscalationState[escalation.ID]; !ok {
matched := false

if escalation.Condition == nil {
matched = true
matched, err := escalation.Eval(filterContext)
if err != nil {
i.logger.Warnw(
"Failed to evaluate escalation condition", zap.Object("rule", r),
zap.Object("escalation", escalation), zap.Error(err),
)
} else if !matched {
incidentAgeFilter := filterContext.ReevaluateAfter(escalation.Condition)
retryAfter = min(retryAfter, incidentAgeFilter)
} else {
var err error
matched, err = escalation.Condition.Eval(filterContext)
if err != nil {
i.logger.Warnw(
"Failed to evaluate escalation condition", zap.Object("rule", r),
zap.Object("escalation", escalation), zap.Error(err),
)

matched = false
} else if !matched {
incidentAgeFilter := filterContext.ReevaluateAfter(escalation.Condition)
retryAfter = min(retryAfter, incidentAgeFilter)
}
}

if matched {
escalations = append(escalations, escalation)
}
}
Expand Down
10 changes: 10 additions & 0 deletions internal/rule/escalation.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ func (e *Escalation) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
return nil
}

// Eval evaluates the configured escalation filter for the provided filter.
// Returns always true if there are no configured escalation conditions.
func (e *Escalation) Eval(filterable *EscalationFilter) (bool, error) {
if e.Condition == nil {
return true, nil
}

return e.Condition.Eval(filterable)
}

func (e *Escalation) DisplayName() string {
if e.NameRaw.Valid && e.NameRaw.String != "" {
return e.NameRaw.String
Expand Down
10 changes: 10 additions & 0 deletions internal/rule/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ func (r *Rule) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
return nil
}

// Eval evaluates the configured object filter for the provided filterable.
// Returns always true if the current rule doesn't have a configured object filter.
func (r *Rule) Eval(filterable filter.Filterable) (bool, error) {
if r.ObjectFilter == nil {
return true, nil
}

return r.ObjectFilter.Eval(filterable)
}

// ContactChannels stores a set of channel IDs for each set of individual contacts.
type ContactChannels map[*recipient.Contact]map[int64]bool

Expand Down

0 comments on commit 62e47fb

Please sign in to comment.