diff --git a/CHANGELOG.md b/CHANGELOG.md index ee6b6f1720f..7e347e0b971 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ * [FEATURE] Querier/StoreGateway: Allow the tenant shard sizes to be a percent of total instances. #5393 * [FEATURE] Added the flag `-alertmanager.api-concurrency` to configure alert manager api concurrency limit. #5412 * [FEATURE] Store Gateway: Add `-store-gateway.sharding-ring.keep-instance-in-the-ring-on-shutdown` to skip unregistering instance from the ring in shutdown. #5421 +* [FEATURE] Ruler: Support for filtering rules in the API. #5417 * [ENHANCEMENT] Distributor/Ingester: Add span on push path #5319 * [ENHANCEMENT] Support object storage backends for runtime configuration file. #5292 * [ENHANCEMENT] Query Frontend: Reject subquery with too small step size. #5323 diff --git a/docs/contributing/how-integration-tests-work.md b/docs/contributing/how-integration-tests-work.md index 2d9fee42977..0b44e35a4d0 100644 --- a/docs/contributing/how-integration-tests-work.md +++ b/docs/contributing/how-integration-tests-work.md @@ -46,4 +46,4 @@ Integration tests have `requires_docker` tag (`// +build requires_docker` line f ## Isolation -Each integration test runs in isolation. For each integration test, we do create a Docker network, start Cortex and its dependencies containers, push/query series to/from Cortex and run assertions on it. Once the test has done, both the Docker network and containers are terminated and deleted. +Each integration test runs in isolation. For each integration test, we do create a Docker network, start Cortex and its dependencies containers, push/query series to/from Cortex and run assertions on it. Once the test has done, both the Docker network and containers are terminated and deleted. \ No newline at end of file diff --git a/integration/e2ecortex/client.go b/integration/e2ecortex/client.go index 7912f28c513..deca9251106 100644 --- a/integration/e2ecortex/client.go +++ b/integration/e2ecortex/client.go @@ -361,6 +361,69 @@ type ServerStatus struct { } `json:"data"` } +type RuleFilter struct { + Namespaces []string + RuleGroupNames []string + RuleNames []string + RuleType string +} + +func addQueryParams(urlValues url.Values, paramName string, params ...string) { + for _, paramValue := range params { + urlValues.Add(paramName, paramValue) + } +} + +// GetPrometheusRulesWithFilter fetches the rules from the Prometheus endpoint /api/v1/rules. +func (c *Client) GetPrometheusRulesWithFilter(filter RuleFilter) ([]*ruler.RuleGroup, error) { + // Create HTTP request + + req, err := http.NewRequest("GET", fmt.Sprintf("http://%s/api/prom/api/v1/rules", c.rulerAddress), nil) + if err != nil { + return nil, err + } + req.Header.Set("X-Scope-OrgID", c.orgID) + + urlValues := req.URL.Query() + addQueryParams(urlValues, "file[]", filter.Namespaces...) + addQueryParams(urlValues, "rule_name[]", filter.RuleNames...) + addQueryParams(urlValues, "rule_group[]", filter.RuleGroupNames...) + addQueryParams(urlValues, "type", filter.RuleType) + req.URL.RawQuery = urlValues.Encode() + + ctx, cancel := context.WithTimeout(context.Background(), c.timeout) + defer cancel() + + // Execute HTTP request + res, err := c.httpClient.Do(req.WithContext(ctx)) + if err != nil { + return nil, err + } + defer res.Body.Close() + + body, err := io.ReadAll(res.Body) + if err != nil { + return nil, err + } + + // Decode the response. + type response struct { + Status string `json:"status"` + Data ruler.RuleDiscovery `json:"data"` + } + + decoded := &response{} + if err := json.Unmarshal(body, decoded); err != nil { + return nil, err + } + + if decoded.Status != "success" { + return nil, fmt.Errorf("unexpected response status '%s'", decoded.Status) + } + + return decoded.Data.RuleGroups, nil +} + // GetPrometheusRules fetches the rules from the Prometheus endpoint /api/v1/rules. func (c *Client) GetPrometheusRules() ([]*ruler.RuleGroup, error) { // Create HTTP request diff --git a/integration/ruler_test.go b/integration/ruler_test.go index e32535c7dad..d1b8018a5b4 100644 --- a/integration/ruler_test.go +++ b/integration/ruler_test.go @@ -9,6 +9,7 @@ import ( "crypto/x509/pkix" "fmt" "math" + "math/rand" "net/http" "os" "path/filepath" @@ -17,6 +18,8 @@ import ( "testing" "time" + "github.com/cortexproject/cortex/pkg/ruler" + "github.com/cortexproject/cortex/pkg/storage/tsdb" "github.com/prometheus/common/model" @@ -389,6 +392,163 @@ func TestRulerSharding(t *testing.T) { assert.ElementsMatch(t, expectedNames, actualNames) } +func TestRulerAPISharding(t *testing.T) { + const numRulesGroups = 100 + + random := rand.New(rand.NewSource(time.Now().UnixNano())) + s, err := e2e.NewScenario(networkName) + require.NoError(t, err) + defer s.Close() + + // Generate multiple rule groups, with 1 rule each. + ruleGroups := make([]rulefmt.RuleGroup, numRulesGroups) + expectedNames := make([]string, numRulesGroups) + alertCount := 0 + for i := 0; i < numRulesGroups; i++ { + num := random.Intn(100) + var ruleNode yaml.Node + var exprNode yaml.Node + + ruleNode.SetString(fmt.Sprintf("rule_%d", i)) + exprNode.SetString(strconv.Itoa(i)) + ruleName := fmt.Sprintf("test_%d", i) + + expectedNames[i] = ruleName + if num%2 == 0 { + alertCount++ + ruleGroups[i] = rulefmt.RuleGroup{ + Name: ruleName, + Interval: 60, + Rules: []rulefmt.RuleNode{{ + Alert: ruleNode, + Expr: exprNode, + }}, + } + } else { + ruleGroups[i] = rulefmt.RuleGroup{ + Name: ruleName, + Interval: 60, + Rules: []rulefmt.RuleNode{{ + Record: ruleNode, + Expr: exprNode, + }}, + } + } + } + + // Start dependencies. + consul := e2edb.NewConsul() + minio := e2edb.NewMinio(9000, rulestoreBucketName) + require.NoError(t, s.StartAndWaitReady(consul, minio)) + + // Configure the ruler. + rulerFlags := mergeFlags( + BlocksStorageFlags(), + RulerFlags(), + RulerShardingFlags(consul.NetworkHTTPEndpoint()), + map[string]string{ + // Since we're not going to run any rule, we don't need the + // store-gateway to be configured to a valid address. + "-querier.store-gateway-addresses": "localhost:12345", + // Enable the bucket index so we can skip the initial bucket scan. + "-blocks-storage.bucket-store.bucket-index.enabled": "true", + }, + ) + + // Start rulers. + ruler1 := e2ecortex.NewRuler("ruler-1", consul.NetworkHTTPEndpoint(), rulerFlags, "") + ruler2 := e2ecortex.NewRuler("ruler-2", consul.NetworkHTTPEndpoint(), rulerFlags, "") + rulers := e2ecortex.NewCompositeCortexService(ruler1, ruler2) + require.NoError(t, s.StartAndWaitReady(ruler1, ruler2)) + + // Upload rule groups to one of the rulers. + c, err := e2ecortex.NewClient("", "", "", ruler1.HTTPEndpoint(), "user-1") + require.NoError(t, err) + + namespaceNames := []string{"test1", "test2", "test3", "test4", "test5"} + namespaceNameCount := make([]int, 5) + nsRand := rand.New(rand.NewSource(time.Now().UnixNano())) + for _, ruleGroup := range ruleGroups { + index := nsRand.Intn(len(namespaceNames)) + namespaceNameCount[index] = namespaceNameCount[index] + 1 + require.NoError(t, c.SetRuleGroup(ruleGroup, namespaceNames[index])) + } + + // Wait until rulers have loaded all rules. + require.NoError(t, rulers.WaitSumMetricsWithOptions(e2e.Equals(numRulesGroups), []string{"cortex_prometheus_rule_group_rules"}, e2e.WaitMissingMetrics)) + + // Since rulers have loaded all rules, we expect that rules have been sharded + // between the two rulers. + require.NoError(t, ruler1.WaitSumMetrics(e2e.Less(numRulesGroups), "cortex_prometheus_rule_group_rules")) + require.NoError(t, ruler2.WaitSumMetrics(e2e.Less(numRulesGroups), "cortex_prometheus_rule_group_rules")) + + testCases := map[string]struct { + filter e2ecortex.RuleFilter + resultCheckFn func(assert.TestingT, []*ruler.RuleGroup) + }{ + "Filter for Alert Rules": { + filter: e2ecortex.RuleFilter{ + RuleType: "alert", + }, + resultCheckFn: func(t assert.TestingT, ruleGroups []*ruler.RuleGroup) { + assert.Len(t, ruleGroups, alertCount, "Expected %d rules but got %d", alertCount, len(ruleGroups)) + }, + }, + "Filter for Recording Rules": { + filter: e2ecortex.RuleFilter{ + RuleType: "record", + }, + resultCheckFn: func(t assert.TestingT, ruleGroups []*ruler.RuleGroup) { + assert.Len(t, ruleGroups, numRulesGroups-alertCount, "Expected %d rules but got %d", numRulesGroups-alertCount, len(ruleGroups)) + }, + }, + "Filter by Namespace Name": { + filter: e2ecortex.RuleFilter{ + Namespaces: []string{namespaceNames[2]}, + }, + resultCheckFn: func(t assert.TestingT, ruleGroups []*ruler.RuleGroup) { + assert.Len(t, ruleGroups, namespaceNameCount[2], "Expected %d rules but got %d", namespaceNameCount[2], len(ruleGroups)) + }, + }, + "Filter by Namespace Name and Alert Rules": { + filter: e2ecortex.RuleFilter{ + RuleType: "alert", + Namespaces: []string{namespaceNames[2]}, + }, + resultCheckFn: func(t assert.TestingT, ruleGroups []*ruler.RuleGroup) { + for _, ruleGroup := range ruleGroups { + rule := ruleGroup.Rules[0].(map[string]interface{}) + ruleType := rule["type"] + assert.Equal(t, "alerting", ruleType, "Expected 'alerting' rule type but got %s", ruleType) + } + }, + }, + "Filter by Rule Names": { + filter: e2ecortex.RuleFilter{ + RuleNames: []string{"rule_3", "rule_64", "rule_99"}, + }, + resultCheckFn: func(t assert.TestingT, ruleGroups []*ruler.RuleGroup) { + ruleNames := []string{} + for _, ruleGroup := range ruleGroups { + rule := ruleGroup.Rules[0].(map[string]interface{}) + ruleName := rule["name"] + ruleNames = append(ruleNames, ruleName.(string)) + + } + assert.Len(t, ruleNames, 3, "Expected %d rules but got %d", 3, len(ruleNames)) + }, + }, + } + // For each test case, fetch the rules with configured filters, and ensure the results match. + for name, tc := range testCases { + t.Run(name, func(t *testing.T) { + actualGroups, err := c.GetPrometheusRulesWithFilter(tc.filter) + require.NoError(t, err) + tc.resultCheckFn(t, actualGroups) + }) + } +} + func TestRulerAlertmanager(t *testing.T) { var namespaceOne = "test_/encoded_+namespace/?" ruleGroup := createTestRuleGroup(t) diff --git a/pkg/ruler/api.go b/pkg/ruler/api.go index 4dda02895a6..8781f44364d 100644 --- a/pkg/ruler/api.go +++ b/pkg/ruler/api.go @@ -2,6 +2,7 @@ package ruler import ( "encoding/json" + "fmt" io "io" "net/http" "net/url" @@ -119,6 +120,26 @@ func respondError(logger log.Logger, w http.ResponseWriter, msg string) { } } +func respondBadRequest(logger log.Logger, w http.ResponseWriter, msg string) { + b, err := json.Marshal(&response{ + Status: "error", + ErrorType: v1.ErrBadData, + Error: msg, + Data: nil, + }) + + if err != nil { + level.Error(logger).Log("msg", "error marshaling json response", "err", err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.WriteHeader(http.StatusBadRequest) + if n, err := w.Write(b); err != nil { + level.Error(logger).Log("msg", "error writing response", "bytesWritten", n, "err", err) + } +} + // API is used to handle HTTP requests for the ruler service type API struct { ruler *Ruler @@ -145,8 +166,27 @@ func (a *API) PrometheusRules(w http.ResponseWriter, req *http.Request) { return } + if err := req.ParseForm(); err != nil { + level.Error(logger).Log("msg", "error parsing form/query params", "err", err) + respondBadRequest(logger, w, "error parsing form/query params") + return + } + + typ := strings.ToLower(req.URL.Query().Get("type")) + if typ != "" && typ != alertingRuleFilter && typ != recordingRuleFilter { + respondBadRequest(logger, w, fmt.Sprintf("unsupported rule type %q", typ)) + return + } + + rulesRequest := RulesRequest{ + RuleNames: req.Form["rule_name[]"], + RuleGroupNames: req.Form["rule_group[]"], + Files: req.Form["file[]"], + Type: typ, + } + w.Header().Set("Content-Type", "application/json") - rgs, err := a.ruler.GetRules(req.Context()) + rgs, err := a.ruler.GetRules(req.Context(), rulesRequest) if err != nil { respondError(logger, w, err.Error()) @@ -238,7 +278,10 @@ func (a *API) PrometheusAlerts(w http.ResponseWriter, req *http.Request) { } w.Header().Set("Content-Type", "application/json") - rgs, err := a.ruler.GetRules(req.Context()) + rulesRequest := RulesRequest{ + Type: alertingRuleFilter, + } + rgs, err := a.ruler.GetRules(req.Context(), rulesRequest) if err != nil { respondError(logger, w, err.Error()) diff --git a/pkg/ruler/ruler.go b/pkg/ruler/ruler.go index cab3e4d2102..2215c41faa2 100644 --- a/pkg/ruler/ruler.go +++ b/pkg/ruler/ruler.go @@ -66,6 +66,9 @@ const ( // errors errListAllUser = "unable to list the ruler users" + + alertingRuleFilter string = "alert" + recordingRuleFilter string = "record" ) // Config is the configuration for the recording rules server. @@ -645,33 +648,59 @@ func filterRuleGroups(userID string, ruleGroups []*rulespb.RuleGroupDesc, ring r // GetRules retrieves the running rules from this ruler and all running rulers in the ring if // sharding is enabled -func (r *Ruler) GetRules(ctx context.Context) ([]*GroupStateDesc, error) { +func (r *Ruler) GetRules(ctx context.Context, rulesRequest RulesRequest) ([]*GroupStateDesc, error) { userID, err := tenant.TenantID(ctx) if err != nil { return nil, fmt.Errorf("no user id found in context") } if r.cfg.EnableSharding { - return r.getShardedRules(ctx, userID) + return r.getShardedRules(ctx, userID, rulesRequest) } - return r.getLocalRules(userID) + return r.getLocalRules(userID, rulesRequest) } -func (r *Ruler) getLocalRules(userID string) ([]*GroupStateDesc, error) { +func (r *Ruler) getLocalRules(userID string, rulesRequest RulesRequest) ([]*GroupStateDesc, error) { groups := r.manager.GetRules(userID) groupDescs := make([]*GroupStateDesc, 0, len(groups)) prefix := filepath.Join(r.cfg.RulePath, userID) + "/" - for _, group := range groups { - interval := group.Interval() + sliceToSet := func(values []string) map[string]struct{} { + set := make(map[string]struct{}, len(values)) + for _, v := range values { + set[v] = struct{}{} + } + return set + } + + ruleNameSet := sliceToSet(rulesRequest.RuleNames) + ruleGroupNameSet := sliceToSet(rulesRequest.RuleGroupNames) + fileSet := sliceToSet(rulesRequest.Files) + ruleType := rulesRequest.Type + returnAlerts := ruleType == "" || ruleType == alertingRuleFilter + returnRecording := ruleType == "" || ruleType == recordingRuleFilter + + for _, group := range groups { // The mapped filename is url path escaped encoded to make handling `/` characters easier decodedNamespace, err := url.PathUnescape(strings.TrimPrefix(group.File(), prefix)) if err != nil { return nil, errors.Wrap(err, "unable to decode rule filename") } + if len(fileSet) > 0 { + if _, OK := fileSet[decodedNamespace]; !OK { + continue + } + } + + if len(ruleGroupNameSet) > 0 { + if _, OK := ruleGroupNameSet[group.Name()]; !OK { + continue + } + } + interval := group.Interval() groupDesc := &GroupStateDesc{ Group: &rulespb.RuleGroupDesc{ @@ -685,6 +714,11 @@ func (r *Ruler) getLocalRules(userID string) ([]*GroupStateDesc, error) { EvaluationDuration: group.GetEvaluationTime(), } for _, r := range group.Rules() { + if len(ruleNameSet) > 0 { + if _, OK := ruleNameSet[r.Name()]; !OK { + continue + } + } lastError := "" if r.LastError() != nil { lastError = r.LastError().Error() @@ -693,7 +727,9 @@ func (r *Ruler) getLocalRules(userID string) ([]*GroupStateDesc, error) { var ruleDesc *RuleStateDesc switch rule := r.(type) { case *promRules.AlertingRule: - rule.ActiveAlerts() + if !returnAlerts { + continue + } alerts := []*AlertStateDesc{} for _, a := range rule.ActiveAlerts() { alerts = append(alerts, &AlertStateDesc{ @@ -725,6 +761,9 @@ func (r *Ruler) getLocalRules(userID string) ([]*GroupStateDesc, error) { EvaluationDuration: rule.GetEvaluationDuration(), } case *promRules.RecordingRule: + if !returnRecording { + continue + } ruleDesc = &RuleStateDesc{ Rule: &rulespb.RuleDesc{ Record: rule.Name(), @@ -741,12 +780,15 @@ func (r *Ruler) getLocalRules(userID string) ([]*GroupStateDesc, error) { } groupDesc.ActiveRules = append(groupDesc.ActiveRules, ruleDesc) } - groupDescs = append(groupDescs, groupDesc) + if len(groupDesc.ActiveRules) > 0 { + groupDescs = append(groupDescs, groupDesc) + } } + return groupDescs, nil } -func (r *Ruler) getShardedRules(ctx context.Context, userID string) ([]*GroupStateDesc, error) { +func (r *Ruler) getShardedRules(ctx context.Context, userID string, rulesRequest RulesRequest) ([]*GroupStateDesc, error) { ring := ring.ReadRing(r.ring) if shardSize := r.limits.RulerTenantShardSize(userID); shardSize > 0 && r.cfg.ShardingStrategy == util.ShardingStrategyShuffle { @@ -779,7 +821,12 @@ func (r *Ruler) getShardedRules(ctx context.Context, userID string) ([]*GroupSta return errors.Wrapf(err, "unable to get client for ruler %s", addr) } - newGrps, err := rulerClient.Rules(ctx, &RulesRequest{}) + newGrps, err := rulerClient.Rules(ctx, &RulesRequest{ + RuleNames: rulesRequest.GetRuleNames(), + RuleGroupNames: rulesRequest.GetRuleGroupNames(), + Files: rulesRequest.GetFiles(), + Type: rulesRequest.GetType(), + }) if err != nil { return errors.Wrapf(err, "unable to retrieve rules from ruler %s", addr) } @@ -801,7 +848,7 @@ func (r *Ruler) Rules(ctx context.Context, in *RulesRequest) (*RulesResponse, er return nil, fmt.Errorf("no user id found in context") } - groupDescs, err := r.getLocalRules(userID) + groupDescs, err := r.getLocalRules(userID, *in) if err != nil { return nil, err } diff --git a/pkg/ruler/ruler.pb.go b/pkg/ruler/ruler.pb.go index e427a077629..93dfd181230 100644 --- a/pkg/ruler/ruler.pb.go +++ b/pkg/ruler/ruler.pb.go @@ -39,6 +39,10 @@ var _ = time.Kitchen const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package type RulesRequest struct { + RuleNames []string `protobuf:"bytes,1,rep,name=ruleNames,proto3" json:"ruleNames,omitempty"` + RuleGroupNames []string `protobuf:"bytes,2,rep,name=ruleGroupNames,proto3" json:"ruleGroupNames,omitempty"` + Files []string `protobuf:"bytes,3,rep,name=files,proto3" json:"files,omitempty"` + Type string `protobuf:"bytes,4,opt,name=type,proto3" json:"type,omitempty"` } func (m *RulesRequest) Reset() { *m = RulesRequest{} } @@ -73,6 +77,34 @@ func (m *RulesRequest) XXX_DiscardUnknown() { var xxx_messageInfo_RulesRequest proto.InternalMessageInfo +func (m *RulesRequest) GetRuleNames() []string { + if m != nil { + return m.RuleNames + } + return nil +} + +func (m *RulesRequest) GetRuleGroupNames() []string { + if m != nil { + return m.RuleGroupNames + } + return nil +} + +func (m *RulesRequest) GetFiles() []string { + if m != nil { + return m.Files + } + return nil +} + +func (m *RulesRequest) GetType() string { + if m != nil { + return m.Type + } + return "" +} + type RulesResponse struct { Groups []*GroupStateDesc `protobuf:"bytes,1,rep,name=groups,proto3" json:"groups,omitempty"` } @@ -380,50 +412,52 @@ func init() { func init() { proto.RegisterFile("ruler.proto", fileDescriptor_9ecbec0a4cfddea6) } var fileDescriptor_9ecbec0a4cfddea6 = []byte{ - // 674 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x54, 0x4f, 0x4f, 0x13, 0x4f, - 0x18, 0xde, 0x29, 0x6c, 0x69, 0xa7, 0xfc, 0xf8, 0x25, 0x43, 0x35, 0x6b, 0x63, 0xa6, 0xa4, 0x5e, - 0x88, 0x89, 0xdb, 0x04, 0x49, 0x8c, 0x07, 0x34, 0x4b, 0x40, 0x2f, 0x1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ad, 0x6d, 0x40, 0x57, 0x5d, - 0x5e, 0x8e, 0xd6, 0xcd, 0x87, 0x40, 0xcb, 0x53, 0x6f, 0x58, 0xf9, 0xda, 0x76, 0xda, 0xb3, 0x4e, - 0xf3, 0xe4, 0xf6, 0x9c, 0xcd, 0xf5, 0xd3, 0x0b, 0xec, 0x9c, 0x5d, 0x60, 0xe7, 0xea, 0x02, 0x83, - 0xa3, 0x02, 0x83, 0x4f, 0x05, 0x06, 0x27, 0x05, 0x06, 0xa7, 0x05, 0x06, 0xdf, 0x0a, 0x0c, 0xbe, - 0x17, 0xd8, 0xb9, 0x2a, 0x30, 0x38, 0xbe, 0xc4, 0xce, 0xe9, 0x25, 0x76, 0xce, 0x2e, 0xb1, 0x33, - 0xa8, 0xeb, 0xf6, 0x1e, 0xfe, 0x08, 0x00, 0x00, 0xff, 0xff, 0x71, 0x5b, 0x12, 0x62, 0xb9, 0x06, - 0x00, 0x00, + // 720 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x54, 0x4f, 0x6b, 0x13, 0x4d, + 0x1c, 0xde, 0x49, 0xbb, 0x69, 0x32, 0xdb, 0xb7, 0x2f, 0x4c, 0xf3, 0xbe, 0xac, 0x41, 0x26, 0x25, + 0x82, 0x14, 0xc1, 0x0d, 0xc4, 0x82, 0x78, 0xa8, 0xb2, 0xa5, 0xd5, 0x8b, 0x88, 0x6c, 0xd5, 0x6b, + 0x99, 0x24, 0xd3, 0xed, 0xea, 0x66, 0x67, 0x9d, 0x99, 0x0d, 0xf5, 0x22, 0xfd, 0x08, 0x3d, 0x7a, + 0xf6, 0xe4, 0x47, 0xe9, 0xb1, 0xc7, 0x22, 0x52, 0xed, 0xf6, 0xe2, 0xb1, 0x1f, 0x41, 0x66, 0x66, + 0xd7, 0x24, 0xb5, 0x82, 0x41, 0x7a, 0x49, 0xf6, 0xf7, 0xe7, 0x79, 0x7e, 0x7f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, 0x21, 0xd0, 0xf2, 0xc4, 0x1b, 0x56, 0xbe, 0xee, 0xcd, 0xc6, 0xb4, 0xd3, 0x3c, 0xb9, + 0x6d, 0x6b, 0x63, 0xed, 0xf8, 0x0c, 0x5b, 0x27, 0x67, 0xd8, 0xba, 0x38, 0xc3, 0xe0, 0x20, 0xc7, + 0xe0, 0x53, 0x8e, 0xc1, 0x51, 0x8e, 0xc1, 0x71, 0x8e, 0xc1, 0xb7, 0x1c, 0x83, 0xef, 0x39, 0xb6, + 0x2e, 0x72, 0x0c, 0x0e, 0xcf, 0xb1, 0x75, 0x7c, 0x8e, 0xad, 0x93, 0x73, 0x6c, 0xf5, 0xaa, 0xba, + 0xbd, 0x7b, 0x3f, 0x02, 0x00, 0x00, 0xff, 0xff, 0x7f, 0xb5, 0x75, 0x2a, 0x29, 0x07, 0x00, 0x00, } func (this *RulesRequest) Equal(that interface{}) bool { @@ -445,6 +479,33 @@ func (this *RulesRequest) Equal(that interface{}) bool { } else if this == nil { return false } + if len(this.RuleNames) != len(that1.RuleNames) { + return false + } + for i := range this.RuleNames { + if this.RuleNames[i] != that1.RuleNames[i] { + return false + } + } + if len(this.RuleGroupNames) != len(that1.RuleGroupNames) { + return false + } + for i := range this.RuleGroupNames { + if this.RuleGroupNames[i] != that1.RuleGroupNames[i] { + return false + } + } + if len(this.Files) != len(that1.Files) { + return false + } + for i := range this.Files { + if this.Files[i] != that1.Files[i] { + return false + } + } + if this.Type != that1.Type { + return false + } return true } func (this *RulesResponse) Equal(that interface{}) bool { @@ -623,8 +684,12 @@ func (this *RulesRequest) GoString() string { if this == nil { return "nil" } - s := make([]string, 0, 4) + s := make([]string, 0, 8) s = append(s, "&ruler.RulesRequest{") + s = append(s, "RuleNames: "+fmt.Sprintf("%#v", this.RuleNames)+",\n") + s = append(s, "RuleGroupNames: "+fmt.Sprintf("%#v", this.RuleGroupNames)+",\n") + s = append(s, "Files: "+fmt.Sprintf("%#v", this.Files)+",\n") + s = append(s, "Type: "+fmt.Sprintf("%#v", this.Type)+",\n") s = append(s, "}") return strings.Join(s, "") } @@ -804,6 +869,40 @@ func (m *RulesRequest) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if len(m.Type) > 0 { + i -= len(m.Type) + copy(dAtA[i:], m.Type) + i = encodeVarintRuler(dAtA, i, uint64(len(m.Type))) + i-- + dAtA[i] = 0x22 + } + if len(m.Files) > 0 { + for iNdEx := len(m.Files) - 1; iNdEx >= 0; iNdEx-- { + i -= len(m.Files[iNdEx]) + copy(dAtA[i:], m.Files[iNdEx]) + i = encodeVarintRuler(dAtA, i, uint64(len(m.Files[iNdEx]))) + i-- + dAtA[i] = 0x1a + } + } + if len(m.RuleGroupNames) > 0 { + for iNdEx := len(m.RuleGroupNames) - 1; iNdEx >= 0; iNdEx-- { + i -= len(m.RuleGroupNames[iNdEx]) + copy(dAtA[i:], m.RuleGroupNames[iNdEx]) + i = encodeVarintRuler(dAtA, i, uint64(len(m.RuleGroupNames[iNdEx]))) + i-- + dAtA[i] = 0x12 + } + } + if len(m.RuleNames) > 0 { + for iNdEx := len(m.RuleNames) - 1; iNdEx >= 0; iNdEx-- { + i -= len(m.RuleNames[iNdEx]) + copy(dAtA[i:], m.RuleNames[iNdEx]) + i = encodeVarintRuler(dAtA, i, uint64(len(m.RuleNames[iNdEx]))) + i-- + dAtA[i] = 0xa + } + } return len(dAtA) - i, nil } @@ -1116,6 +1215,28 @@ func (m *RulesRequest) Size() (n int) { } var l int _ = l + if len(m.RuleNames) > 0 { + for _, s := range m.RuleNames { + l = len(s) + n += 1 + l + sovRuler(uint64(l)) + } + } + if len(m.RuleGroupNames) > 0 { + for _, s := range m.RuleGroupNames { + l = len(s) + n += 1 + l + sovRuler(uint64(l)) + } + } + if len(m.Files) > 0 { + for _, s := range m.Files { + l = len(s) + n += 1 + l + sovRuler(uint64(l)) + } + } + l = len(m.Type) + if l > 0 { + n += 1 + l + sovRuler(uint64(l)) + } return n } @@ -1241,6 +1362,10 @@ func (this *RulesRequest) String() string { return "nil" } s := strings.Join([]string{`&RulesRequest{`, + `RuleNames:` + fmt.Sprintf("%v", this.RuleNames) + `,`, + `RuleGroupNames:` + fmt.Sprintf("%v", this.RuleGroupNames) + `,`, + `Files:` + fmt.Sprintf("%v", this.Files) + `,`, + `Type:` + fmt.Sprintf("%v", this.Type) + `,`, `}`, }, "") return s @@ -1354,6 +1479,134 @@ func (m *RulesRequest) Unmarshal(dAtA []byte) error { return fmt.Errorf("proto: RulesRequest: illegal tag %d (wire type %d)", fieldNum, wire) } switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field RuleNames", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowRuler + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthRuler + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthRuler + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.RuleNames = append(m.RuleNames, string(dAtA[iNdEx:postIndex])) + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field RuleGroupNames", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowRuler + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthRuler + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthRuler + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.RuleGroupNames = append(m.RuleGroupNames, string(dAtA[iNdEx:postIndex])) + iNdEx = postIndex + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Files", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowRuler + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthRuler + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthRuler + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Files = append(m.Files, string(dAtA[iNdEx:postIndex])) + iNdEx = postIndex + case 4: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Type", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowRuler + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthRuler + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthRuler + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Type = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipRuler(dAtA[iNdEx:]) diff --git a/pkg/ruler/ruler.proto b/pkg/ruler/ruler.proto index 6cc6d3f1da6..de749674258 100644 --- a/pkg/ruler/ruler.proto +++ b/pkg/ruler/ruler.proto @@ -19,7 +19,12 @@ service Ruler { rpc Rules(RulesRequest) returns (RulesResponse) {}; } -message RulesRequest {} +message RulesRequest { + repeated string ruleNames = 1; + repeated string ruleGroupNames = 2; + repeated string files = 3; + string type = 4; +} message RulesResponse { repeated GroupStateDesc groups = 1; diff --git a/pkg/ruler/ruler_test.go b/pkg/ruler/ruler_test.go index 2bcb6f813d2..295b326ff53 100644 --- a/pkg/ruler/ruler_test.go +++ b/pkg/ruler/ruler_test.go @@ -325,55 +325,214 @@ func compareRuleGroupDescToStateDesc(t *testing.T, expected *rulespb.RuleGroupDe func TestGetRules(t *testing.T) { // ruler ID -> (user ID -> list of groups). type expectedRulesMap map[string]map[string]rulespb.RuleGroupList + type rulesMap map[string][]*rulespb.RuleDesc type testCase struct { sharding bool shardingStrategy string shuffleShardSize int + rulesRequest RulesRequest + expectedCount map[string]int + } + + ruleMap := rulesMap{ + "ruler1-user1-rule-group1": []*rulespb.RuleDesc{ + { + Record: "rtest_user1_1", + Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + }, + { + Alert: "atest_user1_1", + Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + }, + }, + "ruler1-user1-rule-group2": []*rulespb.RuleDesc{ + { + Record: "rtest_user1_1", + Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + }, + }, + "ruler1-user2-rule-group1": []*rulespb.RuleDesc{ + { + Record: "rtest_user1_1", + Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + }, + }, + "ruler2-user1-rule-group3": []*rulespb.RuleDesc{ + { + Record: "rtest_user1_1", + Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + }, + { + Alert: "atest_user1_1", + Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + }, + }, + "ruler2-user2-rule-group1": []*rulespb.RuleDesc{ + { + Record: "rtest_user1_1", + Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + }, + { + Alert: "atest_user1_1", + Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + }, + }, + "ruler2-user2-rule-group2": []*rulespb.RuleDesc{ + { + Record: "rtest_user2_1", + Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + }, + { + Alert: "atest_user2_1", + Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + }, + }, + "ruler2-user3-rule-group1": []*rulespb.RuleDesc{ + { + Alert: "atest_user3_1", + Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + }, + }, + "ruler3-user2-rule-group1": []*rulespb.RuleDesc{ + { + Record: "rtest_user1_1", + Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + }, + { + Alert: "atest_user1_1", + Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + }, + }, + "ruler3-user2-rule-group2": []*rulespb.RuleDesc{ + { + Record: "rtest_user1_1", + Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + }, + { + Alert: "atest_user1_1", + Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + }, + }, + "ruler3-user3-rule-group1": []*rulespb.RuleDesc{ + { + Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + Record: "rtest_user1_1", + }, + { + Alert: "atest_user1_1", + Expr: "sum(rate(node_cpu_seconds_total[3h:10m]))", + }, + }, } expectedRules := expectedRulesMap{ "ruler1": map[string]rulespb.RuleGroupList{ "user1": { - &rulespb.RuleGroupDesc{User: "user1", Namespace: "namespace", Name: "first", Interval: 10 * time.Second}, - &rulespb.RuleGroupDesc{User: "user1", Namespace: "namespace", Name: "second", Interval: 10 * time.Second}, + &rulespb.RuleGroupDesc{User: "user1", Namespace: "namespace", Name: "first", Interval: 10 * time.Second, Rules: ruleMap["ruler1-user1-rule-group1"]}, + &rulespb.RuleGroupDesc{User: "user1", Namespace: "namespace", Name: "second", Interval: 10 * time.Second, Rules: ruleMap["ruler1-user1-rule-group2"]}, }, "user2": { - &rulespb.RuleGroupDesc{User: "user2", Namespace: "namespace", Name: "third", Interval: 10 * time.Second}, + &rulespb.RuleGroupDesc{User: "user2", Namespace: "namespace", Name: "third", Interval: 10 * time.Second, Rules: ruleMap["ruler1-user2-rule-group1"]}, }, }, "ruler2": map[string]rulespb.RuleGroupList{ "user1": { - &rulespb.RuleGroupDesc{User: "user1", Namespace: "namespace", Name: "third", Interval: 10 * time.Second}, + &rulespb.RuleGroupDesc{User: "user1", Namespace: "namespace", Name: "third", Interval: 10 * time.Second, Rules: ruleMap["ruler2-user1-rule-group3"]}, }, "user2": { - &rulespb.RuleGroupDesc{User: "user2", Namespace: "namespace", Name: "first", Interval: 10 * time.Second}, - &rulespb.RuleGroupDesc{User: "user2", Namespace: "namespace", Name: "second", Interval: 10 * time.Second}, + &rulespb.RuleGroupDesc{User: "user2", Namespace: "namespace", Name: "first", Interval: 10 * time.Second, Rules: ruleMap["ruler2-user2-rule-group1"]}, + &rulespb.RuleGroupDesc{User: "user2", Namespace: "namespace", Name: "second", Interval: 10 * time.Second, Rules: ruleMap["ruler2-user2-rule-group2"]}, + }, + "user3": { + &rulespb.RuleGroupDesc{User: "user3", Namespace: "latency-test", Name: "first", Interval: 10 * time.Second, Rules: ruleMap["ruler2-user3-rule-group1"]}, }, }, "ruler3": map[string]rulespb.RuleGroupList{ "user3": { - &rulespb.RuleGroupDesc{User: "user3", Namespace: "namespace", Name: "third", Interval: 10 * time.Second}, + &rulespb.RuleGroupDesc{User: "user3", Namespace: "namespace", Name: "third", Interval: 10 * time.Second, Rules: ruleMap["ruler3-user3-rule-group1"]}, }, "user2": { - &rulespb.RuleGroupDesc{User: "user2", Namespace: "namespace", Name: "forth", Interval: 10 * time.Second}, - &rulespb.RuleGroupDesc{User: "user2", Namespace: "namespace", Name: "fifty", Interval: 10 * time.Second}, + &rulespb.RuleGroupDesc{User: "user2", Namespace: "namespace", Name: "forth", Interval: 10 * time.Second, Rules: ruleMap["ruler3-user2-rule-group1"]}, + &rulespb.RuleGroupDesc{User: "user2", Namespace: "namespace", Name: "fifty", Interval: 10 * time.Second, Rules: ruleMap["ruler3-user2-rule-group2"]}, }, }, } testCases := map[string]testCase{ - "No Sharding": { + "No Sharding with Rule Type Filter": { sharding: false, + rulesRequest: RulesRequest{ + Type: alertingRuleFilter, + }, + expectedCount: map[string]int{ + "user1": 2, + "user2": 4, + "user3": 2, + }, }, - "Default Sharding": { + "Default Sharding with No Filter": { sharding: true, shardingStrategy: util.ShardingStrategyDefault, + expectedCount: map[string]int{ + "user1": 5, + "user2": 9, + "user3": 3, + }, + }, + "Shuffle Sharding and ShardSize = 2 with Rule Type Filter": { + sharding: true, + shuffleShardSize: 2, + shardingStrategy: util.ShardingStrategyShuffle, + rulesRequest: RulesRequest{ + Type: recordingRuleFilter, + }, + expectedCount: map[string]int{ + "user1": 3, + "user2": 5, + "user3": 1, + }, + }, + "Shuffle Sharding and ShardSize = 2 and Rule Group Name Filter": { + sharding: true, + shuffleShardSize: 2, + shardingStrategy: util.ShardingStrategyShuffle, + rulesRequest: RulesRequest{ + RuleGroupNames: []string{"third"}, + }, + expectedCount: map[string]int{ + "user1": 2, + "user2": 1, + "user3": 2, + }, + }, + "Shuffle Sharding and ShardSize = 2 and Rule Group Name and Rule Type Filter": { + sharding: true, + shuffleShardSize: 2, + shardingStrategy: util.ShardingStrategyShuffle, + rulesRequest: RulesRequest{ + RuleGroupNames: []string{"second", "third"}, + Type: recordingRuleFilter, + }, + expectedCount: map[string]int{ + "user1": 2, + "user2": 2, + "user3": 1, + }, }, - "Shuffle Sharding and ShardSize = 2": { + "Shuffle Sharding and ShardSize = 2 with Rule Type and Namespace Filters": { sharding: true, shuffleShardSize: 2, shardingStrategy: util.ShardingStrategyShuffle, + rulesRequest: RulesRequest{ + Type: alertingRuleFilter, + Files: []string{"latency-test"}, + }, + expectedCount: map[string]int{ + "user1": 0, + "user2": 0, + "user3": 1, + }, }, } @@ -446,13 +605,16 @@ func TestGetRules(t *testing.T) { forEachRuler(func(_ string, r *Ruler) { r.syncRules(context.Background(), rulerSyncReasonInitial) }) - for u := range allRulesByUser { ctx := user.InjectOrgID(context.Background(), u) forEachRuler(func(_ string, r *Ruler) { - rules, err := r.GetRules(ctx) + ruleStateDescriptions, err := r.GetRules(ctx, tc.rulesRequest) require.NoError(t, err) - require.Equal(t, len(allRulesByUser[u]), len(rules)) + rct := 0 + for _, ruleStateDesc := range ruleStateDescriptions { + rct += len(ruleStateDesc.ActiveRules) + } + require.Equal(t, tc.expectedCount[u], rct) if tc.sharding { mockPoolClient := r.clientsPool.(*mockRulerClientsPool)