Skip to content

Commit

Permalink
Fetch label values from index
Browse files Browse the repository at this point in the history
  • Loading branch information
shantanualsi committed Apr 3, 2024
1 parent 82787eb commit 3d0249f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
25 changes: 21 additions & 4 deletions pkg/ingester/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,8 +562,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 {
Expand All @@ -589,9 +587,28 @@ type UniqueValues map[string]struct{}

// LabelsWithValues returns the label names with all the unique values depending on the request
func (i *instance) LabelsWithValues(ctx context.Context, startTime time.Time, matchers ...*labels.Matcher) (map[string]UniqueValues, error) {
// TODO (shantanu): Figure out how to get the label names from index directly when no matchers are given.

labelMap := make(map[string]UniqueValues)
if len(matchers) == 0 {
labelsFromIndex, err := i.index.LabelNames(startTime, nil)
if err != nil {
return nil, err
}

for _, label := range labelsFromIndex {
values, err := i.index.LabelValues(startTime, label, nil)
if err != nil {
return nil, err
}
existingValues, exists := labelMap[label]
if !exists {
existingValues = make(map[string]struct{})
}
for _, v := range values {
existingValues[v] = struct{}{}
}
}
}

err := i.forMatchingStreams(ctx, startTime, matchers, nil, func(s *stream) error {
for _, label := range s.labels {
v, exists := labelMap[label.Name]
Expand Down
12 changes: 0 additions & 12 deletions pkg/querier/queryrange/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,18 +266,6 @@ type DetectedLabelsRequest struct {
logproto.DetectedLabelsRequest
}

// NewDetectedLabelsRequest creates a new request for detected labels
func NewDetectedLabelsRequest(start, end time.Time, query, path string) *DetectedLabelsRequest {
return &DetectedLabelsRequest{
DetectedLabelsRequest: logproto.DetectedLabelsRequest{
Start: &start,
End: &end,
Query: query,
},
path: path,
}
}

func (r *DetectedLabelsRequest) AsProto() *logproto.DetectedLabelsRequest {
return &r.DetectedLabelsRequest
}
Expand Down

0 comments on commit 3d0249f

Please sign in to comment.