Skip to content

Commit 98a8dec

Browse files
Add support for Limit field on RuleGroup which got introduced in prometheus/prometheus#9260 (#5528)
Signed-off-by: Krishna Teja Puttagunta <[email protected]>
1 parent ccd25f2 commit 98a8dec

File tree

8 files changed

+156
-34
lines changed

8 files changed

+156
-34
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Changelog
22

33
## master / unreleased
4+
* [FEATURE] Ruler: Add support for Limit field on RuleGroup. #5528
45
* [FEATURE] AlertManager: Add support for Webex, Discord and Telegram Receiver. #5493
56
* [FEATURE] Ingester: added `-admin-limit-message` to customize the message contained in limit errors.#5460
67
* [CHANGE] AlertManager: include reason label in cortex_alertmanager_notifications_failed_total.#5409

pkg/ruler/api.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ type RuleGroup struct {
6969
Interval float64 `json:"interval"`
7070
LastEvaluation time.Time `json:"lastEvaluation"`
7171
EvaluationTime float64 `json:"evaluationTime"`
72+
Limit int64 `json:"limit"`
7273
}
7374

7475
type rule interface{}
@@ -203,6 +204,7 @@ func (a *API) PrometheusRules(w http.ResponseWriter, req *http.Request) {
203204
Interval: g.Group.Interval.Seconds(),
204205
LastEvaluation: g.GetEvaluationTimestamp(),
205206
EvaluationTime: g.GetEvaluationDuration().Seconds(),
207+
Limit: g.Group.Limit,
206208
}
207209

208210
for i, rl := range g.ActiveRules {

pkg/ruler/api_test.go

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,63 @@ func TestRuler_rules_special_characters(t *testing.T) {
127127
},
128128
},
129129
})
130-
131130
require.Equal(t, string(expectedResponse), string(body))
132131
}
133132

133+
func TestRuler_rules_limit(t *testing.T) {
134+
store := newMockRuleStore(mockRulesLimit)
135+
cfg := defaultRulerConfig(t)
136+
137+
r := newTestRuler(t, cfg, store, nil)
138+
defer services.StopAndAwaitTerminated(context.Background(), r) //nolint:errcheck
139+
140+
a := NewAPI(r, r.store, log.NewNopLogger())
141+
142+
req := requestFor(t, http.MethodGet, "https://localhost:8080/api/prom/api/v1/rules", nil, "user1")
143+
w := httptest.NewRecorder()
144+
a.PrometheusRules(w, req)
145+
146+
resp := w.Result()
147+
body, _ := io.ReadAll(resp.Body)
148+
// Check status code and status response
149+
responseJSON := response{}
150+
err := json.Unmarshal(body, &responseJSON)
151+
require.NoError(t, err)
152+
require.Equal(t, http.StatusOK, resp.StatusCode)
153+
require.Equal(t, responseJSON.Status, "success")
154+
155+
// Testing the running rules for user1 in the mock store
156+
expectedResponse, _ := json.Marshal(response{
157+
Status: "success",
158+
Data: &RuleDiscovery{
159+
RuleGroups: []*RuleGroup{
160+
{
161+
Name: "group1",
162+
File: "namespace1",
163+
Limit: 5,
164+
Rules: []rule{
165+
&recordingRule{
166+
Name: "UP_RULE",
167+
Query: "up",
168+
Health: "unknown",
169+
Type: "recording",
170+
},
171+
&alertingRule{
172+
Name: "UP_ALERT",
173+
Query: "up < 1",
174+
State: "inactive",
175+
Health: "unknown",
176+
Type: "alerting",
177+
Alerts: []*Alert{},
178+
},
179+
},
180+
Interval: 60,
181+
},
182+
},
183+
},
184+
})
185+
require.Equal(t, string(expectedResponse), string(body))
186+
}
134187
func TestRuler_alerts(t *testing.T) {
135188
store := newMockRuleStore(mockRules)
136189
cfg := defaultRulerConfig(t)

pkg/ruler/ruler.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,7 @@ func (r *Ruler) getLocalRules(userID string, rulesRequest RulesRequest) ([]*Grou
708708
Namespace: string(decodedNamespace),
709709
Interval: interval,
710710
User: userID,
711+
Limit: int64(group.Limit()),
711712
},
712713

713714
EvaluationTimestamp: group.GetLastEvaluation(),

pkg/ruler/rulespb/compat.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func ToProto(user string, namespace string, rl rulefmt.RuleGroup) *RuleGroupDesc
1919
Interval: time.Duration(rl.Interval),
2020
Rules: formattedRuleToProto(rl.Rules),
2121
User: user,
22+
Limit: int64(rl.Limit),
2223
}
2324
return &rg
2425
}
@@ -45,6 +46,7 @@ func FromProto(rg *RuleGroupDesc) rulefmt.RuleGroup {
4546
Name: rg.GetName(),
4647
Interval: model.Duration(rg.Interval),
4748
Rules: make([]rulefmt.RuleNode, len(rg.GetRules())),
49+
Limit: int(rg.Limit),
4850
}
4951

5052
for i, rl := range rg.GetRules() {

pkg/ruler/rulespb/rules.pb.go

Lines changed: 74 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/ruler/rulespb/rules.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ message RuleGroupDesc {
2727
// to create custom `ManagerOpts` based on rule configs which can then be passed
2828
// to the Prometheus Manager.
2929
repeated google.protobuf.Any options = 9;
30+
int64 limit =10;
3031
}
3132

3233
// RuleDesc is a proto representation of a Prometheus Rule

pkg/ruler/store_mock_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,27 @@ var (
110110
},
111111
},
112112
}
113+
mockRulesLimit = map[string]rulespb.RuleGroupList{
114+
"user1": {
115+
&rulespb.RuleGroupDesc{
116+
Name: "group1",
117+
Namespace: "namespace1",
118+
User: "user1",
119+
Limit: 5,
120+
Rules: []*rulespb.RuleDesc{
121+
{
122+
Record: "UP_RULE",
123+
Expr: "up",
124+
},
125+
{
126+
Alert: "UP_ALERT",
127+
Expr: "up < 1",
128+
},
129+
},
130+
Interval: interval,
131+
},
132+
},
133+
}
113134
)
114135

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

0 commit comments

Comments
 (0)