Skip to content

Commit

Permalink
api/rules: Add filtering on rule name/group/file
Browse files Browse the repository at this point in the history
This commits adds the option of filtering rules by rule name, rule
group, or file. This brings the rule API closer in-line with the current
Prometheus api.

Signed-off-by: Jacob Baungard Hansen <jacobbaungard@redhat.com>
  • Loading branch information
jacobbaungard committed Jul 19, 2024
1 parent 35c0dbe commit 6ce555d
Show file tree
Hide file tree
Showing 6 changed files with 585 additions and 75 deletions.
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)

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 {
if len(matcherSets) == 0 {
return ruleGroups
}
Expand Down
Loading

0 comments on commit 6ce555d

Please sign in to comment.