Skip to content

Commit

Permalink
Remove hardcoded labels in store shard matcher
Browse files Browse the repository at this point in the history
Signed-off-by: haanhvu <[email protected]>
  • Loading branch information
haanhvu committed Aug 24, 2022
1 parent ee512ae commit 3eb116c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
34 changes: 29 additions & 5 deletions pkg/querysharding/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package querysharding

import (
"fmt"
"strings"

"github.com/prometheus/prometheus/promql/parser"
)
Expand Down Expand Up @@ -55,12 +56,16 @@ func (a *QueryAnalyzer) Analyze(query string) (QueryAnalysis, error) {
}
case *parser.BinaryExpr:
if n.VectorMatching != nil {
analysis = analysis.scopeToLabels(n.VectorMatching.MatchingLabels, n.VectorMatching.On)
labels := make([]string, 0)
//Exclude le label from sharding
labels = excludeLabelsBasedOnPrefix(n.VectorMatching.MatchingLabels, "le=")
analysis = analysis.scopeToLabels(labels, n.VectorMatching.On)
}
case *parser.AggregateExpr:
labels := make([]string, 0)
if len(n.Grouping) > 0 {
labels = n.Grouping
//Exclude le label from sharding
labels = excludeLabelsBasedOnPrefix(n.Grouping, "le=")
}

analysis = analysis.scopeToLabels(labels, !n.Without)
Expand All @@ -85,16 +90,22 @@ func analyzeRootExpression(node parser.Node) QueryAnalysis {
switch n := node.(type) {
case *parser.BinaryExpr:
if n.VectorMatching != nil && n.VectorMatching.On {
return newShardableByLabels(n.VectorMatching.MatchingLabels, n.VectorMatching.On)
labels := make([]string, 0)
//Exclude le label from sharding
labels = excludeLabelsBasedOnPrefix(n.VectorMatching.MatchingLabels, "le=")
return newShardableByLabels(labels, n.VectorMatching.On)
} else {
return nonShardableQuery()
}
case *parser.AggregateExpr:
if len(n.Grouping) == 0 {
return nonShardableQuery()
}

return newShardableByLabels(n.Grouping, !n.Without)

labels := make([]string, 0)
//Exclude le label from sharding
labels = excludeLabelsBasedOnPrefix(n.Grouping, "le=")
return newShardableByLabels(labels, !n.Without)
}

return nonShardableQuery()
Expand All @@ -109,3 +120,16 @@ func contains(needle string, haystack []string) bool {

return false
}

// Exclude labels based on label's prefix
// Return the remaining labels
func excludeLabelsBasedOnPrefix(labels []string, labelPrefix string) []string {
remainingLabels := make([]string, 0)
for _, label := range labels {
if !strings.HasPrefix(label, labelPrefix) {
remainingLabels = append(remainingLabels, label)
}
}

return remainingLabels
}
5 changes: 0 additions & 5 deletions pkg/store/storepb/shard_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ func (s *ShardMatcher) MatchesZLabels(zLabels []labelpb.ZLabel) bool {

*s.buf = (*s.buf)[:0]
for _, lbl := range zLabels {
// Exclude metric name and le label from sharding
if lbl.Name == "__name__" || lbl.Name == "le" {
continue
}

if shardByLabel(s.shardingLabelset, lbl, s.by) {
*s.buf = append(*s.buf, lbl.Name...)
*s.buf = append(*s.buf, sep[0])
Expand Down

0 comments on commit 3eb116c

Please sign in to comment.