From 4b3bab890d3316d1471d4a4390a53572a220855b Mon Sep 17 00:00:00 2001 From: Julien Pivotto Date: Fri, 20 Dec 2019 15:33:33 +0100 Subject: [PATCH] Fix absent output where two label matchers are identical (#6493) Signed-off-by: Julien Pivotto --- promql/functions.go | 17 ++++++++++++++--- promql/testdata/legacy.test | 11 ++++++++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/promql/functions.go b/promql/functions.go index f88fd58d4..ec65bdc88 100644 --- a/promql/functions.go +++ b/promql/functions.go @@ -480,15 +480,26 @@ func funcAbsent(vals []Value, args Expressions, enh *EvalNodeHelper) Vector { if len(vals[0].(Vector)) > 0 { return enh.out } - m := []labels.Label{} + m := labels.Labels{} + empty := []string{} if vs, ok := args[0].(*VectorSelector); ok { for _, ma := range vs.LabelMatchers { - if ma.Type == labels.MatchEqual && ma.Name != labels.MetricName { - m = append(m, labels.Label{Name: ma.Name, Value: ma.Value}) + if ma.Name == labels.MetricName { + continue + } + if ma.Type == labels.MatchEqual && !m.Has(ma.Name) { + m = labels.NewBuilder(m).Set(ma.Name, ma.Value).Labels() + } else { + empty = append(empty, ma.Name) } } } + + for _, v := range empty { + m = labels.NewBuilder(m).Set(v, "").Labels() + } + return append(enh.out, Sample{ Metric: labels.New(m...), diff --git a/promql/testdata/legacy.test b/promql/testdata/legacy.test index 5ed442133..43faf431c 100644 --- a/promql/testdata/legacy.test +++ b/promql/testdata/legacy.test @@ -264,6 +264,15 @@ eval instant at 50m absent(nonexistent) eval instant at 50m absent(nonexistent{job="testjob", instance="testinstance", method=~".x"}) {instance="testinstance", job="testjob"} 1 +eval instant at 50m absent(nonexistent{job="testjob",job="testjob2",foo="bar"}) + {foo="bar"} 1 + +eval instant at 50m absent(nonexistent{job="testjob",job="testjob2",job="three",foo="bar"}) + {foo="bar"} 1 + +eval instant at 50m absent(nonexistent{job="testjob",job=~"testjob2",foo="bar"}) + {foo="bar"} 1 + eval instant at 50m absent(http_requests) eval instant at 50m absent(sum(http_requests)) @@ -365,4 +374,4 @@ load 5m testmetric1{src="a",dst="b"} 0 testmetric2{src="a",dst="b"} 1 -eval_fail instant at 0m ceil({__name__=~'testmetric1|testmetric2'}) \ No newline at end of file +eval_fail instant at 0m ceil({__name__=~'testmetric1|testmetric2'})