diff --git a/promql/engine.go b/promql/engine.go index d9841295b0..d75f3cae11 100644 --- a/promql/engine.go +++ b/promql/engine.go @@ -1545,8 +1545,8 @@ func resultMetric(lhs, rhs labels.Labels, op ItemType, matching *VectorMatching, } var lb *labels.Builder - if matching.Card == CardOneToMany { - // Choose the rhs metric because lhs and rhs have been swapped. + if matching.Card == CardOneToMany && op.isComparisonOperator() { + // Choose the rhs metric as the one to output because lhs and rhs have been swapped. lb = labels.NewBuilder(rhs) } else { lb = labels.NewBuilder(lhs) @@ -1556,6 +1556,22 @@ func resultMetric(lhs, rhs labels.Labels, op ItemType, matching *VectorMatching, lb.Del(labels.MetricName) } + if matching.Card == CardOneToOne && !op.isComparisonOperator() { + if matching.On { + Outer: + for _, l := range lhs { + for _, n := range matching.MatchingLabels { + if l.Name == n { + continue Outer + } + } + lb.Del(l.Name) + } + } else { + lb.Del(matching.MatchingLabels...) + } + } + for _, ln := range matching.Include { // Included labels from the `group_x` modifier are taken from the "one"-side. if v := rhs.Get(ln); v != "" { diff --git a/promql/testdata/operators.test b/promql/testdata/operators.test index a39f0f11e6..29f8d8fa25 100644 --- a/promql/testdata/operators.test +++ b/promql/testdata/operators.test @@ -176,10 +176,10 @@ eval instant at 50m http_requests{group="canary"} unless on(job, instance) http_ http_requests{group="canary", instance="1", job="app-server"} 800 eval instant at 50m http_requests{group="canary"} / on(instance,job) http_requests{group="production"} - {group="canary", instance="0", job="api-server"} 3 - {group="canary", instance="0", job="app-server"} 1.4 - {group="canary", instance="1", job="api-server"} 2 - {group="canary", instance="1", job="app-server"} 1.3333333333333333 + {instance="0", job="api-server"} 3 + {instance="0", job="app-server"} 1.4 + {instance="1", job="api-server"} 2 + {instance="1", job="app-server"} 1.3333333333333333 eval instant at 50m http_requests{group="canary"} unless ignoring(group, instance) http_requests{instance="0"} @@ -188,10 +188,10 @@ eval instant at 50m http_requests{group="canary"} unless ignoring(group) http_re http_requests{group="canary", instance="1", job="app-server"} 800 eval instant at 50m http_requests{group="canary"} / ignoring(group) http_requests{group="production"} - {group="canary", instance="0", job="api-server"} 3 - {group="canary", instance="0", job="app-server"} 1.4 - {group="canary", instance="1", job="api-server"} 2 - {group="canary", instance="1", job="app-server"} 1.3333333333333333 + {instance="0", job="api-server"} 3 + {instance="0", job="app-server"} 1.4 + {instance="1", job="api-server"} 2 + {instance="1", job="app-server"} 1.3333333333333333 # https://github.com/prometheus/prometheus/issues/1489 eval instant at 50m http_requests AND ON (dummy) vector(1) @@ -398,7 +398,7 @@ load 5m # On with no labels, for metrics with no common labels. eval instant at 5m random + on() metricA - {foo="bar"} 5 + {} 5 # Ignoring with no labels is the same as no ignoring. eval instant at 5m metricA + ignoring() metricB