Skip to content

Commit

Permalink
wip add flag --label-excluded-rule-groups-regex
Browse files Browse the repository at this point in the history
  • Loading branch information
the-it committed Jun 19, 2024
1 parent 43f2e9b commit f8934e0
Showing 1 changed file with 21 additions and 5 deletions.
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

0 comments on commit f8934e0

Please sign in to comment.