promql: Fix printing of empty without() (#3719)

* promql: Fix printing of empty without()

Fixes https://github.com/prometheus/prometheus/issues/3704

* Test cleanup fixup
This commit is contained in:
Julius Volz 2018-01-21 22:22:55 +01:00 committed by GitHub
parent 85f23d82a0
commit 1e943fc10a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 7 deletions

View File

@ -139,15 +139,15 @@ func (node *AggregateExpr) String() string {
aggrString += fmt.Sprintf("%s, ", node.Param) aggrString += fmt.Sprintf("%s, ", node.Param)
} }
aggrString += fmt.Sprintf("%s)", node.Expr) aggrString += fmt.Sprintf("%s)", node.Expr)
if len(node.Grouping) > 0 {
var format string
if node.Without { if node.Without {
format = "%s WITHOUT (%s)" aggrString = fmt.Sprintf("%s WITHOUT (%s)", aggrString, strings.Join(node.Grouping, ", "))
} else { } else {
format = "%s BY (%s)" if len(node.Grouping) > 0 {
aggrString = fmt.Sprintf("%s BY (%s)", aggrString, strings.Join(node.Grouping, ", "))
} }
aggrString = fmt.Sprintf(format, aggrString, strings.Join(node.Grouping, ", "))
} }
return aggrString return aggrString
} }

View File

@ -63,6 +63,9 @@ func TestExprString(t *testing.T) {
{ {
in: `sum(task:errors:rate10s{job="s"}) BY (code)`, in: `sum(task:errors:rate10s{job="s"}) BY (code)`,
}, },
{
in: `sum(task:errors:rate10s{job="s"}) WITHOUT ()`,
},
{ {
in: `sum(task:errors:rate10s{job="s"}) WITHOUT (instance)`, in: `sum(task:errors:rate10s{job="s"}) WITHOUT (instance)`,
}, },