From afe054789933413475e60d530274639f177bc274 Mon Sep 17 00:00:00 2001 From: Pierluigi Lenoci Date: Tue, 24 Mar 2026 12:12:54 +0100 Subject: [PATCH] promtool: redirect debug output to stderr (#18346) The debug flag in `promtool test rules` writes diagnostic output using fmt.Printf to stdout, which can interfere with machine-parseable output (e.g. JUnit XML via --junit-output) and piped workflows. Redirect all DEBUG lines to stderr using fmt.Fprintf(os.Stderr, ...), consistent with the existing error output pattern already present in the file (line 78). Signed-off-by: Pierluigi Lenoci --- cmd/promtool/unittest.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/cmd/promtool/unittest.go b/cmd/promtool/unittest.go index dab452af64..d63908efc6 100644 --- a/cmd/promtool/unittest.go +++ b/cmd/promtool/unittest.go @@ -228,9 +228,9 @@ type testGroup struct { func (tg *testGroup) test(testname string, evalInterval time.Duration, groupOrderMap map[string]int, queryOpts promqltest.LazyLoaderOpts, diffFlag, debug, ignoreUnknownFields, fuzzyCompare bool, ruleFiles ...string) (outErr []error) { if debug { testStart := time.Now() - fmt.Printf("DEBUG: Starting test %s\n", testname) + fmt.Fprintf(os.Stderr, "DEBUG: Starting test %s\n", testname) defer func() { - fmt.Printf("DEBUG: Test %s finished, took %v\n", testname, time.Since(testStart)) + fmt.Fprintf(os.Stderr, "DEBUG: Test %s finished, took %v\n", testname, time.Since(testStart)) }() } // Setup testing suite. @@ -534,20 +534,20 @@ Outer: expr := fmt.Sprintf(`{__name__=~".+"}[%v]`, ts) q, err := suite.QueryEngine().NewInstantQuery(context.Background(), suite.Queryable(), nil, expr, mint.Add(ts)) if err != nil { - fmt.Printf("DEBUG: Failed querying, expr: %q, err: %v\n", expr, err) + fmt.Fprintf(os.Stderr, "DEBUG: Failed querying, expr: %q, err: %v\n", expr, err) return errs } res := q.Exec(suite.Context()) if res.Err != nil { - fmt.Printf("DEBUG: Failed query exec, expr: %q, err: %v\n", expr, res.Err) + fmt.Fprintf(os.Stderr, "DEBUG: Failed query exec, expr: %q, err: %v\n", expr, res.Err) return errs } switch v := res.Value.(type) { case promql.Matrix: - fmt.Printf("DEBUG: Dump of all data (input_series and rules) at %v:\n", ts) - fmt.Println(v.String()) + fmt.Fprintf(os.Stderr, "DEBUG: Dump of all data (input_series and rules) at %v:\n", ts) + fmt.Fprintln(os.Stderr, v.String()) default: - fmt.Printf("DEBUG: Got unexpected type %T\n", v) + fmt.Fprintf(os.Stderr, "DEBUG: Got unexpected type %T\n", v) return errs } }