From 1e943fc10ac1972800fa21e1ce4e4ee2e744adf4 Mon Sep 17 00:00:00 2001 From: Julius Volz Date: Sun, 21 Jan 2018 22:22:55 +0100 Subject: [PATCH] promql: Fix printing of empty without() (#3719) * promql: Fix printing of empty without() Fixes https://github.com/prometheus/prometheus/issues/3704 * Test cleanup fixup --- promql/printer.go | 14 +++++++------- promql/printer_test.go | 3 +++ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/promql/printer.go b/promql/printer.go index 709d8cb98..2298ba857 100644 --- a/promql/printer.go +++ b/promql/printer.go @@ -139,15 +139,15 @@ func (node *AggregateExpr) String() string { aggrString += fmt.Sprintf("%s, ", node.Param) } aggrString += fmt.Sprintf("%s)", node.Expr) - if len(node.Grouping) > 0 { - var format string - if node.Without { - format = "%s WITHOUT (%s)" - } else { - format = "%s BY (%s)" + + if node.Without { + aggrString = fmt.Sprintf("%s WITHOUT (%s)", aggrString, strings.Join(node.Grouping, ", ")) + } else { + 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 } diff --git a/promql/printer_test.go b/promql/printer_test.go index 9a828067c..50c637987 100644 --- a/promql/printer_test.go +++ b/promql/printer_test.go @@ -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"}) WITHOUT ()`, + }, { in: `sum(task:errors:rate10s{job="s"}) WITHOUT (instance)`, },