Skip to content

Commit 2639a98

Browse files
committed
Reduce allocs in config
1 parent bab341a commit 2639a98

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

internal/config/rule.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -148,30 +148,37 @@ func (rule Rule) validate() (err error) {
148148
return nil
149149
}
150150

151-
func isDisabledForRule(rule parser.Rule, name string, check checks.RuleChecker, promTags []string) bool {
152-
matches := make([]string, 0, len(promTags)+2)
153-
matches = append(matches, name)
154-
matches = append(matches, check.String())
155-
for _, tag := range promTags {
156-
matches = append(matches, name+"(+"+tag+")")
151+
func matchesTag(comment, name string, tags []string) bool {
152+
// control comment doesn't start with name so it cannot match it.
153+
if !strings.HasPrefix(comment, name) {
154+
return false
157155
}
158-
for _, disable := range comments.Only[comments.Disable](rule.Comments, comments.DisableType) {
159-
for _, match := range matches {
160-
if match == disable.Match {
161-
slog.LogAttrs(context.Background(), slog.LevelDebug,
162-
"Check disabled by comment",
163-
slog.String("check", check.String()),
164-
slog.String("match", match),
165-
)
156+
for _, tag := range tags {
157+
if strings.Contains(comment, tag) {
158+
if comment == name+"(+"+tag+")" {
166159
return true
167160
}
168161
}
169162
}
163+
return false
164+
}
165+
166+
func isDisabledForRule(rule parser.Rule, name string, check checks.RuleChecker, promTags []string) bool {
167+
for _, disable := range comments.Only[comments.Disable](rule.Comments, comments.DisableType) {
168+
if disable.Match == name || disable.Match == check.String() || matchesTag(disable.Match, name, promTags) {
169+
slog.LogAttrs(context.Background(), slog.LevelDebug,
170+
"Check disabled by comment",
171+
slog.String("check", check.String()),
172+
slog.String("match", disable.Match),
173+
)
174+
return true
175+
}
176+
}
170177
for _, snooze := range comments.Only[comments.Snooze](rule.Comments, comments.SnoozeType) {
171178
if !snooze.Until.After(time.Now()) {
172179
continue
173180
}
174-
if slices.Contains(matches, snooze.Match) {
181+
if snooze.Match == name || snooze.Match == check.String() || matchesTag(snooze.Match, name, promTags) {
175182
slog.LogAttrs(context.Background(), slog.LevelDebug,
176183
"Check snoozed by comment",
177184
slog.String("check", check.String()),

0 commit comments

Comments
 (0)