Skip to content

Commit acc552c

Browse files
authoredMay 17, 2023
Merge pull request #735 from pyrra-dev/release-0.6-queries-grouping-fix
Fix adding label matchers for groups if label not in query
2 parents 8190282 + be95130 commit acc552c

File tree

1 file changed

+42
-9
lines changed

1 file changed

+42
-9
lines changed
 

‎main.go

+42-9
Original file line numberDiff line numberDiff line change
@@ -590,46 +590,79 @@ func (s *objectiveServer) List(ctx context.Context, req *connect.Request[objecti
590590

591591
// If specific grouping was selected we need to merge the label matchers for the queries.
592592
if len(groupingMatchers) > 0 {
593-
if oi.Indicator.Ratio != nil {
593+
switch oi.IndicatorType() {
594+
case slo.Ratio:
595+
groupingMatchersErrors := make(map[string]*labels.Matcher, len(groupingMatchers))
596+
groupingMatchersTotal := make(map[string]*labels.Matcher, len(groupingMatchers))
597+
for _, matcher := range groupingMatchers {
598+
// We need to copy the matchers to avoid modifying the original later on.
599+
groupingMatchersErrors[matcher.Name] = &labels.Matcher{Type: matcher.Type, Name: matcher.Name, Value: matcher.Value}
600+
groupingMatchersTotal[matcher.Name] = &labels.Matcher{Type: matcher.Type, Name: matcher.Name, Value: matcher.Value}
601+
}
602+
594603
for _, m := range oi.Indicator.Ratio.Errors.LabelMatchers {
595604
if rm, replace := groupingMatchers[m.Name]; replace {
596605
m.Type = rm.Type
597606
m.Value = rm.Value
607+
delete(groupingMatchersErrors, m.Name)
598608
}
599609
}
600610
for _, m := range oi.Indicator.Ratio.Total.LabelMatchers {
601611
if rm, replace := groupingMatchers[m.Name]; replace {
602612
m.Type = rm.Type
603613
m.Value = rm.Value
614+
delete(groupingMatchersTotal, m.Name)
604615
}
605616
}
606-
}
607-
if oi.Indicator.Latency != nil {
617+
618+
// Now add the remaining matchers we didn't find before.
619+
for _, m := range groupingMatchersErrors {
620+
oi.Indicator.Ratio.Errors.LabelMatchers = append(oi.Indicator.Ratio.Errors.LabelMatchers, m)
621+
}
622+
for _, m := range groupingMatchersTotal {
623+
oi.Indicator.Ratio.Total.LabelMatchers = append(oi.Indicator.Ratio.Total.LabelMatchers, m)
624+
}
625+
case slo.Latency:
626+
groupingMatchersSuccess := make(map[string]*labels.Matcher, len(groupingMatchers))
627+
groupingMatchersTotal := make(map[string]*labels.Matcher, len(groupingMatchers))
628+
for _, matcher := range groupingMatchers {
629+
// We need to copy the matchers to avoid modifying the original later on.
630+
groupingMatchersSuccess[matcher.Name] = &labels.Matcher{Type: matcher.Type, Name: matcher.Name, Value: matcher.Value}
631+
groupingMatchersTotal[matcher.Name] = &labels.Matcher{Type: matcher.Type, Name: matcher.Name, Value: matcher.Value}
632+
}
633+
608634
for _, m := range oi.Indicator.Latency.Success.LabelMatchers {
609635
if rm, replace := groupingMatchers[m.Name]; replace {
610636
m.Type = rm.Type
611637
m.Value = rm.Value
638+
delete(groupingMatchersSuccess, m.Name)
612639
}
613640
}
614641
for _, m := range oi.Indicator.Latency.Total.LabelMatchers {
615642
if rm, replace := groupingMatchers[m.Name]; replace {
616643
m.Type = rm.Type
617644
m.Value = rm.Value
645+
delete(groupingMatchersTotal, m.Name)
618646
}
619647
}
620-
}
621-
if oi.Indicator.BoolGauge != nil {
648+
649+
// Now add the remaining matchers we didn't find before.
650+
for _, m := range groupingMatchersSuccess {
651+
oi.Indicator.Latency.Success.LabelMatchers = append(oi.Indicator.Latency.Success.LabelMatchers, m)
652+
}
653+
for _, m := range groupingMatchersTotal {
654+
oi.Indicator.Latency.Total.LabelMatchers = append(oi.Indicator.Latency.Total.LabelMatchers, m)
655+
}
656+
case slo.BoolGauge:
622657
for _, m := range oi.Indicator.BoolGauge.LabelMatchers {
623658
if rm, replace := groupingMatchers[m.Name]; replace {
624659
m.Type = rm.Type
625660
m.Value = rm.Value
626661
delete(groupingMatchers, m.Name)
627662
}
628663
}
629-
if len(groupingMatchers) > 0 {
630-
for _, m := range groupingMatchers {
631-
oi.Indicator.BoolGauge.LabelMatchers = append(oi.Indicator.BoolGauge.LabelMatchers, m)
632-
}
664+
for _, m := range groupingMatchers {
665+
oi.Indicator.BoolGauge.LabelMatchers = append(oi.Indicator.BoolGauge.LabelMatchers, m)
633666
}
634667
}
635668
}

0 commit comments

Comments
 (0)
Please sign in to comment.