Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
fayzal-g committed Oct 16, 2024
1 parent baeb8d9 commit c5f3572
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
6 changes: 0 additions & 6 deletions pkg/ruler/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"io"
"net/http"
"net/url"
"sort"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -266,11 +265,6 @@ func (a *API) PrometheusRules(w http.ResponseWriter, req *http.Request) {
groups = append(groups, &grp)
}

// keep data.groups are in order
sort.Slice(groups, func(i, j int) bool {
return groups[i].File < groups[j].File
})

b, err := json.Marshal(&response{
Status: "success",
Data: &RuleDiscovery{RuleGroups: groups, NextToken: token},
Expand Down
27 changes: 15 additions & 12 deletions pkg/ruler/ruler.go
Original file line number Diff line number Diff line change
Expand Up @@ -995,20 +995,21 @@ func (r *Ruler) GetRules(ctx context.Context, req RulesRequest) ([]*GroupStateDe
return nil
})

// If the request asks for pagination, we fetch req.MaxGroups number
// of rule groups from each replica. We then merge and sort these and
// take the top k (k = MaxGroups)
if req.MaxGroups > 0 {
slices.SortFunc(merged, func(a, b *GroupStateDesc) int {
nsCmp := strings.Compare(a.Group.Namespace, b.Group.Namespace)
if nsCmp != 0 {
return nsCmp
}
// Sort by namespace and group
slices.SortFunc(merged, func(a, b *GroupStateDesc) int {
nsCmp := strings.Compare(a.Group.Namespace, b.Group.Namespace)
if nsCmp != 0 {
return nsCmp
}

// If Namespaces are equal, check the group names
return strings.Compare(a.Group.Name, b.Group.Name)
})
// If Namespaces are equal, check the group names
return strings.Compare(a.Group.Name, b.Group.Name)
})

// If the request asks for pagination, we fetch req.MaxGroups number
// of rule groups from each replica. These are merged and sorted and
// we take the top k (k = MaxGroups)
if req.MaxGroups > 0 {
if len(merged) > int(req.MaxGroups) {
groupForToken := merged[req.MaxGroups]
return merged[:req.MaxGroups], getRuleGroupNextToken(groupForToken.Group.Namespace, groupForToken.Group.Name), err
Expand Down Expand Up @@ -1106,6 +1107,8 @@ func (r *Ruler) getLocalRules(ctx context.Context, userID string, req RulesReque
return nil, errors.Wrap(err, "unable to decode rule filename")
}

// If a pagination token is provided, skip past groups until we reach the namespace+group that is
// greater than or equal to the namespace+group used to generate the token.
if req.NextToken != "" && !foundToken {
if !tokenGreaterThanOrEqual(getRuleGroupNextToken(decodedNamespace, group.Name()), req.NextToken) {
continue
Expand Down

0 comments on commit c5f3572

Please sign in to comment.