@@ -28,12 +28,7 @@ func manageRulesRead(ctx context.Context, args ManageRulesReadParams) (any, erro
2828 LabelSelectors : args .LabelSelectors ,
2929 })
3030 }
31- return listGrafanaRules (ctx , & GetRulesOpts {
32- RuleLimit : args .RuleLimit ,
33- LimitAlerts : args .LimitAlerts ,
34- FolderUID : args .FolderUID ,
35- SearchFolder : args .SearchFolder ,
36- }, args .LabelSelectors )
31+ return listGrafanaRules (ctx , args .toGetRulesOpts (), args .LabelSelectors )
3732 case "get" :
3833 return getAlertRuleDetail (ctx , args .RuleUID , args .LimitAlerts )
3934 case "versions" :
@@ -57,12 +52,7 @@ func manageRulesReadWrite(ctx context.Context, args ManageRulesReadWriteParams)
5752 LabelSelectors : args .LabelSelectors ,
5853 })
5954 }
60- return listGrafanaRules (ctx , & GetRulesOpts {
61- RuleLimit : args .RuleLimit ,
62- LimitAlerts : args .LimitAlerts ,
63- FolderUID : args .FolderUID ,
64- SearchFolder : args .SearchFolder ,
65- }, args .LabelSelectors )
55+ return listGrafanaRules (ctx , args .toGetRulesOpts (), args .LabelSelectors )
6656 case "get" :
6757 return getAlertRuleDetail (ctx , args .RuleUID , args .LimitAlerts )
6858 case "versions" :
@@ -92,7 +82,15 @@ func getAlertRuleDetail(ctx context.Context, uid string, limitAlerts int) (*aler
9282 return nil , fmt .Errorf ("creating alerting client for rule %s: %w" , uid , err )
9383 }
9484
95- rulesResp , err := ac .GetRules (ctx , & GetRulesOpts {LimitAlerts : limitAlerts })
85+ opts := & GetRulesOpts {LimitAlerts : limitAlerts }
86+ if alertRule .Payload .FolderUID != nil {
87+ opts .FolderUID = * alertRule .Payload .FolderUID
88+ }
89+ if alertRule .Payload .RuleGroup != nil {
90+ opts .RuleGroup = * alertRule .Payload .RuleGroup
91+ }
92+
93+ rulesResp , err := ac .GetRules (ctx , opts )
9694 if err != nil {
9795 slog .WarnContext (ctx , "failed to fetch runtime state for alert rule" ,
9896 "uid" , uid , "error" , err )
@@ -227,6 +225,12 @@ func listGrafanaRules(ctx context.Context, opts *GetRulesOpts, labelSelectors []
227225 }
228226 }
229227
228+ // Server-side rule_limit returns complete groups, so it may exceed the
229+ // requested limit. Enforce the limit client-side as well.
230+ if opts != nil && opts .RuleLimit > 0 && len (summaries ) > opts .RuleLimit {
231+ summaries = summaries [:opts .RuleLimit ]
232+ }
233+
230234 return summaries , nil
231235}
232236
0 commit comments