From cdc3c2aaba9f4ce92f3d22ceccfb2e486a764ae8 Mon Sep 17 00:00:00 2001 From: Saswata Mukherjee Date: Sat, 29 Jan 2022 20:14:40 +0530 Subject: [PATCH 1/5] Add matcher support to Query Rules endpoint Signed-off-by: Saswata Mukherjee --- pkg/api/query/v1.go | 5 + pkg/rules/rules.go | 59 ++++++++++++ pkg/rules/rulespb/rpc.pb.go | 177 +++++++++++++++++++++++------------- pkg/rules/rulespb/rpc.proto | 1 + 4 files changed, 178 insertions(+), 64 deletions(-) diff --git a/pkg/api/query/v1.go b/pkg/api/query/v1.go index f5365a8f39..5ac8bbad32 100644 --- a/pkg/api/query/v1.go +++ b/pkg/api/query/v1.go @@ -787,10 +787,15 @@ func NewRulesHandler(client rules.UnaryClient, enablePartialResponse bool) func( typ = int32(rulespb.RulesRequest_ALL) } + if err := r.ParseForm(); err != nil { + return nil, nil, &api.ApiError{Typ: api.ErrorInternal, Err: errors.Errorf("error parsing request form='%v'", typeParam)} + } + // TODO(bwplotka): Allow exactly the same functionality as query API: passing replica, dedup and partial response as HTTP params as well. req := &rulespb.RulesRequest{ Type: rulespb.RulesRequest_Type(typ), PartialResponseStrategy: ps, + MatcherString: r.Form[MatcherParam], } tracing.DoInSpan(ctx, "retrieve_rules", func(ctx context.Context) { groups, warnings, err = client.Rules(ctx, req) diff --git a/pkg/rules/rules.go b/pkg/rules/rules.go index 219fba6aef..e147f1b8c2 100644 --- a/pkg/rules/rules.go +++ b/pkg/rules/rules.go @@ -7,9 +7,12 @@ import ( "context" "sort" "sync" + "text/template" + "text/template/parse" "github.com/pkg/errors" "github.com/prometheus/prometheus/model/labels" + "github.com/prometheus/prometheus/promql/parser" "github.com/prometheus/prometheus/storage" "github.com/thanos-io/thanos/pkg/rules/rulespb" @@ -58,6 +61,16 @@ func (rr *GRPCClient) Rules(ctx context.Context, req *rulespb.RulesRequest) (*ru return nil, nil, errors.Wrap(err, "proxy Rules") } + var matcherSets [][]*labels.Matcher + for _, s := range req.MatcherString { + matchers, err := parser.ParseMetricSelector(s) + if err != nil { + return nil, nil, errors.Wrap(err, "proxy Rules") + } + matcherSets = append(matcherSets, matchers) + } + + resp.groups = filterRules(resp.groups, matcherSets) // TODO(bwplotka): Move to SortInterface with equal method and heap. resp.groups = dedupGroups(resp.groups) for _, g := range resp.groups { @@ -67,6 +80,52 @@ func (rr *GRPCClient) Rules(ctx context.Context, req *rulespb.RulesRequest) (*ru return &rulespb.RuleGroups{Groups: resp.groups}, resp.warnings, nil } +// filterRules filters rules in a group according to given matcherSets. +func filterRules(ruleGroups []*rulespb.RuleGroup, matcherSets [][]*labels.Matcher) []*rulespb.RuleGroup { + if len(matcherSets) == 0 || len(ruleGroups) == 0 { + return ruleGroups + } + + for _, g := range ruleGroups { + filteredRules := []*rulespb.Rule{} + for _, r := range g.Rules { + rl := r.GetLabels() + if matches(matcherSets, rl) { + filteredRules = append(filteredRules, r) + } + } + g.Rules = filteredRules + } + + return ruleGroups +} + +// matches returns whether the non-templated labels satisfy all the matchers in matcherSets. +func matches(matcherSets [][]*labels.Matcher, l labels.Labels) bool { + if len(matcherSets) == 0 { + return true + } + + var nonTemplatedLabels labels.Labels + labelTemplate := template.New("label") + for _, label := range l { + t, err := labelTemplate.Parse(label.Value) + // Label value is non-templated if it is one node of type NodeText. + if err == nil && len(t.Root.Nodes) == 1 && t.Root.Nodes[0].Type() == parse.NodeText { + nonTemplatedLabels = append(nonTemplatedLabels, label) + } + } + + for _, matchers := range matcherSets { + for _, m := range matchers { + if v := nonTemplatedLabels.Get(m.Name); !m.Matches(v) { + return false + } + } + } + return true +} + // dedupRules re-sorts the set so that the same series with different replica // labels are coming right after each other. func dedupRules(rules []*rulespb.Rule, replicaLabels map[string]struct{}) []*rulespb.Rule { diff --git a/pkg/rules/rulespb/rpc.pb.go b/pkg/rules/rulespb/rpc.pb.go index 4d85a902b8..2c3f368d1a 100644 --- a/pkg/rules/rulespb/rpc.pb.go +++ b/pkg/rules/rulespb/rpc.pb.go @@ -108,6 +108,7 @@ func (RulesRequest_Type) EnumDescriptor() ([]byte, []int) { type RulesRequest struct { Type RulesRequest_Type `protobuf:"varint,1,opt,name=type,proto3,enum=thanos.RulesRequest_Type" json:"type,omitempty"` PartialResponseStrategy storepb.PartialResponseStrategy `protobuf:"varint,2,opt,name=partial_response_strategy,json=partialResponseStrategy,proto3,enum=thanos.PartialResponseStrategy" json:"partial_response_strategy,omitempty"` + MatcherString []string `protobuf:"bytes,3,rep,name=MatcherString,proto3" json:"MatcherString,omitempty"` } func (m *RulesRequest) Reset() { *m = RulesRequest{} } @@ -552,70 +553,71 @@ func init() { func init() { proto.RegisterFile("rules/rulespb/rpc.proto", fileDescriptor_91b1d28f30eb5efb) } var fileDescriptor_91b1d28f30eb5efb = []byte{ - // 999 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x56, 0xcd, 0x6e, 0xdb, 0x46, - 0x10, 0x26, 0x2d, 0x91, 0x12, 0xc7, 0x76, 0xa2, 0x6e, 0x62, 0x98, 0x56, 0x0a, 0xd1, 0x10, 0x90, - 0xc2, 0x2d, 0x1a, 0xa9, 0xb0, 0x91, 0x14, 0x39, 0x15, 0xa2, 0xad, 0xc6, 0x02, 0x0c, 0x37, 0x58, - 0x09, 0x3d, 0xa4, 0x07, 0x95, 0x92, 0x37, 0x32, 0x01, 0x8a, 0x64, 0x76, 0x57, 0x2e, 0xf4, 0x00, - 0xbd, 0xe7, 0xdc, 0x17, 0xe9, 0xbd, 0x27, 0x1f, 0x73, 0xec, 0x49, 0x6d, 0xed, 0x9b, 0x0e, 0x7d, - 0x86, 0x62, 0x77, 0x49, 0x51, 0x56, 0xe5, 0x3a, 0x69, 0xd5, 0x8b, 0x66, 0x77, 0xe6, 0x9b, 0xfd, - 0x99, 0xf9, 0xf6, 0x13, 0x61, 0x9b, 0x8e, 0x02, 0xc2, 0xea, 0xf2, 0x37, 0xee, 0xd5, 0x69, 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0x76, 0x4e, 0x16, 0x1b, 0xdd, 0xb8, 0x47, 0x5b, 0x44, 0x54, 0xcf, 0x24, 0x08, 0x2b, - 0x83, 0xda, 0x60, 0x79, 0x7d, 0xee, 0x5f, 0x90, 0xae, 0xc7, 0x25, 0x69, 0xef, 0xe0, 0xcb, 0x74, - 0xe2, 0x20, 0x95, 0xd0, 0xe0, 0x9f, 0x47, 0x43, 0x9f, 0x93, 0x61, 0xcc, 0xc7, 0x92, 0x2f, 0xc5, - 0xd4, 0x2f, 0x98, 0x22, 0x68, 0x43, 0x24, 0x91, 0x2d, 0xb5, 0xab, 0x74, 0x60, 0x65, 0xfe, 0x89, - 0x29, 0xe6, 0xff, 0xc9, 0x94, 0x1f, 0x0d, 0x30, 0x64, 0x39, 0xb2, 0x62, 0xe9, 0x1f, 0x50, 0xac, - 0x54, 0x4b, 0xd6, 0x96, 0x6a, 0x89, 0x03, 0xc6, 0x9b, 0x11, 0xa1, 0x63, 0x59, 0xff, 0xe4, 0xd6, - 0xd2, 0x81, 0x95, 0x41, 0x5f, 0x42, 0xe9, 0x6f, 0x4f, 0x7d, 0x4e, 0x27, 0xd2, 0x18, 0xbe, 0x7f, - 0xb6, 0xf0, 0xb4, 0x33, 0x7a, 0x19, 0xff, 0x91, 0x5e, 0xe6, 0xbf, 0xa7, 0xd7, 0x73, 0x30, 0xe5, - 0x43, 0x60, 0x76, 0x41, 0xaa, 0xe1, 0xd6, 0x8d, 0x92, 0xa5, 0x4f, 0x41, 0x29, 0xb2, 0x02, 0xe2, - 0xc4, 0xa2, 0x2a, 0x98, 0xe7, 0xc4, 0x0b, 0xf8, 0xb9, 0xd4, 0x01, 0x4b, 0x61, 0x94, 0x07, 0x27, - 0x16, 0x3d, 0x03, 0x50, 0xf2, 0x45, 0x69, 0x44, 0xa5, 0xc4, 0x58, 0xee, 0xf6, 0x74, 0xe2, 0x3c, - 0x90, 0x2a, 0x24, 0x9c, 0x19, 0xdd, 0xb0, 0x35, 0x73, 0xde, 0x25, 0xa5, 0xb0, 0x22, 0x29, 0x5d, - 0x5f, 0xa5, 0x94, 0x56, 0x7f, 0xca, 0xc1, 0xe6, 0x0d, 0x45, 0xba, 0xe3, 0x6f, 0x6a, 0x46, 0xad, - 0xb5, 0x5b, 0xa8, 0x95, 0x31, 0x24, 0xf7, 0xa1, 0x0c, 0xc9, 0x9a, 0x93, 0x7f, 0xcf, 0xe6, 0x18, - 0xab, 0x6a, 0x8e, 0xb9, 0xa2, 0xe6, 0x14, 0x56, 0xd9, 0x9c, 0xcf, 0x0e, 0x00, 0x32, 0x15, 0x40, - 0x1b, 0x50, 0x6c, 0x9d, 0x36, 0x0e, 0x3b, 0xad, 0x6f, 0x9b, 0x25, 0x0d, 0xad, 0x43, 0xe1, 0x65, - 0xf3, 0xf4, 0xa8, 0x75, 0xfa, 0x42, 0x7d, 0x1b, 0x7d, 0xdd, 0xc2, 0x62, 0xbc, 0xb6, 0xff, 0x15, - 0x18, 0xf2, 0xdb, 0x08, 0x3d, 0x4b, 0x07, 0x0f, 0x97, 0x7d, 0xd3, 0x95, 0xb7, 0x16, 0xbc, 0x4a, - 0xa0, 0xbe, 0xd0, 0xdd, 0xc7, 0x97, 0x7f, 0x54, 0xb4, 0xcb, 0xab, 0x8a, 0xfe, 0xee, 0xaa, 0xa2, - 0xff, 0x7e, 0x55, 0xd1, 0xdf, 0x5e, 0x57, 0xb4, 0x77, 0xd7, 0x15, 0xed, 0xd7, 0xeb, 0x8a, 0xf6, - 0xaa, 0x90, 0x7c, 0xc7, 0xf6, 0x4c, 0x79, 0xb9, 0x83, 0xbf, 0x02, 0x00, 0x00, 0xff, 0xff, 0x8f, - 0x24, 0x78, 0x7a, 0xdf, 0x0a, 0x00, 0x00, + // 1021 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x56, 0x41, 0x6f, 0xdb, 0x36, + 0x14, 0x96, 0x22, 0x4b, 0xb6, 0x9e, 0x93, 0x34, 0x63, 0x1b, 0x44, 0x49, 0x07, 0x2b, 0x30, 0xd6, + 0x21, 0x1b, 0x56, 0x7b, 0x48, 0xd0, 0x0e, 0x3d, 0x0d, 0x51, 0x92, 0x35, 0x01, 0xb2, 0xac, 0xa0, + 0x83, 0x1d, 0xba, 0x83, 0xc7, 0x38, 0xac, 0x23, 0x40, 0x96, 0x54, 0x92, 0xce, 0x90, 0x1f, 0xb0, + 0x7b, 0xcf, 0xfb, 0x23, 0xfb, 0x0b, 0x39, 0xf6, 0xb8, 0x93, 0xb7, 0x25, 0x97, 0xc1, 0x87, 0xfd, + 0x86, 0x81, 0xa4, 0x64, 0x39, 0x9e, 0xb3, 0xb4, 0x9b, 0x77, 0xd1, 0x23, 0xdf, 0xfb, 0x1e, 0x45, + 0xbe, 0xf7, 0xf1, 0x93, 0x60, 0x85, 0xf5, 0x23, 0xca, 0x9b, 0xea, 0x99, 0x9e, 0x34, 0x59, 0xda, + 0x69, 0xa4, 0x2c, 0x11, 0x09, 0x72, 0xc4, 0x19, 0x89, 0x13, 0xbe, 0xb6, 0xca, 0x45, 0xc2, 0x68, + 0x53, 0x3d, 0xd3, 0x93, 0xa6, 0xb8, 0x48, 0x29, 0xd7, 0x90, 0x3c, 0x14, 0x91, 0x13, 0x1a, 0x4d, + 0x84, 0x1e, 0x74, 0x93, 0x6e, 0xa2, 0x86, 0x4d, 0x39, 0xca, 0xbc, 0x7e, 0x37, 0x49, 0xba, 0x11, + 0x6d, 0xaa, 0xd9, 0x49, 0xff, 0x55, 0x53, 0x84, 0x3d, 0xca, 0x05, 0xe9, 0xa5, 0x1a, 0x50, 0xff, + 0xc3, 0x84, 0x79, 0x2c, 0xb7, 0x82, 0xe9, 0xeb, 0x3e, 0xe5, 0x02, 0x3d, 0x86, 0x92, 0x5c, 0xd6, + 0x33, 0xd7, 0xcd, 0x8d, 0xc5, 0xcd, 0xd5, 0x86, 0xde, 0x54, 0x63, 0x1c, 0xd3, 0x38, 0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x5b, 0xbc, 0x1e, 0x43, 0x49, 0x76, + 0x04, 0x3d, 0x01, 0x97, 0xd1, 0x4e, 0xc2, 0x4e, 0x25, 0xcb, 0x34, 0x25, 0x97, 0x47, 0x2d, 0xcb, + 0x03, 0x12, 0xb9, 0x6f, 0xe0, 0x02, 0x89, 0x1e, 0x81, 0x4d, 0x22, 0xca, 0x84, 0x22, 0x41, 0x75, + 0x73, 0x21, 0x4f, 0xd9, 0x96, 0x4e, 0xc9, 0x60, 0x15, 0x1d, 0x63, 0xe9, 0xcf, 0x16, 0x2c, 0xa8, + 0xe0, 0x41, 0xcc, 0x05, 0x89, 0x3b, 0x14, 0x3d, 0x03, 0x47, 0x49, 0x0a, 0x9f, 0xbc, 0x09, 0x2f, + 0x0f, 0xa5, 0xbb, 0x45, 0x45, 0xb0, 0x98, 0x55, 0x3a, 0x03, 0xe2, 0xcc, 0xa2, 0x7d, 0xa8, 0x92, + 0x38, 0x4e, 0x84, 0xaa, 0x31, 0xcf, 0xf6, 0x30, 0x25, 0xff, 0x7e, 0x96, 0x3f, 0x8e, 0xc6, 0xe3, + 0x13, 0xb4, 0x05, 0x36, 0x17, 0x44, 0x50, 0xcf, 0x52, 0xc5, 0x46, 0x37, 0xce, 0xd1, 0x92, 0x11, + 0xdd, 0x33, 0x05, 0xc2, 0xda, 0xa0, 0x16, 0xb8, 0xa4, 0x23, 0xc2, 0x73, 0xda, 0x26, 0x42, 0x91, + 0xf6, 0x0e, 0xbe, 0x0c, 0x07, 0x3e, 0xd2, 0x09, 0xdb, 0xe2, 0xb3, 0xa4, 0x17, 0x0a, 0xda, 0x4b, + 0xc5, 0x85, 0xe2, 0x4b, 0x25, 0xf7, 0x4b, 0xa6, 0x48, 0xda, 0x50, 0x45, 0x64, 0x57, 0xbf, 0x55, + 0x39, 0xb0, 0x36, 0xff, 0xc4, 0x14, 0xe7, 0xff, 0x64, 0xca, 0x8f, 0x36, 0xd8, 0xaa, 0x1c, 0x45, + 0xb1, 0xcc, 0xf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xe7, 0xfa, 0xdf, 0xe8, 0xab, 0x03, + 0x2c, 0xc7, 0x73, 0x9b, 0x5f, 0x82, 0xad, 0xfe, 0x8d, 0xd0, 0xd3, 0x7c, 0xf0, 0x60, 0xda, 0x9f, + 0xdf, 0xda, 0xf2, 0x84, 0x57, 0x0b, 0xd4, 0xe7, 0x66, 0xf0, 0xe8, 0xf2, 0xf7, 0x9a, 0x71, 0x79, + 0x55, 0x33, 0xdf, 0x5e, 0xd5, 0xcc, 0xdf, 0xae, 0x6a, 0xe6, 0x9b, 0xeb, 0x9a, 0xf1, 0xf6, 0xba, + 0x66, 0xfc, 0x72, 0x5d, 0x33, 0x5e, 0x96, 0xb3, 0xbf, 0xdd, 0x13, 0x47, 0x1d, 0x6e, 0xeb, 0xaf, + 0x00, 0x00, 0x00, 0xff, 0xff, 0xfc, 0x67, 0x13, 0xd9, 0x05, 0x0b, 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. @@ -749,6 +751,15 @@ func (m *RulesRequest) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if len(m.MatcherString) > 0 { + for iNdEx := len(m.MatcherString) - 1; iNdEx >= 0; iNdEx-- { + i -= len(m.MatcherString[iNdEx]) + copy(dAtA[i:], m.MatcherString[iNdEx]) + i = encodeVarintRpc(dAtA, i, uint64(len(m.MatcherString[iNdEx]))) + i-- + dAtA[i] = 0x1a + } + } if m.PartialResponseStrategy != 0 { i = encodeVarintRpc(dAtA, i, uint64(m.PartialResponseStrategy)) i-- @@ -1299,6 +1310,12 @@ func (m *RulesRequest) Size() (n int) { if m.PartialResponseStrategy != 0 { n += 1 + sovRpc(uint64(m.PartialResponseStrategy)) } + if len(m.MatcherString) > 0 { + for _, s := range m.MatcherString { + l = len(s) + n += 1 + l + sovRpc(uint64(l)) + } + } return n } @@ -1602,6 +1619,38 @@ func (m *RulesRequest) Unmarshal(dAtA []byte) error { break } } + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field MatcherString", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowRpc + } + 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 ErrInvalidLengthRpc + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthRpc + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.MatcherString = append(m.MatcherString, string(dAtA[iNdEx:postIndex])) + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipRpc(dAtA[iNdEx:]) diff --git a/pkg/rules/rulespb/rpc.proto b/pkg/rules/rulespb/rpc.proto index 2049334610..db04f0d95e 100644 --- a/pkg/rules/rulespb/rpc.proto +++ b/pkg/rules/rulespb/rpc.proto @@ -40,6 +40,7 @@ message RulesRequest { } Type type = 1; PartialResponseStrategy partial_response_strategy = 2; + repeated string MatcherString = 3; } message RulesResponse { From 91002dc38bb7cf5da8a2541883b454b3395aa7d6 Mon Sep 17 00:00:00 2001 From: Saswata Mukherjee Date: Sat, 29 Jan 2022 20:38:34 +0530 Subject: [PATCH 2/5] Add CHANGELOG entry Signed-off-by: Saswata Mukherjee --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ada30b5b9..9b987de78d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re - [#4974](https://github.com/thanos-io/thanos/pull/4974) Store: Support tls_config configuration for connecting with Azure storage. - [#4999](https://github.com/thanos-io/thanos/pull/4999) COS: Support `endpoint` configuration for vpc internal endpoint. - [#5059](https://github.com/thanos-io/thanos/pull/5059) Compactor: Adding minimum retention flag validation for downsampling retention. +- [#5111](https://github.com/thanos-io/thanos/pull/5111) Add matcher support to Query Rules endpoint ### Fixed From 4a1dc372dc56bb9622ff7cd4efd3f8ea9b9082e9 Mon Sep 17 00:00:00 2001 From: Saswata Mukherjee Date: Mon, 31 Jan 2022 09:32:24 +0530 Subject: [PATCH 3/5] Add tests and implement suggestions Signed-off-by: Saswata Mukherjee --- CHANGELOG.md | 2 +- pkg/rules/rules.go | 2 +- pkg/rules/rules_test.go | 359 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 361 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b987de78d..80161807a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,7 +24,7 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re - [#4974](https://github.com/thanos-io/thanos/pull/4974) Store: Support tls_config configuration for connecting with Azure storage. - [#4999](https://github.com/thanos-io/thanos/pull/4999) COS: Support `endpoint` configuration for vpc internal endpoint. - [#5059](https://github.com/thanos-io/thanos/pull/5059) Compactor: Adding minimum retention flag validation for downsampling retention. -- [#5111](https://github.com/thanos-io/thanos/pull/5111) Add matcher support to Query Rules endpoint +- [#5111](https://github.com/thanos-io/thanos/pull/5111) Add matcher support to Query Rules endpoint. ### Fixed diff --git a/pkg/rules/rules.go b/pkg/rules/rules.go index e147f1b8c2..0f6040c9d0 100644 --- a/pkg/rules/rules.go +++ b/pkg/rules/rules.go @@ -87,7 +87,7 @@ func filterRules(ruleGroups []*rulespb.RuleGroup, matcherSets [][]*labels.Matche } for _, g := range ruleGroups { - filteredRules := []*rulespb.Rule{} + filteredRules := g.Rules[:0] for _, r := range g.Rules { rl := r.GetLabels() if matches(matcherSets, rl) { diff --git a/pkg/rules/rules_test.go b/pkg/rules/rules_test.go index 730ef42959..16daa33429 100644 --- a/pkg/rules/rules_test.go +++ b/pkg/rules/rules_test.go @@ -11,6 +11,7 @@ import ( "time" "github.com/gogo/protobuf/proto" + "github.com/prometheus/prometheus/model/labels" "github.com/prometheus/prometheus/storage" "github.com/thanos-io/thanos/pkg/rules/rulespb" @@ -954,3 +955,361 @@ func TestDedupGroups(t *testing.T) { }) } } + +func TestFilterRules(t *testing.T) { + for _, tc := range []struct { + name string + matcherSets [][]*labels.Matcher + groups, want []*rulespb.RuleGroup + }{ + { + name: "no groups", + groups: nil, + want: nil, + }, + { + name: "empty group with no matcher", + groups: []*rulespb.RuleGroup{ + {Name: "a"}, + }, + want: []*rulespb.RuleGroup{ + {Name: "a"}, + }, + }, + { + name: "multiple empty groups with no matcher", + groups: []*rulespb.RuleGroup{ + {Name: "a"}, + {Name: "b"}, + }, + want: []*rulespb.RuleGroup{ + {Name: "a"}, + {Name: "b"}, + }, + }, + { + name: "single group with labeled rules and no matcher", + groups: []*rulespb.RuleGroup{ + { + Name: "a", + Rules: []*rulespb.Rule{ + rulespb.NewAlertingRule(&rulespb.Alert{ + Name: "a1", + Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "replica", Value: "1"}, + {Name: "label", Value: "foo"}, + }}, + }), + rulespb.NewRecordingRule(&rulespb.RecordingRule{ + Name: "r1", Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "label", Value: "foo"}, + }}, + }), + rulespb.NewRecordingRule(&rulespb.RecordingRule{ + Name: "r2", Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "otherlabel", Value: "bar"}, + }}, + }), + }, + }, + }, + want: []*rulespb.RuleGroup{ + { + Name: "a", + Rules: []*rulespb.Rule{ + rulespb.NewAlertingRule(&rulespb.Alert{ + Name: "a1", + Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "replica", Value: "1"}, + {Name: "label", Value: "foo"}, + }}, + }), + rulespb.NewRecordingRule(&rulespb.RecordingRule{ + Name: "r1", Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "label", Value: "foo"}, + }}, + }), + rulespb.NewRecordingRule(&rulespb.RecordingRule{ + Name: "r2", Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "otherlabel", Value: "bar"}, + }}, + }), + }, + }, + }, + }, + { + name: "single group with labeled rules and matcher", + matcherSets: [][]*labels.Matcher{{&labels.Matcher{Name: "label", Value: "foo", Type: labels.MatchEqual}}}, + groups: []*rulespb.RuleGroup{ + { + Name: "a", + Rules: []*rulespb.Rule{ + rulespb.NewAlertingRule(&rulespb.Alert{ + Name: "a1", + Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "replica", Value: "1"}, + {Name: "label", Value: "foo"}, + }}, + }), + rulespb.NewRecordingRule(&rulespb.RecordingRule{ + Name: "r1", Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "label", Value: "foo"}, + }}, + }), + rulespb.NewRecordingRule(&rulespb.RecordingRule{ + Name: "r2", Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "otherlabel", Value: "bar"}, + }}, + }), + }, + }, + }, + want: []*rulespb.RuleGroup{ + { + Name: "a", + Rules: []*rulespb.Rule{ + rulespb.NewAlertingRule(&rulespb.Alert{ + Name: "a1", + Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "replica", Value: "1"}, + {Name: "label", Value: "foo"}, + }}, + }), + rulespb.NewRecordingRule(&rulespb.RecordingRule{ + Name: "r1", Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "label", Value: "foo"}, + }}, + }), + }, + }, + }, + }, + { + name: "single group with no match for matcher", + matcherSets: [][]*labels.Matcher{{&labels.Matcher{Name: "foo", Value: "bar", Type: labels.MatchEqual}}}, + groups: []*rulespb.RuleGroup{ + { + Name: "a", + Rules: []*rulespb.Rule{ + rulespb.NewAlertingRule(&rulespb.Alert{ + Name: "a1", + Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "replica", Value: "1"}, + {Name: "label", Value: "foo"}, + }}, + }), + rulespb.NewRecordingRule(&rulespb.RecordingRule{ + Name: "r1", Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "label", Value: "foo"}, + }}, + }), + rulespb.NewRecordingRule(&rulespb.RecordingRule{ + Name: "r2", Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "otherlabel", Value: "bar"}, + }}, + }), + }, + }, + }, + want: []*rulespb.RuleGroup{{ + Name: "a", + Rules: []*rulespb.Rule{}, + }}, + }, + { + name: "single group with templated labels", + matcherSets: [][]*labels.Matcher{{&labels.Matcher{Name: "templatedlabel", Value: "{{ $externalURL }}", Type: labels.MatchEqual}}}, + groups: []*rulespb.RuleGroup{ + { + Name: "a", + Rules: []*rulespb.Rule{ + rulespb.NewAlertingRule(&rulespb.Alert{ + Name: "a1", + Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "label", Value: "foo"}, + {Name: "templatedlabel", Value: "{{ $externalURL }}"}, + }}, + }), + rulespb.NewAlertingRule(&rulespb.Alert{ + Name: "a2", + Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "label", Value: "foo"}, + }}, + }), + }, + }, + }, + want: []*rulespb.RuleGroup{{ + Name: "a", + Rules: []*rulespb.Rule{}, + }}, + }, + { + name: "multiple group with labeled rules and matcher", + matcherSets: [][]*labels.Matcher{{&labels.Matcher{Name: "label", Value: "foo", Type: labels.MatchEqual}}}, + groups: []*rulespb.RuleGroup{ + { + Name: "a", + Rules: []*rulespb.Rule{ + rulespb.NewAlertingRule(&rulespb.Alert{ + Name: "a1a", + Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "label", Value: "foo"}, + }}, + }), + rulespb.NewRecordingRule(&rulespb.RecordingRule{ + Name: "r1a", Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "label", Value: "foo"}, + }}, + }), + rulespb.NewRecordingRule(&rulespb.RecordingRule{ + Name: "r1b", Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "otherlabel", Value: "bar"}, + }}, + }), + }, + }, + { + Name: "b", + Rules: []*rulespb.Rule{ + rulespb.NewAlertingRule(&rulespb.Alert{ + Name: "a1b", + Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "some", Value: "label"}, + }}, + }), + rulespb.NewRecordingRule(&rulespb.RecordingRule{ + Name: "r1b", Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "label", Value: "foo"}, + }}, + }), + }, + }, + }, + want: []*rulespb.RuleGroup{ + { + Name: "a", + Rules: []*rulespb.Rule{ + rulespb.NewAlertingRule(&rulespb.Alert{ + Name: "a1a", + Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "label", Value: "foo"}, + }}, + }), + rulespb.NewRecordingRule(&rulespb.RecordingRule{ + Name: "r1a", Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "label", Value: "foo"}, + }}, + }), + }, + }, + { + Name: "b", + Rules: []*rulespb.Rule{ + rulespb.NewRecordingRule(&rulespb.RecordingRule{ + Name: "r1b", Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "label", Value: "foo"}, + }}, + }), + }, + }, + }, + }, + { + name: "multiple group with labeled rules and no match", + matcherSets: [][]*labels.Matcher{{&labels.Matcher{Name: "foo", Value: "bar", Type: labels.MatchEqual}}}, + groups: []*rulespb.RuleGroup{ + { + Name: "a", + Rules: []*rulespb.Rule{ + rulespb.NewAlertingRule(&rulespb.Alert{ + Name: "a1a", + Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "label", Value: "foo"}, + }}, + }), + rulespb.NewRecordingRule(&rulespb.RecordingRule{ + Name: "r1a", Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "label", Value: "foo"}, + }}, + }), + rulespb.NewRecordingRule(&rulespb.RecordingRule{ + Name: "r1b", Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "otherlabel", Value: "bar"}, + }}, + }), + }, + }, + { + Name: "b", + Rules: []*rulespb.Rule{ + rulespb.NewAlertingRule(&rulespb.Alert{ + Name: "a1b", + Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "some", Value: "label"}, + }}, + }), + rulespb.NewRecordingRule(&rulespb.RecordingRule{ + Name: "r1b", Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "label", Value: "foo"}, + }}, + }), + }, + }, + }, + want: []*rulespb.RuleGroup{ + { + Name: "a", + Rules: []*rulespb.Rule{}, + }, + { + Name: "b", + Rules: []*rulespb.Rule{}, + }, + }, + }, + { + name: "multiple group with templated labels", + matcherSets: [][]*labels.Matcher{{&labels.Matcher{Name: "templatedlabel", Value: "{{ $externalURL }}", Type: labels.MatchEqual}}}, + groups: []*rulespb.RuleGroup{ + { + Name: "a", + Rules: []*rulespb.Rule{ + rulespb.NewAlertingRule(&rulespb.Alert{ + Name: "a1a", + Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "templatedlabel", Value: "{{ $externalURL }}"}, + }}, + }), + }, + }, + { + Name: "b", + Rules: []*rulespb.Rule{ + rulespb.NewAlertingRule(&rulespb.Alert{ + Name: "a1b", + Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "templated", Value: "{{ $externalURL }}"}, + }}, + }), + }, + }, + }, + want: []*rulespb.RuleGroup{ + { + Name: "a", + Rules: []*rulespb.Rule{}, + }, + { + Name: "b", + Rules: []*rulespb.Rule{}, + }, + }, + }, + } { + t.Run(tc.name, func(t *testing.T) { + testutil.Equals(t, tc.want, filterRules(tc.groups, tc.matcherSets)) + }) + } +} From 2120a1c75eab18103903cf08364f0fe90ef67be7 Mon Sep 17 00:00:00 2001 From: Saswata Mukherjee Date: Mon, 31 Jan 2022 16:12:50 +0530 Subject: [PATCH 4/5] Pre-allocate matcherSets Signed-off-by: Saswata Mukherjee --- pkg/rules/rules.go | 10 +-- pkg/rules/rulespb/rpc.pb.go | 132 ++++++++++++++++++------------------ pkg/rules/rulespb/rpc.proto | 2 +- 3 files changed, 72 insertions(+), 72 deletions(-) diff --git a/pkg/rules/rules.go b/pkg/rules/rules.go index 0f6040c9d0..cfcc3508a9 100644 --- a/pkg/rules/rules.go +++ b/pkg/rules/rules.go @@ -61,13 +61,13 @@ func (rr *GRPCClient) Rules(ctx context.Context, req *rulespb.RulesRequest) (*ru return nil, nil, errors.Wrap(err, "proxy Rules") } - var matcherSets [][]*labels.Matcher - for _, s := range req.MatcherString { - matchers, err := parser.ParseMetricSelector(s) + var err error + matcherSets := make([][]*labels.Matcher, len(req.MatcherString)) + for i, s := range req.MatcherString { + matcherSets[i], err = parser.ParseMetricSelector(s) if err != nil { - return nil, nil, errors.Wrap(err, "proxy Rules") + return nil, nil, errors.Wrap(err, "parser ParseMetricSelector") } - matcherSets = append(matcherSets, matchers) } resp.groups = filterRules(resp.groups, matcherSets) diff --git a/pkg/rules/rulespb/rpc.pb.go b/pkg/rules/rulespb/rpc.pb.go index 2c3f368d1a..2cbaff2c52 100644 --- a/pkg/rules/rulespb/rpc.pb.go +++ b/pkg/rules/rulespb/rpc.pb.go @@ -108,7 +108,7 @@ func (RulesRequest_Type) EnumDescriptor() ([]byte, []int) { type RulesRequest struct { Type RulesRequest_Type `protobuf:"varint,1,opt,name=type,proto3,enum=thanos.RulesRequest_Type" json:"type,omitempty"` PartialResponseStrategy storepb.PartialResponseStrategy `protobuf:"varint,2,opt,name=partial_response_strategy,json=partialResponseStrategy,proto3,enum=thanos.PartialResponseStrategy" json:"partial_response_strategy,omitempty"` - MatcherString []string `protobuf:"bytes,3,rep,name=MatcherString,proto3" json:"MatcherString,omitempty"` + MatcherString []string `protobuf:"bytes,3,rep,name=matcher_string,json=matcherString,proto3" json:"matcher_string,omitempty"` } func (m *RulesRequest) Reset() { *m = RulesRequest{} } @@ -553,71 +553,71 @@ func init() { func init() { proto.RegisterFile("rules/rulespb/rpc.proto", fileDescriptor_91b1d28f30eb5efb) } var fileDescriptor_91b1d28f30eb5efb = []byte{ - // 1021 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x56, 0x41, 0x6f, 0xdb, 0x36, - 0x14, 0x96, 0x22, 0x4b, 0xb6, 0x9e, 0x93, 0x34, 0x63, 0x1b, 0x44, 0x49, 0x07, 0x2b, 0x30, 0xd6, - 0x21, 0x1b, 0x56, 0x7b, 0x48, 0xd0, 0x0e, 0x3d, 0x0d, 0x51, 0x92, 0x35, 0x01, 0xb2, 0xac, 0xa0, - 0x83, 0x1d, 0xba, 0x83, 0xc7, 0x38, 0xac, 0x23, 0x40, 0x96, 0x54, 0x92, 0xce, 0x90, 0x1f, 0xb0, - 0x7b, 0xcf, 0xfb, 0x23, 0xfb, 0x0b, 0x39, 0xf6, 0xb8, 0x93, 0xb7, 0x25, 0x97, 0xc1, 0x87, 0xfd, - 0x86, 0x81, 0xa4, 0x64, 0x39, 0x9e, 0xb3, 0xb4, 0x9b, 0x77, 0xd1, 0x23, 0xdf, 0xfb, 0x1e, 0x45, - 0xbe, 0xf7, 0xf1, 0x93, 0x60, 0x85, 0xf5, 0x23, 0xca, 0x9b, 0xea, 0x99, 0x9e, 0x34, 0x59, 0xda, - 0x69, 0xa4, 0x2c, 0x11, 0x09, 0x72, 0xc4, 0x19, 0x89, 0x13, 0xbe, 0xb6, 0xca, 0x45, 0xc2, 0x68, - 0x53, 0x3d, 0xd3, 0x93, 0xa6, 0xb8, 0x48, 0x29, 0xd7, 0x90, 0x3c, 0x14, 0x91, 0x13, 0x1a, 0x4d, - 0x84, 0x1e, 0x74, 0x93, 0x6e, 0xa2, 0x86, 0x4d, 0x39, 0xca, 0xbc, 0x7e, 0x37, 0x49, 0xba, 0x11, - 0x6d, 0xaa, 0xd9, 0x49, 0xff, 0x55, 0x53, 0x84, 0x3d, 0xca, 0x05, 0xe9, 0xa5, 0x1a, 0x50, 0xff, - 0xc3, 0x84, 0x79, 0x2c, 0xb7, 0x82,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da, 0x50, 0x45, 0x64, 0x57, 0xbf, 0x55, - 0x39, 0xb0, 0x36, 0xff, 0xc4, 0x14, 0xe7, 0xff, 0x64, 0xca, 0x8f, 0x36, 0xd8, 0xaa, 0x1c, 0x45, - 0xb1, 0xcc, 0xf7, 0x28, 0x56, 0xae, 0x25, 0x73, 0x53, 0xb5, 0xc4, 0x07, 0xfb, 0x75, 0x9f, 0xb2, - 0x0b, 0x55, 0xff, 0xec, 0xd4, 0xca, 0x81, 0xb5, 0x41, 0x5f, 0xc0, 0xd2, 0xdf, 0xae, 0xfa, 0x98, - 0x4e, 0xe4, 0x31, 0x7c, 0xef, 0x74, 0xe2, 0x6a, 0x17, 0xf4, 0xb2, 0xff, 0x23, 0xbd, 0x9c, 0x7f, - 0x4f, 0xaf, 0x67, 0xe0, 0xa8, 0x8b, 0xc0, 0xbd, 0xb2, 0x52, 0xc3, 0xe5, 0x1b, 0x25, 0xcb, 0xaf, - 0x82, 0x56, 0x64, 0x0d, 0xc4, 0x99, 0x45, 0x75, 0x70, 0xce, 0x28, 0x89, 0xc4, 0x99, 0xd2, 0x01, - 0x57, 0x63, 0xb4, 0x07, 0x67, 0x16, 0x3d, 0x05, 0xd0, 0xf2, 0xc5, 0x58, 0xc2, 0x94, 0xc4, 0xb8, - 0xc1, 0xca, 0x70, 0xe0, 0xdf, 0x57, 0x2a, 0x24, 0x9d, 0x05, 0xdd, 0xb0, 0x3b, 0x72, 0xde, 0x25, - 0xa5, 0x30, 0x23, 0x29, 0xad, 0xce, 0x52, 0x4a, 0xeb, 0x3f, 0x59, 0xb0, 0x70, 0x43, 0x91, 0xee, - 0xf8, 0x4c, 0x8d, 0xa8, 0x35, 0x77, 0x0b, 0xb5, 0x0a, 0x86, 0x58, 0xef, 0xcb, 0x90, 0xa2, 0x39, - 0xa5, 0x77, 0x6c, 0x8e, 0x3d, 0xab, 0xe6, 0x38, 0x33, 0x6a, 0x4e, 0x79, 0x96, 0xcd, 0xf9, 0x74, - 0x0b, 0xa0, 0x50, 0x01, 0x34, 0x0f, 0x95, 0x83, 0xa3, 0xed, 0x9d, 0xe3, 0x83, 0x6f, 0xf7, 0x96, - 0x0c, 0x54, 0x85, 0xf2, 0x8b, 0xbd, 0xa3, 0xdd, 0x83, 0xa3, 0xe7, 0xfa, 0xdf, 0xe8, 0xab, 0x03, - 0x2c, 0xc7, 0x73, 0x9b, 0x5f, 0x82, 0xad, 0xfe, 0x8d, 0xd0, 0xd3, 0x7c, 0xf0, 0x60, 0xda, 0x9f, - 0xdf, 0xda, 0xf2, 0x84, 0x57, 0x0b, 0xd4, 0xe7, 0x66, 0xf0, 0xe8, 0xf2, 0xf7, 0x9a, 0x71, 0x79, - 0x55, 0x33, 0xdf, 0x5e, 0xd5, 0xcc, 0xdf, 0xae, 0x6a, 0xe6, 0x9b, 0xeb, 0x9a, 0xf1, 0xf6, 0xba, - 0x66, 0xfc, 0x72, 0x5d, 0x33, 0x5e, 0x96, 0xb3, 0xbf, 0xdd, 0x13, 0x47, 0x1d, 0x6e, 0xeb, 0xaf, - 0x00, 0x00, 0x00, 0xff, 0xff, 0xfc, 0x67, 0x13, 0xd9, 0x05, 0x0b, 0x00, 0x00, + // 1022 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x56, 0x41, 0x4f, 0xe3, 0x46, + 0x14, 0xb6, 0x71, 0xec, 0xc4, 0x2f, 0xc0, 0xd2, 0xd9, 0x45, 0x18, 0xb6, 0x8a, 0x51, 0x24, 0x2a, + 0x5a, 0x75, 0x93, 0x0a, 0xb4, 0x5b, 0xed, 0xa9, 0xc2, 0x40, 0x17, 0x24, 0x44, 0x57, 0x13, 0xd4, + 0xc3, 0xf6, 0x90, 0x0e, 0x61, 0x36, 0x58, 0x72, 0x6c, 0xef, 0xcc, 0x84, 0x8a, 0x1f, 0xd0, 0xfb, + 0x9e, 0xfb, 0x47, 0xfa, 0x17, 0x38, 0xee, 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f9, 0xee, 0xba, 0x66, 0xfe, 0x76, 0x5d, 0x33, 0xdf, 0xde, 0xd4, 0x8c, 0x77, + 0x37, 0x35, 0xe3, 0x97, 0x9b, 0x9a, 0xf1, 0xaa, 0x9c, 0xfd, 0xee, 0x9e, 0x3a, 0xea, 0x70, 0xdb, + 0x7f, 0x05, 0x00, 0x00, 0xff, 0xff, 0x3c, 0x4a, 0xb5, 0xe4, 0x06, 0x0b, 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. diff --git a/pkg/rules/rulespb/rpc.proto b/pkg/rules/rulespb/rpc.proto index db04f0d95e..c708a1e82e 100644 --- a/pkg/rules/rulespb/rpc.proto +++ b/pkg/rules/rulespb/rpc.proto @@ -40,7 +40,7 @@ message RulesRequest { } Type type = 1; PartialResponseStrategy partial_response_strategy = 2; - repeated string MatcherString = 3; + repeated string matcher_string = 3; } message RulesResponse { From 9e25ab21341cb94c9f5794f47de608c104bda827 Mon Sep 17 00:00:00 2001 From: Saswata Mukherjee Date: Thu, 3 Feb 2022 09:06:13 +0530 Subject: [PATCH 5/5] Add other matchtype testcase Signed-off-by: Saswata Mukherjee --- pkg/api/query/v1.go | 2 +- pkg/rules/rules.go | 2 +- pkg/rules/rules_test.go | 109 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 2 deletions(-) diff --git a/pkg/api/query/v1.go b/pkg/api/query/v1.go index 5ac8bbad32..5da44c7a2d 100644 --- a/pkg/api/query/v1.go +++ b/pkg/api/query/v1.go @@ -788,7 +788,7 @@ func NewRulesHandler(client rules.UnaryClient, enablePartialResponse bool) func( } if err := r.ParseForm(); err != nil { - return nil, nil, &api.ApiError{Typ: api.ErrorInternal, Err: errors.Errorf("error parsing request form='%v'", typeParam)} + return nil, nil, &api.ApiError{Typ: api.ErrorInternal, Err: errors.Errorf("error parsing request form='%v'", MatcherParam)} } // TODO(bwplotka): Allow exactly the same functionality as query API: passing replica, dedup and partial response as HTTP params as well. diff --git a/pkg/rules/rules.go b/pkg/rules/rules.go index cfcc3508a9..dbe99cfd48 100644 --- a/pkg/rules/rules.go +++ b/pkg/rules/rules.go @@ -82,7 +82,7 @@ func (rr *GRPCClient) Rules(ctx context.Context, req *rulespb.RulesRequest) (*ru // filterRules filters rules in a group according to given matcherSets. func filterRules(ruleGroups []*rulespb.RuleGroup, matcherSets [][]*labels.Matcher) []*rulespb.RuleGroup { - if len(matcherSets) == 0 || len(ruleGroups) == 0 { + if len(matcherSets) == 0 { return ruleGroups } diff --git a/pkg/rules/rules_test.go b/pkg/rules/rules_test.go index 16daa33429..5e2f90fe59 100644 --- a/pkg/rules/rules_test.go +++ b/pkg/rules/rules_test.go @@ -1307,6 +1307,115 @@ func TestFilterRules(t *testing.T) { }, }, }, + { + name: "multiple group with templated labels and non templated matcher", + matcherSets: [][]*labels.Matcher{{&labels.Matcher{Name: "templatedlabel", Value: "foo", Type: labels.MatchEqual}}}, + groups: []*rulespb.RuleGroup{ + { + Name: "a", + Rules: []*rulespb.Rule{ + rulespb.NewAlertingRule(&rulespb.Alert{ + Name: "a1a", + Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "templatedlabel", Value: "{{ $externalURL }}"}, + }}, + }), + }, + }, + { + Name: "b", + Rules: []*rulespb.Rule{ + rulespb.NewAlertingRule(&rulespb.Alert{ + Name: "a1b", + Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "templated", Value: "{{ $externalURL }}"}, + }}, + }), + }, + }, + }, + want: []*rulespb.RuleGroup{ + { + Name: "a", + Rules: []*rulespb.Rule{}, + }, + { + Name: "b", + Rules: []*rulespb.Rule{}, + }, + }, + }, + { + name: "multiple group with regex matcher", + matcherSets: [][]*labels.Matcher{{labels.MustNewMatcher(labels.MatchRegexp, "label", "f.*")}}, + groups: []*rulespb.RuleGroup{ + { + Name: "a", + Rules: []*rulespb.Rule{ + rulespb.NewAlertingRule(&rulespb.Alert{ + Name: "a1a", + Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "label", Value: "foo"}, + }}, + }), + rulespb.NewRecordingRule(&rulespb.RecordingRule{ + Name: "r1a", Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "label", Value: "foo"}, + }}, + }), + rulespb.NewRecordingRule(&rulespb.RecordingRule{ + Name: "r1b", Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "otherlabel", Value: "bar"}, + }}, + }), + }, + }, + { + Name: "b", + Rules: []*rulespb.Rule{ + rulespb.NewAlertingRule(&rulespb.Alert{ + Name: "a1b", + Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "some", Value: "label"}, + }}, + }), + rulespb.NewRecordingRule(&rulespb.RecordingRule{ + Name: "r1b", Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "label", Value: "foo"}, + }}, + }), + }, + }, + }, + want: []*rulespb.RuleGroup{ + { + Name: "a", + Rules: []*rulespb.Rule{ + rulespb.NewAlertingRule(&rulespb.Alert{ + Name: "a1a", + Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "label", Value: "foo"}, + }}, + }), + rulespb.NewRecordingRule(&rulespb.RecordingRule{ + Name: "r1a", Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "label", Value: "foo"}, + }}, + }), + }, + }, + { + Name: "b", + Rules: []*rulespb.Rule{ + rulespb.NewRecordingRule(&rulespb.RecordingRule{ + Name: "r1b", Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ + {Name: "label", Value: "foo"}, + }}, + }), + }, + }, + }, + }, } { t.Run(tc.name, func(t *testing.T) { testutil.Equals(t, tc.want, filterRules(tc.groups, tc.matcherSets))