From d5744e794d97a4d9c6e398c44e0a2166ee82f8b8 Mon Sep 17 00:00:00 2001 From: Samuel Dufel Date: Thu, 20 Oct 2022 08:22:44 -0700 Subject: [PATCH] Fix sharding behavior for vector matches (#5799) --- pkg/querysharding/analyzer.go | 4 ++++ pkg/querysharding/analyzer_test.go | 7 ++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/pkg/querysharding/analyzer.go b/pkg/querysharding/analyzer.go index f93a103fcf..ccd1f4ccb9 100644 --- a/pkg/querysharding/analyzer.go +++ b/pkg/querysharding/analyzer.go @@ -7,6 +7,7 @@ import ( "fmt" lru "github.com/hashicorp/golang-lru" + "github.com/prometheus/common/model" "github.com/prometheus/prometheus/promql/parser" ) @@ -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: diff --git a/pkg/querysharding/analyzer_test.go b/pkg/querysharding/analyzer_test.go index efd9dd5d94..dc3a059b23 100644 --- a/pkg/querysharding/analyzer_test.go +++ b/pkg/querysharding/analyzer_test.go @@ -7,6 +7,7 @@ import ( "sort" "testing" + "github.com/prometheus/common/model" "github.com/stretchr/testify/require" ) @@ -142,12 +143,12 @@ 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", @@ -155,7 +156,7 @@ sum by (container) ( (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",