-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
api/rules: Add filtering on rule name/group/file #7560
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,7 +70,9 @@ func (rr *GRPCClient) Rules(ctx context.Context, req *rulespb.RulesRequest) (*ru | |
} | ||
} | ||
|
||
resp.groups = filterRules(resp.groups, matcherSets) | ||
resp.groups = filterRulesByMatchers(resp.groups, matcherSets) | ||
resp.groups = filterRulesByNamesAndFile(resp.groups, req.RuleName, req.RuleGroup, req.File) | ||
|
||
// TODO(bwplotka): Move to SortInterface with equal method and heap. | ||
resp.groups = dedupGroups(resp.groups) | ||
for _, g := range resp.groups { | ||
|
@@ -80,8 +82,57 @@ func (rr *GRPCClient) Rules(ctx context.Context, req *rulespb.RulesRequest) (*ru | |
return &rulespb.RuleGroups{Groups: resp.groups}, resp.warnings, nil | ||
} | ||
|
||
// filterRules filters rules in a group according to given matcherSets. | ||
func filterRules(ruleGroups []*rulespb.RuleGroup, matcherSets [][]*labels.Matcher) []*rulespb.RuleGroup { | ||
// filters rules by group name, rule name or file. | ||
func filterRulesByNamesAndFile(ruleGroups []*rulespb.RuleGroup, ruleName []string, ruleGroup []string, file []string) []*rulespb.RuleGroup { | ||
if len(ruleName) == 0 && len(ruleGroup) == 0 && len(file) == 0 { | ||
return ruleGroups | ||
} | ||
|
||
queryFormToSet := func(values []string) map[string]struct{} { | ||
set := make(map[string]struct{}, len(values)) | ||
for _, v := range values { | ||
set[v] = struct{}{} | ||
} | ||
return set | ||
} | ||
|
||
rnSet := queryFormToSet(ruleName) | ||
rgSet := queryFormToSet(ruleGroup) | ||
fSet := queryFormToSet(file) | ||
|
||
rgs := make([]*rulespb.RuleGroup, 0, len(ruleGroups)) | ||
for _, grp := range ruleGroups { | ||
if len(rgSet) > 0 { | ||
if _, ok := rgSet[grp.Name]; !ok { | ||
continue | ||
} | ||
} | ||
|
||
if len(fSet) > 0 { | ||
if _, ok := fSet[grp.File]; !ok { | ||
continue | ||
} | ||
} | ||
|
||
if len(rnSet) > 0 { | ||
ruleCount := 0 | ||
for _, r := range grp.Rules { | ||
if _, ok := rnSet[r.GetName()]; ok { | ||
grp.Rules[ruleCount] = r | ||
ruleCount++ | ||
} | ||
} | ||
grp.Rules = grp.Rules[:ruleCount] | ||
} | ||
if len(grp.Rules) > 0 { | ||
rgs = append(rgs, grp) | ||
} | ||
} | ||
return rgs | ||
} | ||
|
||
// filterRulesbyMatchers filters rules in a group according to given matcherSets. | ||
func filterRulesByMatchers(ruleGroups []*rulespb.RuleGroup, matcherSets [][]*labels.Matcher) []*rulespb.RuleGroup { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we also need to update this function now to better mimic prometheus/prometheus#10194? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, most likely. We also need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure |
||
if len(matcherSets) == 0 { | ||
return ruleGroups | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think just using
slices.Contains
instead of set might be more memory intensive?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason I did it like this, was simply because that's how the code looks like in Prometheus. I am not sure if there are significant performance benefits either way, although perhaps the code is a bit cleaner using
slices.Contains
, so happy to change to that approach if you think it's best?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see, fine if it mirrors prometheus then