Skip to content

Commit

Permalink
Fix sharding behavior for vector matches (#5799)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdufel authored Oct 20, 2022
1 parent 35a3e3d commit d5744e7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
4 changes: 4 additions & 0 deletions pkg/querysharding/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"

lru "github.com/hashicorp/golang-lru"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/promql/parser"
)

Expand Down Expand Up @@ -99,6 +100,9 @@ func (a *QueryAnalyzer) Analyze(query string) (QueryAnalysis, error) {
case *parser.BinaryExpr:
if n.VectorMatching != nil {
shardingLabels := without(n.VectorMatching.MatchingLabels, []string{"le"})
if !n.VectorMatching.On && len(shardingLabels) > 0 {
shardingLabels = append(shardingLabels, model.MetricNameLabel)
}
analysis = analysis.scopeToLabels(shardingLabels, n.VectorMatching.On)
}
case *parser.AggregateExpr:
Expand Down
7 changes: 4 additions & 3 deletions pkg/querysharding/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sort"
"testing"

"github.com/prometheus/common/model"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -142,20 +143,20 @@ sum by (container) (
{
name: "binary expression with without vector matching and grouping",
expression: `sum without (cluster, pod) (http_requests_total{code="400"}) / ignoring (pod) sum without (cluster, pod) (http_requests_total)`,
shardingLabels: []string{"pod", "cluster"},
shardingLabels: []string{"pod", "cluster", model.MetricNameLabel},
},
{
name: "multiple binary expressions with without grouping",
expression: `(http_requests_total{code="400"} + ignoring (pod) http_requests_total{code="500"}) / ignoring (cluster, pod) http_requests_total`,
shardingLabels: []string{"cluster", "pod"},
shardingLabels: []string{"cluster", "pod", model.MetricNameLabel},
},
{
name: "multiple binary expressions with without vector matchers",
expression: `
(http_requests_total{code="400"} + ignoring (cluster, pod) http_requests_total{code="500"})
/ ignoring (pod)
http_requests_total`,
shardingLabels: []string{"cluster", "pod"},
shardingLabels: []string{"cluster", "pod", model.MetricNameLabel},
},
{
name: "histogram quantile",
Expand Down

0 comments on commit d5744e7

Please sign in to comment.