Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip add flag --label-excluded-rule-groups-regex #8429

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions pkg/mimirtool/commands/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,12 @@ type RuleCommand struct {
SyncConcurrency int

// Prepare Rules Config
InPlaceEdit bool
AggregationLabel string
AggregationLabelExcludedRuleGroups string
aggregationLabelExcludedRuleGroupsList map[string]struct{}
InPlaceEdit bool
AggregationLabel string
AggregationLabelExcludedRuleGroups string
AggregationLabelExcludedRuleGroupsRegex string
aggregationLabelExcludedRuleGroupsList map[string]struct{}
aggregationLabelExcludedRuleGroupsRegex *regexp.Regexp

// Lint Rules Config
LintDryRun bool
Expand Down Expand Up @@ -265,6 +267,7 @@ func (r *RuleCommand) Register(app *kingpin.Application, envVars EnvVarNames, re
).Short('i').BoolVar(&r.InPlaceEdit)
prepareCmd.Flag("label", "label to include as part of the aggregations.").Default(defaultPrepareAggregationLabel).Short('l').StringVar(&r.AggregationLabel)
prepareCmd.Flag("label-excluded-rule-groups", "Comma separated list of rule group names to exclude when including the configured label to aggregations.").StringVar(&r.AggregationLabelExcludedRuleGroups)
prepareCmd.Flag("label-excluded-rule-groups-regex", "Comma separated list of regexes for rule group names to exclude when including the configured label to aggregations.").StringVar(&r.AggregationLabelExcludedRuleGroupsRegex)

// Lint Command
lintCmd.Arg("rule-files", "The rule files to check.").ExistingFilesVar(&r.RuleFilesList)
Expand Down Expand Up @@ -349,6 +352,13 @@ func (r *RuleCommand) setupArgs() error {
}
}

aggregationLabelExcludedRuleGroupsRegex, err := regexp.Compile(fmt.Sprintf("(%s)", strings.ReplaceAll(r.AggregationLabelExcludedRuleGroupsRegex, ",", "|")))
if err != nil {
return errors.New("invalid regex for aggregation label excluded rule groups, provided by the flag --label-excluded-rule-groups-regex")
}
r.aggregationLabelExcludedRuleGroupsRegex = aggregationLabelExcludedRuleGroupsRegex


// TODO: Remove statement in Mimir 2.14.
if r.RuleFiles != "" {
log.Warn("flag --rule-files is deprecated, use the argument instead")
Expand Down Expand Up @@ -741,7 +751,13 @@ func (r *RuleCommand) prepare(_ *kingpin.ParseContext) error {
// Do not apply the aggregation label to excluded rule groups.
applyTo := func(group rwrulefmt.RuleGroup, _ rulefmt.RuleNode) bool {
_, excluded := r.aggregationLabelExcludedRuleGroupsList[group.Name]
return !excluded
if excluded {
return false
}
if r.aggregationLabelExcludedRuleGroupsRegex.MatchString(group.Name) {
return false
}
return true
}

var count, mod int
Expand Down
Loading