Skip to content

Add support for Limit field on RuleGroup which got introduced in https://github.com/prometheus/prometheus/pull/9260 #5528

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

Merged
merged 1 commit into from
Aug 23, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## master / unreleased
* [FEATURE] Ruler: Add support for Limit field on RuleGroup. #5528
* [FEATURE] AlertManager: Add support for Webex, Discord and Telegram Receiver. #5493
* [FEATURE] Ingester: added `-admin-limit-message` to customize the message contained in limit errors.#5460
* [CHANGE] AlertManager: include reason label in cortex_alertmanager_notifications_failed_total.#5409
Expand Down
2 changes: 2 additions & 0 deletions pkg/ruler/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type RuleGroup struct {
Interval float64 `json:"interval"`
LastEvaluation time.Time `json:"lastEvaluation"`
EvaluationTime float64 `json:"evaluationTime"`
Limit int64 `json:"limit"`
}

type rule interface{}
Expand Down Expand Up @@ -203,6 +204,7 @@ func (a *API) PrometheusRules(w http.ResponseWriter, req *http.Request) {
Interval: g.Group.Interval.Seconds(),
LastEvaluation: g.GetEvaluationTimestamp(),
EvaluationTime: g.GetEvaluationDuration().Seconds(),
Limit: g.Group.Limit,
}

for i, rl := range g.ActiveRules {
Expand Down
55 changes: 54 additions & 1 deletion pkg/ruler/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,63 @@ func TestRuler_rules_special_characters(t *testing.T) {
},
},
})

require.Equal(t, string(expectedResponse), string(body))
}

func TestRuler_rules_limit(t *testing.T) {
store := newMockRuleStore(mockRulesLimit)
cfg := defaultRulerConfig(t)

r := newTestRuler(t, cfg, store, nil)
defer services.StopAndAwaitTerminated(context.Background(), r) //nolint:errcheck

a := NewAPI(r, r.store, log.NewNopLogger())

req := requestFor(t, http.MethodGet, "https://localhost:8080/api/prom/api/v1/rules", nil, "user1")
w := httptest.NewRecorder()
a.PrometheusRules(w, req)

resp := w.Result()
body, _ := io.ReadAll(resp.Body)
// Check status code and status response
responseJSON := response{}
err := json.Unmarshal(body, &responseJSON)
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
require.Equal(t, responseJSON.Status, "success")

// Testing the running rules for user1 in the mock store
expectedResponse, _ := json.Marshal(response{
Status: "success",
Data: &RuleDiscovery{
RuleGroups: []*RuleGroup{
{
Name: "group1",
File: "namespace1",
Limit: 5,
Rules: []rule{
&recordingRule{
Name: "UP_RULE",
Query: "up",
Health: "unknown",
Type: "recording",
},
&alertingRule{
Name: "UP_ALERT",
Query: "up < 1",
State: "inactive",
Health: "unknown",
Type: "alerting",
Alerts: []*Alert{},
},
},
Interval: 60,
},
},
},
})
require.Equal(t, string(expectedResponse), string(body))
}
func TestRuler_alerts(t *testing.T) {
store := newMockRuleStore(mockRules)
cfg := defaultRulerConfig(t)
Expand Down
1 change: 1 addition & 0 deletions pkg/ruler/ruler.go
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,7 @@ func (r *Ruler) getLocalRules(userID string, rulesRequest RulesRequest) ([]*Grou
Namespace: string(decodedNamespace),
Interval: interval,
User: userID,
Limit: int64(group.Limit()),
},

EvaluationTimestamp: group.GetLastEvaluation(),
Expand Down
2 changes: 2 additions & 0 deletions pkg/ruler/rulespb/compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func ToProto(user string, namespace string, rl rulefmt.RuleGroup) *RuleGroupDesc
Interval: time.Duration(rl.Interval),
Rules: formattedRuleToProto(rl.Rules),
User: user,
Limit: int64(rl.Limit),
}
return &rg
}
Expand All @@ -45,6 +46,7 @@ func FromProto(rg *RuleGroupDesc) rulefmt.RuleGroup {
Name: rg.GetName(),
Interval: model.Duration(rg.Interval),
Rules: make([]rulefmt.RuleNode, len(rg.GetRules())),
Limit: int(rg.Limit),
}

for i, rl := range rg.GetRules() {
Expand Down
107 changes: 74 additions & 33 deletions pkg/ruler/rulespb/rules.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/ruler/rulespb/rules.proto
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ message RuleGroupDesc {
// to create custom `ManagerOpts` based on rule configs which can then be passed
// to the Prometheus Manager.
repeated google.protobuf.Any options = 9;
int64 limit =10;
}

// RuleDesc is a proto representation of a Prometheus Rule
Expand Down
21 changes: 21 additions & 0 deletions pkg/ruler/store_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,27 @@ var (
},
},
}
mockRulesLimit = map[string]rulespb.RuleGroupList{
"user1": {
&rulespb.RuleGroupDesc{
Name: "group1",
Namespace: "namespace1",
User: "user1",
Limit: 5,
Rules: []*rulespb.RuleDesc{
{
Record: "UP_RULE",
Expr: "up",
},
{
Alert: "UP_ALERT",
Expr: "up < 1",
},
},
Interval: interval,
},
},
}
)

func newMockRuleStore(rules map[string]rulespb.RuleGroupList) *mockRuleStore {
Expand Down