diff --git a/promql/printer.go b/promql/printer.go index 709d8cb980..2298ba857d 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 9a828067cc..50c637987e 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)`, },