From 764f2d03a52d9bbc702d6e9b285db7cac082a192 Mon Sep 17 00:00:00 2001 From: jessicagreben Date: Wed, 24 Nov 2021 09:17:49 -0800 Subject: [PATCH 1/4] add const for exit codes Signed-off-by: jessicagreben --- cmd/promtool/main.go | 69 ++++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 31 deletions(-) diff --git a/cmd/promtool/main.go b/cmd/promtool/main.go index 7ae656c68..184bc9fdb 100644 --- a/cmd/promtool/main.go +++ b/cmd/promtool/main.go @@ -56,6 +56,13 @@ import ( "github.com/prometheus/prometheus/scrape" ) +const ( + successExitCode = 0 + failureExitCode = 1 + // exit code 3 is used for "one or more lint issues detected". + lintErrExitCode = 3 +) + func main() { app := kingpin.New(filepath.Base(os.Args[0]), "Tooling for the Prometheus monitoring system.").UsageWriter(os.Stdout) app.Version(version.Print("promtool")) @@ -294,9 +301,9 @@ func CheckConfig(agentMode bool, files ...string) int { } } if failed { - return 1 + return failureExitCode } - return 0 + return successExitCode } // CheckWebConfig validates web configuration files. @@ -312,9 +319,9 @@ func CheckWebConfig(files ...string) int { fmt.Fprintln(os.Stderr, f, "SUCCESS") } if failed { - return 1 + return failureExitCode } - return 0 + return successExitCode } func checkFileExists(fn string) error { @@ -503,9 +510,9 @@ func CheckRules(files ...string) int { fmt.Println() } if failed { - return 1 + return failureExitCode } - return 0 + return successExitCode } func checkRules(filename string) (int, []error) { @@ -513,7 +520,7 @@ func checkRules(filename string) (int, []error) { rgs, errs := rulefmt.ParseFile(filename) if errs != nil { - return 0, errs + return successExitCode, errs } numRules := 0 @@ -609,7 +616,7 @@ func CheckMetrics() int { problems, err := l.Lint() if err != nil { fmt.Fprintln(os.Stderr, "error while linting:", err) - return 1 + return failureExitCode } for _, p := range problems { @@ -617,10 +624,10 @@ func CheckMetrics() int { } if len(problems) > 0 { - return 3 + return lintErrExitCode } - return 0 + return successExitCode } // QueryInstant performs an instant query against a Prometheus server. @@ -636,7 +643,7 @@ func QueryInstant(url *url.URL, query, evalTime string, p printer) int { c, err := api.NewClient(config) if err != nil { fmt.Fprintln(os.Stderr, "error creating API client:", err) - return 1 + return failureExitCode } eTime := time.Now() @@ -644,7 +651,7 @@ func QueryInstant(url *url.URL, query, evalTime string, p printer) int { eTime, err = parseTime(evalTime) if err != nil { fmt.Fprintln(os.Stderr, "error parsing evaluation time:", err) - return 1 + return failureExitCode } } @@ -660,7 +667,7 @@ func QueryInstant(url *url.URL, query, evalTime string, p printer) int { p.printValue(val) - return 0 + return successExitCode } // QueryRange performs a range query against a Prometheus server. @@ -685,7 +692,7 @@ func QueryRange(url *url.URL, headers map[string]string, query, start, end strin c, err := api.NewClient(config) if err != nil { fmt.Fprintln(os.Stderr, "error creating API client:", err) - return 1 + return failureExitCode } var stime, etime time.Time @@ -696,7 +703,7 @@ func QueryRange(url *url.URL, headers map[string]string, query, start, end strin etime, err = parseTime(end) if err != nil { fmt.Fprintln(os.Stderr, "error parsing end time:", err) - return 1 + return failureExitCode } } @@ -706,13 +713,13 @@ func QueryRange(url *url.URL, headers map[string]string, query, start, end strin stime, err = parseTime(start) if err != nil { fmt.Fprintln(os.Stderr, "error parsing start time:", err) - return 1 + return failureExitCode } } if !stime.Before(etime) { fmt.Fprintln(os.Stderr, "start time is not before end time") - return 1 + return failureExitCode } if step == 0 { @@ -733,7 +740,7 @@ func QueryRange(url *url.URL, headers map[string]string, query, start, end strin } p.printValue(val) - return 0 + return successExitCode } // QuerySeries queries for a series against a Prometheus server. @@ -749,13 +756,13 @@ func QuerySeries(url *url.URL, matchers []string, start, end string, p printer) c, err := api.NewClient(config) if err != nil { fmt.Fprintln(os.Stderr, "error creating API client:", err) - return 1 + return failureExitCode } stime, etime, err := parseStartTimeAndEndTime(start, end) if err != nil { fmt.Fprintln(os.Stderr, err) - return 1 + return failureExitCode } // Run query against client. @@ -769,7 +776,7 @@ func QuerySeries(url *url.URL, matchers []string, start, end string, p printer) } p.printSeries(val) - return 0 + return successExitCode } // QueryLabels queries for label values against a Prometheus server. @@ -785,13 +792,13 @@ func QueryLabels(url *url.URL, name, start, end string, p printer) int { c, err := api.NewClient(config) if err != nil { fmt.Fprintln(os.Stderr, "error creating API client:", err) - return 1 + return failureExitCode } stime, etime, err := parseStartTimeAndEndTime(start, end) if err != nil { fmt.Fprintln(os.Stderr, err) - return 1 + return failureExitCode } // Run query against client. @@ -808,7 +815,7 @@ func QueryLabels(url *url.URL, name, start, end string, p printer) int { } p.printLabelValues(val) - return 0 + return successExitCode } func handleAPIError(err error) int { @@ -819,7 +826,7 @@ func handleAPIError(err error) int { fmt.Fprintln(os.Stderr, "query error:", err) } - return 1 + return failureExitCode } func parseStartTimeAndEndTime(start, end string) (time.Time, time.Time, error) { @@ -911,9 +918,9 @@ func debugPprof(url string) int { endPointGroups: pprofEndpoints, }); err != nil { fmt.Fprintln(os.Stderr, "error completing debug command:", err) - return 1 + return failureExitCode } - return 0 + return successExitCode } func debugMetrics(url string) int { @@ -923,9 +930,9 @@ func debugMetrics(url string) int { endPointGroups: metricsEndpoints, }); err != nil { fmt.Fprintln(os.Stderr, "error completing debug command:", err) - return 1 + return failureExitCode } - return 0 + return successExitCode } func debugAll(url string) int { @@ -935,9 +942,9 @@ func debugAll(url string) int { endPointGroups: allEndpoints, }); err != nil { fmt.Fprintln(os.Stderr, "error completing debug command:", err) - return 1 + return failureExitCode } - return 0 + return successExitCode } type printer interface { From 99bb56fc46df4609b984c558e5240ac537722902 Mon Sep 17 00:00:00 2001 From: jessicagreben Date: Wed, 1 Dec 2021 04:45:18 -0800 Subject: [PATCH 2/4] add errcodes from sd file Signed-off-by: jessicagreben --- cmd/promtool/main.go | 7 ++++--- cmd/promtool/sd.go | 10 +++++----- cmd/promtool/unittest.go | 4 ++-- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/cmd/promtool/main.go b/cmd/promtool/main.go index 184bc9fdb..5c85de54f 100644 --- a/cmd/promtool/main.go +++ b/cmd/promtool/main.go @@ -57,9 +57,10 @@ import ( ) const ( - successExitCode = 0 - failureExitCode = 1 - // exit code 3 is used for "one or more lint issues detected". + successExitCode = 0 + failureExitCode = 1 + configErrExitCode = 2 + // Exit code 3 is used for "one or more lint issues detected". lintErrExitCode = 3 ) diff --git a/cmd/promtool/sd.go b/cmd/promtool/sd.go index f3b2c13b4..2950d3e35 100644 --- a/cmd/promtool/sd.go +++ b/cmd/promtool/sd.go @@ -43,7 +43,7 @@ func CheckSD(sdConfigFiles, sdJobName string, sdTimeout time.Duration) int { cfg, err := config.LoadFile(sdConfigFiles, false, false, logger) if err != nil { fmt.Fprintln(os.Stderr, "Cannot load config", err) - return 2 + return configErrExitCode } var scrapeConfig *config.ScrapeConfig @@ -63,7 +63,7 @@ func CheckSD(sdConfigFiles, sdJobName string, sdTimeout time.Duration) int { for _, job := range jobs { fmt.Fprintf(os.Stderr, "\t%s\n", job) } - return 1 + return failureExitCode } targetGroupChan := make(chan []*targetgroup.Group) @@ -74,7 +74,7 @@ func CheckSD(sdConfigFiles, sdJobName string, sdTimeout time.Duration) int { d, err := cfg.NewDiscoverer(discovery.DiscovererOptions{Logger: logger}) if err != nil { fmt.Fprintln(os.Stderr, "Could not create new discoverer", err) - return 2 + return configErrExitCode } go d.Run(ctx, targetGroupChan) } @@ -100,11 +100,11 @@ outerLoop: res, err := json.MarshalIndent(results, "", " ") if err != nil { fmt.Fprintf(os.Stderr, "Could not marshal result json: %s", err) - return 2 + return configErrExitCode } fmt.Printf("%s", res) - return 0 + return successExitCode } func getSDCheckResult(targetGroups []*targetgroup.Group, scrapeConfig *config.ScrapeConfig) []sdCheckResult { diff --git a/cmd/promtool/unittest.go b/cmd/promtool/unittest.go index 62e2843d4..b8704231d 100644 --- a/cmd/promtool/unittest.go +++ b/cmd/promtool/unittest.go @@ -56,9 +56,9 @@ func RulesUnitTest(queryOpts promql.LazyLoaderOpts, files ...string) int { fmt.Println() } if failed { - return 1 + return failureExitCode } - return 0 + return successExitCode } func ruleUnitTest(filename string, queryOpts promql.LazyLoaderOpts) []error { From 59f7ef06d0002e818c73642c963d49f3ccf3d70e Mon Sep 17 00:00:00 2001 From: jessicagreben Date: Sat, 18 Dec 2021 04:44:13 -0800 Subject: [PATCH 3/4] update exit code for sd Signed-off-by: jessicagreben --- cmd/promtool/sd.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/promtool/sd.go b/cmd/promtool/sd.go index 2950d3e35..90ed4a56c 100644 --- a/cmd/promtool/sd.go +++ b/cmd/promtool/sd.go @@ -74,7 +74,7 @@ func CheckSD(sdConfigFiles, sdJobName string, sdTimeout time.Duration) int { d, err := cfg.NewDiscoverer(discovery.DiscovererOptions{Logger: logger}) if err != nil { fmt.Fprintln(os.Stderr, "Could not create new discoverer", err) - return configErrExitCode + return failureExitCode } go d.Run(ctx, targetGroupChan) } @@ -100,7 +100,7 @@ outerLoop: res, err := json.MarshalIndent(results, "", " ") if err != nil { fmt.Fprintf(os.Stderr, "Could not marshal result json: %s", err) - return configErrExitCode + return failureExitCode } fmt.Printf("%s", res) From 4b03fa31008e43390772ab20eeca254d3d6dc553 Mon Sep 17 00:00:00 2001 From: jessicagreben Date: Thu, 30 Dec 2021 05:37:57 -0800 Subject: [PATCH 4/4] replace config exit code with failure exit code Signed-off-by: jessicagreben --- cmd/promtool/main.go | 5 ++--- cmd/promtool/sd.go | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/cmd/promtool/main.go b/cmd/promtool/main.go index 5c85de54f..698594820 100644 --- a/cmd/promtool/main.go +++ b/cmd/promtool/main.go @@ -57,9 +57,8 @@ import ( ) const ( - successExitCode = 0 - failureExitCode = 1 - configErrExitCode = 2 + successExitCode = 0 + failureExitCode = 1 // Exit code 3 is used for "one or more lint issues detected". lintErrExitCode = 3 ) diff --git a/cmd/promtool/sd.go b/cmd/promtool/sd.go index 90ed4a56c..ce2bb3de5 100644 --- a/cmd/promtool/sd.go +++ b/cmd/promtool/sd.go @@ -43,7 +43,7 @@ func CheckSD(sdConfigFiles, sdJobName string, sdTimeout time.Duration) int { cfg, err := config.LoadFile(sdConfigFiles, false, false, logger) if err != nil { fmt.Fprintln(os.Stderr, "Cannot load config", err) - return configErrExitCode + return failureExitCode } var scrapeConfig *config.ScrapeConfig