Skip to content

Commit 9d50a7b

Browse files
committed
Incident.applyMatchingRules: Simplify Source ID comparison
1 parent 4d21ff3 commit 9d50a7b

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

internal/incident/incident.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"slices"
87
"strconv"
98
"sync"
109
"time"
@@ -401,32 +400,27 @@ func (i *Incident) applyMatchingRules(ctx context.Context, tx *sqlx.Tx, ev *even
401400
i.Rules = make(map[int64]struct{})
402401
}
403402

404-
sourceRulesInfo, ok := i.runtimeConfig.RulesBySource[ev.SourceId]
405-
if !ok {
406-
i.logger.Errorw("Event refers to non-existing source", zap.Int64("source_id", ev.SourceId))
407-
return fmt.Errorf("cannot lookup event source %d", ev.SourceId)
408-
}
409-
410403
for _, ruleId := range ev.RuleIds {
411404
ruleIdInt, err := strconv.ParseInt(ruleId, 10, 64)
412405
if err != nil {
413406
i.logger.Errorw("Event rule is not an integer", zap.String("rule_id", ruleId), zap.Error(err))
414407
return fmt.Errorf("cannot convert rule id %q to an int: %w", ruleId, err)
415408
}
416409

417-
if !slices.Contains(sourceRulesInfo.RuleIDs, ruleIdInt) {
418-
i.logger.Errorw("Event rule does not belong to source",
419-
zap.Int64("source_id", ev.SourceId),
420-
zap.Int64("rule_id", ruleIdInt))
421-
return fmt.Errorf("event rule %d does not belong to source %d", ruleIdInt, ev.SourceId)
422-
}
423-
424410
r, ok := i.runtimeConfig.Rules[ruleIdInt]
425411
if !ok {
426412
i.logger.Errorw("Event refers to non-existing event rule, might got deleted", zap.Int64("rule_id", ruleIdInt))
427413
return fmt.Errorf("cannot apply unknown rule %d", ruleIdInt)
428414
}
429415

416+
if r.SourceID != ev.SourceId {
417+
i.logger.Errorw("Rule source ID does not match event source ID",
418+
zap.Int64("event_source_id", ev.SourceId),
419+
zap.Int64("rule_source_id", r.SourceID),
420+
zap.Int64("rule_id", ruleIdInt))
421+
return fmt.Errorf("rule %d source ID %d does not match event source %d", ruleIdInt, r.SourceID, ev.SourceId)
422+
}
423+
430424
if _, ok := i.Rules[r.ID]; !ok {
431425
i.Rules[r.ID] = struct{}{}
432426
i.logger.Infow("Rule matches", zap.Object("rule", r))

0 commit comments

Comments
 (0)