Skip to content

Commit 4ed4a6b

Browse files
Adding support to filter rules
Signed-off-by: Anand Rajagopal <anrajag@amazon.com>
1 parent feb51b0 commit 4ed4a6b

File tree

5 files changed

+35
-10
lines changed

5 files changed

+35
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* [ENHANCEMENT] Store Gateway: Add new metrics `cortex_bucket_store_sent_chunk_size_bytes`, `cortex_bucket_store_postings_size_bytes` and `cortex_bucket_store_empty_postings_total`. #5397
3333
* [ENHANCEMENT] Add jitter to lifecycler heartbeat. #5404
3434
* [ENHANCEMENT] Store Gateway: Add config `estimated_max_series_size_bytes` and `estimated_max_chunk_size_bytes` to address data overfetch. #5401
35+
* [ENHANCEMENT] Ruler: Support for filtering rules. #5417
3536
* [BUGFIX] Ruler: Validate if rule group can be safely converted back to rule group yaml from protobuf message #5265
3637
* [BUGFIX] Querier: Convert gRPC `ResourceExhausted` status code from store gateway to 422 limit error. #5286
3738
* [BUGFIX] Alertmanager: Route web-ui requests to the alertmanager distributor when sharding is enabled. #5293

docs/contributing/how-integration-tests-work.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ This will locally build the `quay.io/cortexproject/cortex:latest` image used by
2020
Once the Docker image is built, you can run integration tests:
2121

2222
```
23-
go test -v -tags=requires_docker ./integration/...
23+
go test -v -tags=integration,requires_docker,integration_alertmanager,integration_memberlist,integration_querier,integration_ruler,integration_query_fuzz ./integration/...
2424
```
2525

26-
If you want to run a single test you can use a filter. For example, to only run `TestChunksStorageAllIndexBackends`:
26+
If you want to run a single test you can use a filter. For example, to only run `TestRulerMetricsWhenIngesterFails`:
2727

2828
```
29-
go test -v -tags=requires_docker ./integration -run "^TestChunksStorageAllIndexBackends$"
29+
go test -v -tags=integration,requires_docker,integration_ruler ./integration/ -run "^TestRulerMetricsWhenIngesterFails$" -count=1
3030
```
3131

3232
### Supported environment variables

pkg/ruler/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ func (a *API) PrometheusAlerts(w http.ResponseWriter, req *http.Request) {
284284

285285
w.Header().Set("Content-Type", "application/json")
286286
rulesRequest := RulesRequest{
287-
//Type: AlertingRuleFilter,
287+
Type: AlertingRuleFilter,
288288
}
289289
rgs, err := a.ruler.GetRules(req.Context(), rulesRequest)
290290

pkg/ruler/ruler.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ func (r *Ruler) getLocalRules(userID string, rulesRequest RulesRequest) ([]*Grou
726726
switch rule := r.(type) {
727727
case *promRules.AlertingRule:
728728
if !returnAlerts {
729-
break
729+
continue
730730
}
731731
alerts := []*AlertStateDesc{}
732732
for _, a := range rule.ActiveAlerts() {
@@ -760,7 +760,7 @@ func (r *Ruler) getLocalRules(userID string, rulesRequest RulesRequest) ([]*Grou
760760
}
761761
case *promRules.RecordingRule:
762762
if !returnRecording {
763-
break
763+
continue
764764
}
765765
ruleDesc = &RuleStateDesc{
766766
Rule: &rulespb.RuleDesc{
@@ -776,9 +776,7 @@ func (r *Ruler) getLocalRules(userID string, rulesRequest RulesRequest) ([]*Grou
776776
default:
777777
return nil, errors.Errorf("failed to assert type of rule '%v'", rule.Name())
778778
}
779-
if ruleDesc != nil {
780-
groupDesc.ActiveRules = append(groupDesc.ActiveRules, ruleDesc)
781-
}
779+
groupDesc.ActiveRules = append(groupDesc.ActiveRules, ruleDesc)
782780
}
783781
if len(groupDesc.ActiveRules) > 0 {
784782
groupDescs = append(groupDescs, groupDesc)

pkg/ruler/ruler_test.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,6 @@ func compareRuleGroupDescToStateDesc(t *testing.T, expected *rulespb.RuleGroupDe
325325
func TestGetRules(t *testing.T) {
326326
// ruler ID -> (user ID -> list of groups).
327327
type expectedRulesMap map[string]map[string]rulespb.RuleGroupList
328-
type ruleExecutionMap map[string]map[string]rulespb.RuleGroupList
329328
type rulesMap map[string][]*rulespb.RuleDesc
330329

331330
type testCase struct {
@@ -486,6 +485,33 @@ func TestGetRules(t *testing.T) {
486485
"user3": 1,
487486
},
488487
},
488+
"Shuffle Sharding and ShardSize = 2 and Rule Group Name Filter": {
489+
sharding: true,
490+
shuffleShardSize: 2,
491+
shardingStrategy: util.ShardingStrategyShuffle,
492+
rulesRequest: RulesRequest{
493+
RuleGroupNames: []string{"third"},
494+
},
495+
expectedCount: map[string]int{
496+
"user1": 2,
497+
"user2": 1,
498+
"user3": 2,
499+
},
500+
},
501+
"Shuffle Sharding and ShardSize = 2 and Rule Group Name and Rule Type Filter": {
502+
sharding: true,
503+
shuffleShardSize: 2,
504+
shardingStrategy: util.ShardingStrategyShuffle,
505+
rulesRequest: RulesRequest{
506+
RuleGroupNames: []string{"second", "third"},
507+
Type: RecordingRuleFilter,
508+
},
509+
expectedCount: map[string]int{
510+
"user1": 2,
511+
"user2": 2,
512+
"user3": 1,
513+
},
514+
},
489515
}
490516

491517
for name, tc := range testCases {

0 commit comments

Comments
 (0)