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

api/rules: Add filtering on rule name/group/file #7560

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re

### Added

- [#7560](https://github.com/thanos-io/thanos/pull/7560) Query: Added the possibility of filtering rules by rule_name, rule_group or file to HTTP api.

### Changed

- [#7494](https://github.com/thanos-io/thanos/pull/7494) Ruler: remove trailing period from SRV records returned by discovery `dnsnosrva` lookups
Expand Down
6 changes: 6 additions & 0 deletions pkg/api/query/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ const (
LookbackDeltaParam = "lookback_delta"
EngineParam = "engine"
QueryAnalyzeParam = "analyze"
RuleNameParam = "rule_name[]"
RuleGroupParam = "rule_group[]"
FileParam = "file[]"
)

type PromqlEngineType string
Expand Down Expand Up @@ -1397,6 +1400,9 @@ func NewRulesHandler(client rules.UnaryClient, enablePartialResponse bool) func(
Type: rulespb.RulesRequest_Type(typ),
PartialResponseStrategy: ps,
MatcherString: r.Form[MatcherParam],
RuleName: r.Form[RuleNameParam],
RuleGroup: r.Form[RuleGroupParam],
File: r.Form[FileParam],
}
tracing.DoInSpan(ctx, "retrieve_rules", func(ctx context.Context) {
groups, warnings, err = client.Rules(ctx, req)
Expand Down
57 changes: 54 additions & 3 deletions pkg/rules/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Comment on lines +99 to +101
Copy link
Member

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?

Copy link
Contributor Author

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?

Copy link
Member

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


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 {
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, most likely. We also need exclude_alerts to be fully aligned with the Prometheus API. Could we deal with those in separate PRs perhaps?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

if len(matcherSets) == 0 {
return ruleGroups
}
Expand Down
Loading
Loading