From ea5400d13bde23b5af871f7ce7c364408b4d41e1 Mon Sep 17 00:00:00 2001 From: sugaf1204 Date: Wed, 3 Dec 2025 03:11:35 +0900 Subject: [PATCH] feat(ruler): add labels support to rule groups --- pkg/ruler/base/manager.go | 10 ++ pkg/ruler/compat.go | 10 ++ pkg/ruler/rulespb/compat.go | 2 + pkg/ruler/rulespb/compat_test.go | 35 ++++++ pkg/ruler/rulespb/rules.pb.go | 203 ++++++++++++++++++------------- pkg/ruler/rulespb/rules.proto | 4 + pkg/tool/rules/parser_test.go | 40 ++++++ pkg/tool/rules/rules.go | 10 ++ 8 files changed, 229 insertions(+), 85 deletions(-) create mode 100644 pkg/ruler/rulespb/compat_test.go diff --git a/pkg/ruler/base/manager.go b/pkg/ruler/base/manager.go index 1d38b54fb7d64..a369bdb2142d0 100644 --- a/pkg/ruler/base/manager.go +++ b/pkg/ruler/base/manager.go @@ -301,6 +301,16 @@ func (*DefaultMultiTenantManager) ValidateRuleGroup(g rulefmt.RuleGroup) []error return errs } + for k, v := range g.Labels { + if !model.LabelName(k).IsValid() || k == model.MetricNameLabel { + errs = append(errs, errors.Errorf("invalid label name: %s", k)) + } + + if !model.LabelValue(v).IsValid() { + errs = append(errs, errors.Errorf("invalid label value: %s", v)) + } + } + for i, r := range g.Rules { ruleNode := rulefmt.RuleNode{ Record: yaml.Node{Value: r.Record}, diff --git a/pkg/ruler/compat.go b/pkg/ruler/compat.go index ada27fbc74e73..05766487abcf9 100644 --- a/pkg/ruler/compat.go +++ b/pkg/ruler/compat.go @@ -233,6 +233,16 @@ func ValidateGroups(grps ...rulefmt.RuleGroup) (errs []error) { ) } + for k, v := range g.Labels { + if !model.LabelName(k).IsValid() || k == model.MetricNameLabel { + errs = append(errs, errors.Errorf("invalid label name: %s", k)) + } + + if !model.LabelValue(v).IsValid() { + errs = append(errs, errors.Errorf("invalid label value: %s", v)) + } + } + set[g.Name] = struct{}{} for _, r := range g.Rules { diff --git a/pkg/ruler/rulespb/compat.go b/pkg/ruler/rulespb/compat.go index dc1f4dd4a3ab9..75aa28885129a 100644 --- a/pkg/ruler/rulespb/compat.go +++ b/pkg/ruler/rulespb/compat.go @@ -19,6 +19,7 @@ func ToProto(user string, namespace string, rl rulefmt.RuleGroup) *RuleGroupDesc Rules: formattedRuleToProto(rl.Rules), User: user, Limit: int64(rl.Limit), + Labels: logproto.FromLabelsToLabelAdapters(labels.FromMap(rl.Labels)), } return &rg } @@ -46,6 +47,7 @@ func FromProto(rg *RuleGroupDesc) rulefmt.RuleGroup { Interval: model.Duration(rg.Interval), Rules: make([]rulefmt.Rule, len(rg.GetRules())), Limit: int(rg.GetLimit()), + Labels: logproto.FromLabelAdaptersToLabels(rg.Labels).Map(), } for i, rl := range rg.GetRules() { diff --git a/pkg/ruler/rulespb/compat_test.go b/pkg/ruler/rulespb/compat_test.go new file mode 100644 index 0000000000000..be846ba559787 --- /dev/null +++ b/pkg/ruler/rulespb/compat_test.go @@ -0,0 +1,35 @@ +package rulespb + +import ( + "testing" + + "github.com/prometheus/prometheus/model/rulefmt" + "github.com/stretchr/testify/require" + + "github.com/grafana/loki/v3/pkg/logproto" +) + +func TestRuleGroupRoundTripIncludesGroupLabels(t *testing.T) { + group := rulefmt.RuleGroup{ + Name: "group", + Labels: map[string]string{ + "foo": "bar", + "shared": "group", + }, + Rules: []rulefmt.Rule{{ + Alert: "AlertName", + Expr: "vector(1)", + Labels: map[string]string{ + "shared": "rule", + }, + }}, + } + + proto := ToProto("user", "namespace", group) + require.Equal(t, group.Labels, logproto.FromLabelAdaptersToLabels(proto.Labels).Map()) + require.Equal(t, group.Rules[0].Labels, logproto.FromLabelAdaptersToLabels(proto.Rules[0].Labels).Map()) + + roundTrip := FromProto(proto) + require.Equal(t, group.Labels, roundTrip.Labels) + require.Equal(t, group.Rules[0].Labels, roundTrip.Rules[0].Labels) +} diff --git a/pkg/ruler/rulespb/rules.pb.go b/pkg/ruler/rulespb/rules.pb.go index 3765e9dd88a7b..4dffe66410c88 100644 --- a/pkg/ruler/rulespb/rules.pb.go +++ b/pkg/ruler/rulespb/rules.pb.go @@ -43,8 +43,9 @@ type RuleGroupDesc struct { // having to repeatedly redefine the proto description. It can also be leveraged // to create custom `ManagerOpts` based on rule configs which can then be passed // to the Prometheus Manager. - Options []*types.Any `protobuf:"bytes,9,rep,name=options,proto3" json:"options,omitempty"` - Limit int64 `protobuf:"varint,10,opt,name=limit,proto3" json:"limit,omitempty"` + Options []*types.Any `protobuf:"bytes,9,rep,name=options,proto3" json:"options,omitempty"` + Limit int64 `protobuf:"varint,10,opt,name=limit,proto3" json:"limit,omitempty"` + Labels []github_com_grafana_loki_v3_pkg_logproto.LabelAdapter `protobuf:"bytes,11,rep,name=labels,proto3,customtype=github.com/grafana/loki/v3/pkg/logproto.LabelAdapter" json:"labels"` } func (m *RuleGroupDesc) Reset() { *m = RuleGroupDesc{} } @@ -206,39 +207,39 @@ func init() { func init() { proto.RegisterFile("pkg/ruler/rulespb/rules.proto", fileDescriptor_dd3ef3757f506fba) } var fileDescriptor_dd3ef3757f506fba = []byte{ - // 503 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x53, 0x31, 0x6f, 0xd3, 0x40, - 0x18, 0xf5, 0x25, 0x8e, 0x6b, 0x5f, 0x54, 0x11, 0x9d, 0x22, 0xe4, 0x14, 0xb8, 0x44, 0x95, 0x90, + // 510 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xbc, 0x53, 0x31, 0x6f, 0xd3, 0x40, + 0x14, 0xf6, 0xd5, 0x8e, 0x6b, 0x5f, 0x54, 0x11, 0x9d, 0x22, 0xe4, 0x14, 0xb8, 0x44, 0x95, 0x90, 0x32, 0x20, 0x5b, 0x6a, 0x61, 0x43, 0x42, 0x8d, 0x2a, 0x21, 0x45, 0x1d, 0x90, 0x47, 0xb6, 0xb3,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x09, 0x3d, 0x86, 0x66, 0x76, 0xdf, 0x9e, 0x74, 0xa7, 0xfd, - 0xa3, 0x7b, 0x81, 0x89, 0x45, 0xf9, 0x52, 0x96, 0x22, 0xc3, 0x2a, 0x67, 0x55, 0x41, 0xa5, 0xef, - 0x18, 0x67, 0xaa, 0x46, 0x01, 0xdc, 0x13, 0xb9, 0xba, 0xb8, 0xf0, 0x3d, 0xdd, 0x3c, 0xfc, 0x43, - 0xfa, 0x24, 0x5b, 0x47, 0xed, 0x21, 0x34, 0x84, 0x3d, 0x9e, 0xae, 0xd2, 0xd2, 0x87, 0x13, 0x30, - 0xed, 0x46, 0x06, 0xcc, 0x6d, 0xb7, 0x37, 0x70, 0xe6, 0xb6, 0xbb, 0x37, 0x70, 0xe7, 0xb6, 0xeb, - 0x0e, 0xbc, 0xc3, 0xef, 0x1d, 0xe8, 0xb6, 0xfa, 0x4a, 0x98, 0xbe, 0xcd, 0x65, 0x1b, 0x89, 0xaa, - 0xd1, 0x7d, 0xe8, 0x48, 0x9a, 0x08, 0xb9, 0x68, 0xf2, 0x68, 0x90, 0x12, 0x20, 0x9c, 0xca, 0x52, - 0x27, 0xe1, 0x45, 0x06, 0xa0, 0x67, 0xb0, 0xbb, 0x14, 0xd2, 0xb7, 0xff, 0x3d, 0x1d, 0x75, 0x1e, - 0x09, 0xe8, 0x70, 0x12, 0x53, 0x5e, 0xf8, 0x3d, 0x3d, 0xdc, 0x28, 0xb8, 0x7d, 0x7d, 0x67, 0x94, - 0x91, 0x64, 0x7d, 0xa6, 0xd8, 0x57, 0x24, 0x95, 0xb3, 0xe7, 0xaa, 0xf3, 0xf3, 0xf5, 0xf8, 0x29, - 0x4b, 0xcb, 0x37, 0x55, 0x1c, 0x24, 0x62, 0x15, 0x32, 0x49, 0x96, 0x24, 0x23, 0x21, 0x17, 0xe7, - 0x69, 0x78, 0x71, 0x1c, 0xde, 0xfd, 0x10, 0x02, 0xdd, 0x7a, 0xb2, 0x20, 0x79, 0x49, 0x65, 0xd4, - 0xc8, 0xa0, 0x35, 0xec, 0x93, 0x2c, 0x13, 0x25, 0x31, 0x91, 0x3a, 0xff, 0x57, 0xf5, 0xae, 0x96, - 0x4e, 0x7f, 0x7f, 0x16, 0x6f, 0xb6, 0xd8, 0xba, 0xda, 0x62, 0xeb, 0x66, 0x8b, 0xc1, 0xbb, 0x1a, - 0x83, 0x4f, 0x35, 0x06, 0x97, 0x35, 0x06, 0x9b, 0x1a, 0x83, 0xaf, 0x35, 0x06, 0xdf, 0x6a, 0x6c, - 0xdd, 0xd4, 0x18, 0xbc, 0xdf, 0x61, 0x6b, 0xb3, 0xc3, 0xd6, 0xd5, 0x0e, 0x5b, 0xaf, 0x9f, 0xfc, - 0x45, 0xfe, 0x97, 0x9f, 0x2e, 0x76, 0xb4, 0x95, 0xe3, 0x1f, 0x01, 0x00, 0x00, 0xff, 0xff, 0xab, - 0x30, 0xeb, 0xc9, 0x90, 0x03, 0x00, 0x00, + 0x73, 0x31, 0x56, 0x2f, 0x3e, 0xeb, 0x6c, 0x57, 0x64, 0x63, 0x62, 0x66, 0xe4, 0x27, 0xf0, 0x53, + 0x3a, 0x66, 0xac, 0x18, 0x0a, 0x71, 0x16, 0xc6, 0x2e, 0xec, 0xe8, 0xee, 0xec, 0x10, 0x60, 0x80, + 0x85, 0x2e, 0xbe, 0xf7, 0xee, 0xbb, 0xf7, 0xbe, 0xef, 0x7d, 0x4f, 0x86, 0x8f, 0xf2, 0x8b, 0x24, + 0x10, 0x15, 0xa3, 0x42, 0x7d, 0x8b, 0x3c, 0xd2, 0xa7, 0x9f, 0x0b, 0x5e, 0x72, 0xd4, 0x51, 0xc9, + 0x61, 0x3f, 0xe1, 0x09, 0x57, 0x37, 0x81, 0x8c, 0x34, 0x78, 0x38, 0x48, 0x38, 0x4f, 0x18, 0x0d, + 0x54, 0x16, 0x55, 0xf3, 0x80, 0x64, 0xcb, 0x06, 0xc2, 0xbf, 0x43, 0xb3, 0x4a, 0x90, 0x32, 0xe5,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00, 0x00, } func (this *RuleGroupDesc) Equal(that interface{}) bool { @@ -291,6 +292,14 @@ func (this *RuleGroupDesc) Equal(that interface{}) bool { if this.Limit != that1.Limit { return false } + if len(this.Labels) != len(that1.Labels) { + return false + } + for i := range this.Labels { + if !this.Labels[i].Equal(that1.Labels[i]) { + return false + } + } return true } func (this *RuleDesc) Equal(that interface{}) bool { @@ -346,7 +355,7 @@ func (this *RuleGroupDesc) GoString() string { if this == nil { return "nil" } - s := make([]string, 0, 11) + s := make([]string, 0, 12) s = append(s, "&rulespb.RuleGroupDesc{") s = append(s, "Name: "+fmt.Sprintf("%#v", this.Name)+",\n") s = append(s, "Namespace: "+fmt.Sprintf("%#v", this.Namespace)+",\n") @@ -359,6 +368,7 @@ func (this *RuleGroupDesc) GoString() string { s = append(s, "Options: "+fmt.Sprintf("%#v", this.Options)+",\n") } s = append(s, "Limit: "+fmt.Sprintf("%#v", this.Limit)+",\n") + s = append(s, "Labels: "+fmt.Sprintf("%#v", this.Labels)+",\n") s = append(s, "}") return strings.Join(s, "") } @@ -405,6 +415,20 @@ func (m *RuleGroupDesc) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if len(m.Labels) > 0 { + for iNdEx := len(m.Labels) - 1; iNdEx >= 0; iNdEx-- { + { + size := m.Labels[iNdEx].Size() + i -= size + if _, err := m.Labels[iNdEx].MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintRules(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x5a + } + } if m.Limit != 0 { i = encodeVarintRules(dAtA, i, uint64(m.Limit)) i-- @@ -596,6 +620,12 @@ func (m *RuleGroupDesc) Size() (n int) { if m.Limit != 0 { n += 1 + sovRules(uint64(m.Limit)) } + if len(m.Labels) > 0 { + for _, e := range m.Labels { + l = e.Size() + n += 1 + l + sovRules(uint64(l)) + } + } return n } @@ -662,6 +692,7 @@ func (this *RuleGroupDesc) String() string { `User:` + fmt.Sprintf("%v", this.User) + `,`, `Options:` + repeatedStringForOptions + `,`, `Limit:` + fmt.Sprintf("%v", this.Limit) + `,`, + `Labels:` + fmt.Sprintf("%v", this.Labels) + `,`, `}`, }, "") return s @@ -934,16 +965,47 @@ func (m *RuleGroupDesc) Unmarshal(dAtA []byte) error { break } } + case 11: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Labels", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowRules + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthRules + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthRules + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Labels = append(m.Labels, github_com_grafana_loki_v3_pkg_logproto.LabelAdapter{}) + if err := m.Labels[len(m.Labels)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipRules(dAtA[iNdEx:]) if err != nil { return err } - if skippy < 0 { - return ErrInvalidLengthRules - } - if (iNdEx + skippy) < 0 { + if (skippy < 0) || (iNdEx+skippy) < 0 { return ErrInvalidLengthRules } if (iNdEx + skippy) > l { @@ -1190,10 +1252,7 @@ func (m *RuleDesc) Unmarshal(dAtA []byte) error { if err != nil { return err } - if skippy < 0 { - return ErrInvalidLengthRules - } - if (iNdEx + skippy) < 0 { + if (skippy < 0) || (iNdEx+skippy) < 0 { return ErrInvalidLengthRules } if (iNdEx + skippy) > l { @@ -1211,6 +1270,7 @@ func (m *RuleDesc) Unmarshal(dAtA []byte) error { func skipRules(dAtA []byte) (n int, err error) { l := len(dAtA) iNdEx := 0 + depth := 0 for iNdEx < l { var wire uint64 for shift := uint(0); ; shift += 7 { @@ -1242,10 +1302,8 @@ func skipRules(dAtA []byte) (n int, err error) { break } } - return iNdEx, nil case 1: iNdEx += 8 - return iNdEx, nil case 2: var length int for shift := uint(0); ; shift += 7 { @@ -1266,55 +1324,30 @@ func skipRules(dAtA []byte) (n int, err error) { return 0, ErrInvalidLengthRules } iNdEx += length - if iNdEx < 0 { - return 0, ErrInvalidLengthRules - } - return iNdEx, nil case 3: - for { - var innerWire uint64 - var start int = iNdEx - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return 0, ErrIntOverflowRules - } - if iNdEx >= l { - return 0, io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - innerWire |= (uint64(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - innerWireType := int(innerWire & 0x7) - if innerWireType == 4 { - break - } - next, err := skipRules(dAtA[start:]) - if err != nil { - return 0, err - } - iNdEx = start + next - if iNdEx < 0 { - return 0, ErrInvalidLengthRules - } - } - return iNdEx, nil + depth++ case 4: - return iNdEx, nil + if depth == 0 { + return 0, ErrUnexpectedEndOfGroupRules + } + depth-- case 5: iNdEx += 4 - return iNdEx, nil default: return 0, fmt.Errorf("proto: illegal wireType %d", wireType) } + if iNdEx < 0 { + return 0, ErrInvalidLengthRules + } + if depth == 0 { + return iNdEx, nil + } } - panic("unreachable") + return 0, io.ErrUnexpectedEOF } var ( - ErrInvalidLengthRules = fmt.Errorf("proto: negative length found during unmarshaling") - ErrIntOverflowRules = fmt.Errorf("proto: integer overflow") + ErrInvalidLengthRules = fmt.Errorf("proto: negative length found during unmarshaling") + ErrIntOverflowRules = fmt.Errorf("proto: integer overflow") + ErrUnexpectedEndOfGroupRules = fmt.Errorf("proto: unexpected end of group") ) diff --git a/pkg/ruler/rulespb/rules.proto b/pkg/ruler/rulespb/rules.proto index 82dc7daabd56a..170668ccf2817 100644 --- a/pkg/ruler/rulespb/rules.proto +++ b/pkg/ruler/rulespb/rules.proto @@ -28,6 +28,10 @@ message RuleGroupDesc { // to the Prometheus Manager. repeated google.protobuf.Any options = 9; int64 limit = 10; + repeated logproto.LegacyLabelPair labels = 11 [ + (gogoproto.nullable) = false, + (gogoproto.customtype) = "github.com/grafana/loki/v3/pkg/logproto.LabelAdapter" + ]; } // RuleDesc is a proto representation of a Prometheus Rule diff --git a/pkg/tool/rules/parser_test.go b/pkg/tool/rules/parser_test.go index 7348f39197e39..e30c25787969c 100644 --- a/pkg/tool/rules/parser_test.go +++ b/pkg/tool/rules/parser_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/prometheus/prometheus/model/rulefmt" + "github.com/stretchr/testify/require" "github.com/grafana/loki/v3/pkg/tool/rules/rwrulefmt" ) @@ -161,3 +162,42 @@ func compareNamespace(g, w RuleNamespace) error { return nil } + +func TestParseBytes_GroupLabels(t *testing.T) { + content := []byte(` +groups: +- name: labeled + labels: + foo: bar + shared: group + rules: + - alert: TestAlert + expr: vector(1) + labels: + shared: rule +`) + + namespaces, errs := ParseBytes(content) + require.Empty(t, errs) + require.Len(t, namespaces, 1) + require.Len(t, namespaces[0].Groups, 1) + + group := namespaces[0].Groups[0] + require.Equal(t, map[string]string{"foo": "bar", "shared": "group"}, group.Labels) + require.Equal(t, "rule", group.Rules[0].Labels["shared"]) +} + +func TestParseBytes_InvalidGroupLabels(t *testing.T) { + content := []byte(` +groups: +- name: labeled + labels: + __name__: invalid + rules: + - alert: TestAlert + expr: vector(1) +`) + + _, errs := ParseBytes(content) + require.NotEmpty(t, errs) +} diff --git a/pkg/tool/rules/rules.go b/pkg/tool/rules/rules.go index adc5f63f5d4d9..0deb28263ad7c 100644 --- a/pkg/tool/rules/rules.go +++ b/pkg/tool/rules/rules.go @@ -240,6 +240,16 @@ func (r RuleNamespace) Validate() []error { // ValidateRuleGroup validates a rulegroup func ValidateRuleGroup(g rwrulefmt.RuleGroup) []error { var errs []error + for k, v := range g.Labels { + if !model.LabelName(k).IsValid() || k == model.MetricNameLabel { + errs = append(errs, fmt.Errorf("invalid label name: %s", k)) + } + + if !model.LabelValue(v).IsValid() { + errs = append(errs, fmt.Errorf("invalid label value: %s", v)) + } + } + for i, r := range g.Rules { ruleNode := rulefmt.RuleNode{ Record: yaml.Node{Value: r.Record},