Skip to content

Commit

Permalink
Support rule filter query params as in vanilla Prometheus
Browse files Browse the repository at this point in the history
  • Loading branch information
fayzal-g committed Feb 5, 2025
1 parent d0ed3eb commit cba19cb
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pkg/ruler/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,19 @@ func (a *API) PrometheusRules(w http.ResponseWriter, req *http.Request) {
MaxGroups: maxGroups,
}

// The file, rule_group and rule_name query parameters differ
// from Vanilla prometheus: file[], rule_group[], rule_name[]
// If they are provided in this format, use them instead.
if req.URL.Query().Has("rule_name[]") {
rulesReq.RuleName = req.URL.Query()["rule_name[]"]
}
if req.URL.Query().Has("rule_group[]") {
rulesReq.RuleGroup = req.URL.Query()["rule_group[]"]
}
if req.URL.Query().Has("file[]") {
rulesReq.File = req.URL.Query()["file[]"]
}

ruleTypeFilter := strings.ToLower(req.URL.Query().Get("type"))
if ruleTypeFilter != "" {
switch ruleTypeFilter {
Expand Down
66 changes: 66 additions & 0 deletions pkg/ruler/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,72 @@ func TestRuler_PrometheusRules(t *testing.T) {
},
},
},
"when filtering using query parameters from vanilla Prometheus": {
configuredRules: makeFilterTestRules(),
expectedConfigured: len(makeFilterTestRules()),
queryParams: "?" + url.Values{
"file[]": []string{namespaceName(3)},
"rule_group[]": []string{groupName(2)},
}.Encode(),
limits: validation.MockDefaultOverrides(),
expectedRules: []*RuleGroup{
{
Name: groupName(2),
File: namespaceName(3),
Rules: []rule{
&recordingRule{
Name: "NonUniqueNamedRule",
Query: "up",
Health: "unknown",
Type: "recording",
},
&alertingRule{
Name: "UniqueNamedRuleN3G2",
Query: "up < 1",
State: "inactive",
Health: "unknown",
Type: "alerting",
Alerts: []*Alert{},
},
},
Interval: 60,
},
},
},
"when supplying filter params in both formats, vanilla Prometheus format takes precedent": {
configuredRules: makeFilterTestRules(),
expectedConfigured: len(makeFilterTestRules()),
queryParams: "?" + url.Values{
"file:": []string{"foo"},
"rule_group": []string{"bar"},
"file[]": []string{namespaceName(3)},
"rule_group[]": []string{groupName(2)},
}.Encode(),
limits: validation.MockDefaultOverrides(),
expectedRules: []*RuleGroup{
{
Name: groupName(2),
File: namespaceName(3),
Rules: []rule{
&recordingRule{
Name: "NonUniqueNamedRule",
Query: "up",
Health: "unknown",
Type: "recording",
},
&alertingRule{
Name: "UniqueNamedRuleN3G2",
Query: "up < 1",
State: "inactive",
Health: "unknown",
Type: "alerting",
Alerts: []*Alert{},
},
},
Interval: 60,
},
},
},
}

for name, tc := range testCases {
Expand Down

0 comments on commit cba19cb

Please sign in to comment.