@@ -645,26 +645,53 @@ func filterRuleGroups(userID string, ruleGroups []*rulespb.RuleGroupDesc, ring r
645
645
646
646
// GetRules retrieves the running rules from this ruler and all running rulers in the ring if
647
647
// sharding is enabled
648
- func (r * Ruler ) GetRules (ctx context.Context ) ([]* GroupStateDesc , error ) {
648
+ func (r * Ruler ) GetRules (ctx context.Context , rulesRequest RulesRequest ) ([]* GroupStateDesc , error ) {
649
649
userID , err := tenant .TenantID (ctx )
650
650
if err != nil {
651
651
return nil , fmt .Errorf ("no user id found in context" )
652
652
}
653
653
654
654
if r .cfg .EnableSharding {
655
- return r .getShardedRules (ctx , userID )
655
+ return r .getShardedRules (ctx , userID , rulesRequest )
656
656
}
657
657
658
- return r .getLocalRules (userID )
658
+ return r .getLocalRules (userID , rulesRequest )
659
659
}
660
660
661
- func (r * Ruler ) getLocalRules (userID string ) ([]* GroupStateDesc , error ) {
661
+ func (r * Ruler ) getLocalRules (userID string , rulesRequest RulesRequest ) ([]* GroupStateDesc , error ) {
662
662
groups := r .manager .GetRules (userID )
663
663
664
664
groupDescs := make ([]* GroupStateDesc , 0 , len (groups ))
665
665
prefix := filepath .Join (r .cfg .RulePath , userID ) + "/"
666
666
667
+ sliceToSet := func (values []string ) map [string ]struct {} {
668
+ set := make (map [string ]struct {}, len (values ))
669
+ for _ , v := range values {
670
+ set [v ] = struct {}{}
671
+ }
672
+ return set
673
+ }
674
+
675
+ ruleNameSet := sliceToSet (rulesRequest .RuleNames )
676
+ ruleGroupNameSet := sliceToSet (rulesRequest .RuleGroupNames )
677
+ fileSet := sliceToSet (rulesRequest .Files )
678
+ ruleType := rulesRequest .Type
679
+
680
+ returnAlerts := ruleType == "" || ruleType == "alert"
681
+ returnRecording := ruleType == "" || ruleType == "record"
682
+
667
683
for _ , group := range groups {
684
+ if len (fileSet ) > 0 {
685
+ if _ , OK := fileSet [group .File ()]; ! OK {
686
+ continue
687
+ }
688
+ }
689
+
690
+ if len (ruleGroupNameSet ) > 0 {
691
+ if _ , OK := ruleGroupNameSet [group .Name ()]; ! OK {
692
+ continue
693
+ }
694
+ }
668
695
interval := group .Interval ()
669
696
670
697
// The mapped filename is url path escaped encoded to make handling `/` characters easier
@@ -685,6 +712,11 @@ func (r *Ruler) getLocalRules(userID string) ([]*GroupStateDesc, error) {
685
712
EvaluationDuration : group .GetEvaluationTime (),
686
713
}
687
714
for _ , r := range group .Rules () {
715
+ if len (ruleNameSet ) > 0 {
716
+ if _ , OK := ruleNameSet [r .Name ()]; ! OK {
717
+ continue
718
+ }
719
+ }
688
720
lastError := ""
689
721
if r .LastError () != nil {
690
722
lastError = r .LastError ().Error ()
@@ -693,7 +725,9 @@ func (r *Ruler) getLocalRules(userID string) ([]*GroupStateDesc, error) {
693
725
var ruleDesc * RuleStateDesc
694
726
switch rule := r .(type ) {
695
727
case * promRules.AlertingRule :
696
- rule .ActiveAlerts ()
728
+ if ! returnAlerts {
729
+ break
730
+ }
697
731
alerts := []* AlertStateDesc {}
698
732
for _ , a := range rule .ActiveAlerts () {
699
733
alerts = append (alerts , & AlertStateDesc {
@@ -725,6 +759,9 @@ func (r *Ruler) getLocalRules(userID string) ([]*GroupStateDesc, error) {
725
759
EvaluationDuration : rule .GetEvaluationDuration (),
726
760
}
727
761
case * promRules.RecordingRule :
762
+ if ! returnRecording {
763
+ break
764
+ }
728
765
ruleDesc = & RuleStateDesc {
729
766
Rule : & rulespb.RuleDesc {
730
767
Record : rule .Name (),
@@ -741,12 +778,14 @@ func (r *Ruler) getLocalRules(userID string) ([]*GroupStateDesc, error) {
741
778
}
742
779
groupDesc .ActiveRules = append (groupDesc .ActiveRules , ruleDesc )
743
780
}
744
- groupDescs = append (groupDescs , groupDesc )
781
+ if len (groupDesc .ActiveRules ) > 0 {
782
+ groupDescs = append (groupDescs , groupDesc )
783
+ }
745
784
}
746
785
return groupDescs , nil
747
786
}
748
787
749
- func (r * Ruler ) getShardedRules (ctx context.Context , userID string ) ([]* GroupStateDesc , error ) {
788
+ func (r * Ruler ) getShardedRules (ctx context.Context , userID string , rulesRequest RulesRequest ) ([]* GroupStateDesc , error ) {
750
789
ring := ring .ReadRing (r .ring )
751
790
752
791
if shardSize := r .limits .RulerTenantShardSize (userID ); shardSize > 0 && r .cfg .ShardingStrategy == util .ShardingStrategyShuffle {
@@ -779,7 +818,7 @@ func (r *Ruler) getShardedRules(ctx context.Context, userID string) ([]*GroupSta
779
818
return errors .Wrapf (err , "unable to get client for ruler %s" , addr )
780
819
}
781
820
782
- newGrps , err := rulerClient .Rules (ctx , & RulesRequest {} )
821
+ newGrps , err := rulerClient .Rules (ctx , & rulesRequest )
783
822
if err != nil {
784
823
return errors .Wrapf (err , "unable to retrieve rules from ruler %s" , addr )
785
824
}
@@ -801,7 +840,7 @@ func (r *Ruler) Rules(ctx context.Context, in *RulesRequest) (*RulesResponse, er
801
840
return nil , fmt .Errorf ("no user id found in context" )
802
841
}
803
842
804
- groupDescs , err := r .getLocalRules (userID )
843
+ groupDescs , err := r .getLocalRules (userID , * in )
805
844
if err != nil {
806
845
return nil , err
807
846
}
0 commit comments