From 505904f77b8420633d6840cb111ef70c2de66159 Mon Sep 17 00:00:00 2001 From: shantanualshi Date: Thu, 4 Apr 2024 14:59:24 +0530 Subject: [PATCH] Bugfixes for detected labels API --- pkg/ingester/ingester.go | 4 ++-- pkg/ingester/instance.go | 2 -- pkg/querier/ingester_querier.go | 19 +++++++++++++------ pkg/querier/querier.go | 8 +++++++- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/pkg/ingester/ingester.go b/pkg/ingester/ingester.go index 244cd3b50d60e..937eceaa683e1 100644 --- a/pkg/ingester/ingester.go +++ b/pkg/ingester/ingester.go @@ -1389,7 +1389,7 @@ func (i *Ingester) GetDetectedLabels(ctx context.Context, req *logproto.Detected } var matchers []*labels.Matcher if req.Query != "" { - matchers, err := syntax.ParseMatchers(req.Query, true) + matchers, err = syntax.ParseMatchers(req.Query, true) if err != nil { return nil, err } @@ -1403,7 +1403,7 @@ func (i *Ingester) GetDetectedLabels(ctx context.Context, req *logproto.Detected } result := make(map[string]*logproto.UniqueLabelValues) for label, values := range labelMap { - uniqueValues := make([]string, len(values)) + var uniqueValues []string for v := range values { uniqueValues = append(uniqueValues, v) } diff --git a/pkg/ingester/instance.go b/pkg/ingester/instance.go index 5463eb16532b2..99bf587b21e85 100644 --- a/pkg/ingester/instance.go +++ b/pkg/ingester/instance.go @@ -564,8 +564,6 @@ func (i *instance) Label(ctx context.Context, req *logproto.LabelRequest, matche } labels := util.NewUniqueStrings(0) - // (shantanu) can create a map here to store label names::values and count the unique values - // just return a string to int map err := i.forMatchingStreams(ctx, *req.Start, matchers, nil, func(s *stream) error { for _, label := range s.labels { if req.Values && label.Name == req.Name { diff --git a/pkg/querier/ingester_querier.go b/pkg/querier/ingester_querier.go index 68d88bbff4508..8b5b4d14a729e 100644 --- a/pkg/querier/ingester_querier.go +++ b/pkg/querier/ingester_querier.go @@ -370,17 +370,24 @@ func (q *IngesterQuerier) DetectedLabel(ctx context.Context, req *logproto.Detec labelMap := make(map[string][]string) for _, resp := range ingesterResponses { thisIngester := resp.response.(*logproto.LabelToValuesResponse) - for label, values := range thisIngester.Labels { - uniqueValues := make([]string, len(values.Values)) - allValues := thisIngester.Labels[label] - uniqueValues = append(uniqueValues, allValues.Values...) - labelMap[label] = uniqueValues + for label, thisIngesterValues := range thisIngester.Labels { + var combinedValues []string + allIngesterValues, isLabelPresent := labelMap[label] + if isLabelPresent { + combinedValues = append(allIngesterValues, thisIngesterValues.Values...) + } else { + combinedValues = thisIngesterValues.Values + } + labelMap[label] = combinedValues } } + + // Dedupe all ingester values mergedResult := make(map[string]*logproto.UniqueLabelValues) for label, val := range labelMap { - uniqueValues := slices.CompactFunc(val, strings.EqualFold) + slices.Sort(val) + uniqueValues := slices.Compact(val) mergedResult[label] = &logproto.UniqueLabelValues{ Values: uniqueValues, diff --git a/pkg/querier/querier.go b/pkg/querier/querier.go index dd550377d1a40..3c03cde0653b7 100644 --- a/pkg/querier/querier.go +++ b/pkg/querier/querier.go @@ -915,7 +915,7 @@ func (q *SingleTenantQuerier) DetectedLabels(ctx context.Context, req *logproto. g, ctx := errgroup.WithContext(ctx) ingesterQueryInterval, _ := q.buildQueryIntervals(*req.Start, *req.End) - if !q.cfg.QueryStoreOnly { + if !q.cfg.QueryStoreOnly && ingesterQueryInterval != nil { g.Go(func() error { var err error splitReq := *req @@ -932,6 +932,12 @@ func (q *SingleTenantQuerier) DetectedLabels(ctx context.Context, req *logproto. return nil, err } + if ingesterLabels == nil { + return &logproto.DetectedLabelsResponse{ + DetectedLabels: []*logproto.DetectedLabel{}, + }, nil + } + for label, values := range ingesterLabels.Labels { if q.isLabelRelevant(label, values) { detectedLabels = append(detectedLabels, &logproto.DetectedLabel{Label: label, Cardinality: uint64(len(values.Values))})