Skip to content

Commit

Permalink
Filter out ID types and add common labels
Browse files Browse the repository at this point in the history
  • Loading branch information
shantanualsi committed Apr 2, 2024
1 parent 49a91bb commit d063675
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion pkg/querier/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"context"
"flag"
"net/http"
"regexp"
"time"

"github.com/go-kit/log"
"github.com/opentracing/opentracing-go"
"golang.org/x/exp/slices"

"github.com/grafana/loki/v3/pkg/storage/stores/index"
"github.com/grafana/loki/v3/pkg/storage/stores/index/seriesvolume"
Expand Down Expand Up @@ -915,6 +917,9 @@ func (q *SingleTenantQuerier) DetectedFields(_ context.Context, _ *logproto.Dete
func (q *SingleTenantQuerier) DetectedLabels(ctx context.Context, req *logproto.DetectedLabelsRequest) (*logproto.DetectedLabelsResponse, error) {
var ingesterLabels *logproto.LabelToValuesResponse
var detectedLabels []*logproto.DetectedLabel

staticLabels := []string{"pod", "namespace", "cluster", "instance"}

g, ctx := errgroup.WithContext(ctx)
ingesterQueryInterval, _ := q.buildQueryIntervals(*req.Start, *req.End)
if !q.cfg.QueryStoreOnly {
Expand All @@ -937,7 +942,9 @@ func (q *SingleTenantQuerier) DetectedLabels(ctx context.Context, req *logproto.
for label, values := range ingesterLabels.Labels {
cardinality := len(values.Values)
// TODO(shantanu) make these values configurable
if cardinality < 5 || cardinality > 50 {
if !slices.Contains(staticLabels, label) &&
(cardinality < 1 || cardinality > 50) ||
containsAllIDTypes(values.Values) {
continue
}
detectedLabels = append(detectedLabels, &logproto.DetectedLabel{Label: label, Cardinality: uint64(cardinality)})
Expand All @@ -947,3 +954,18 @@ func (q *SingleTenantQuerier) DetectedLabels(ctx context.Context, req *logproto.
DetectedLabels: detectedLabels,
}, nil
}

// containsAllIDTypes filters out all UUID, GUID and numeric types. Returns false if even one value is not of the type
func containsAllIDTypes(values []string) bool {
pattern := `^(?:(?:[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})|(?:(?:\{)?[0-9a-fA-F]{8}(?:-?[0-9a-fA-F]{4}){3}-?[0-9a-fA-F]{12}(?:\})?)|(\d+))$`

re := regexp.MustCompile(pattern)

for _, v := range values {
if !re.MatchString(v) {
return false
}
}

return true
}

0 comments on commit d063675

Please sign in to comment.