From 64b4556e7188236231d2c40a9bb644e7ea596941 Mon Sep 17 00:00:00 2001 From: Pierre Date: Thu, 8 Feb 2018 19:49:29 +0100 Subject: [PATCH] Improve check cmd output, fixes #1159 --- cmd/agent/app/check.go | 33 +++++++++++++++---- .../improve-check-cli-e08f666abf9157f4.yaml | 5 +++ 2 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 releasenotes/notes/improve-check-cli-e08f666abf9157f4.yaml diff --git a/cmd/agent/app/check.go b/cmd/agent/app/check.go index 13467dac75f15..dbd269c715dde 100644 --- a/cmd/agent/app/check.go +++ b/cmd/agent/app/check.go @@ -10,14 +10,17 @@ import ( "fmt" "time" + "github.com/fatih/color" + "github.com/spf13/cobra" + "github.com/DataDog/datadog-agent/cmd/agent/common" "github.com/DataDog/datadog-agent/pkg/aggregator" + "github.com/DataDog/datadog-agent/pkg/collector/autodiscovery" "github.com/DataDog/datadog-agent/pkg/collector/check" "github.com/DataDog/datadog-agent/pkg/config" "github.com/DataDog/datadog-agent/pkg/serializer" "github.com/DataDog/datadog-agent/pkg/status" "github.com/DataDog/datadog-agent/pkg/util" - "github.com/spf13/cobra" ) var ( @@ -51,6 +54,10 @@ var checkCmd = &cobra.Command{ return err } + if flagNoColor { + color.NoColor = true + } + if logLevel == "" { if confFilePath != "" { logLevel = config.Datadog.GetString("log_level") @@ -84,8 +91,20 @@ var checkCmd = &cobra.Command{ common.SetupAutoConfig(config.Datadog.GetString("confd_path")) cs := common.AC.GetChecksByName(checkName) if len(cs) == 0 { - fmt.Println("no check found") - return fmt.Errorf("no check found") + for check, error := range autodiscovery.GetConfigErrors() { + if checkName == check { + fmt.Fprintln(color.Output, fmt.Sprintf("\n%s: invalid config for %s: %s", color.RedString("Error"), color.YellowString(check), error)) + } + } + for check, errors := range autodiscovery.GetLoaderErrors() { + if checkName == check { + fmt.Fprintln(color.Output, fmt.Sprintf("\n%s: could not load %s:", color.RedString("Error"), color.YellowString(checkName))) + for loader, error := range errors { + fmt.Fprintln(color.Output, fmt.Sprintf("* %s: %s", color.YellowString(loader), error)) + } + } + } + return fmt.Errorf("no valid check found") } if len(cs) > 1 { @@ -130,28 +149,28 @@ func runCheck(c check.Check, agg *aggregator.BufferedAggregator) *check.Stats { func printMetrics(agg *aggregator.BufferedAggregator) { series := agg.GetSeries() if len(series) != 0 { - fmt.Println("Series: ") + fmt.Fprintln(color.Output, fmt.Sprintf("=== %s ===", color.BlueString("Series"))) j, _ := json.MarshalIndent(series, "", " ") fmt.Println(string(j)) } sketches := agg.GetSketches() if len(sketches) != 0 { - fmt.Println("Sketches: ") + fmt.Fprintln(color.Output, fmt.Sprintf("=== %s ===", color.BlueString("Sketches"))) j, _ := json.MarshalIndent(sketches, "", " ") fmt.Println(string(j)) } serviceChecks := agg.GetServiceChecks() if len(serviceChecks) != 0 { - fmt.Println("Service Checks: ") + fmt.Fprintln(color.Output, fmt.Sprintf("=== %s ===", color.BlueString("Service Checks"))) j, _ := json.MarshalIndent(serviceChecks, "", " ") fmt.Println(string(j)) } events := agg.GetEvents() if len(events) != 0 { - fmt.Println("Events: ") + fmt.Fprintln(color.Output, fmt.Sprintf("=== %s ===", color.BlueString("Events"))) j, _ := json.MarshalIndent(events, "", " ") fmt.Println(string(j)) } diff --git a/releasenotes/notes/improve-check-cli-e08f666abf9157f4.yaml b/releasenotes/notes/improve-check-cli-e08f666abf9157f4.yaml new file mode 100644 index 0000000000000..3d42f5bba2e03 --- /dev/null +++ b/releasenotes/notes/improve-check-cli-e08f666abf9157f4.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - | + Fix https://github.com/DataDog/datadog-agent/issues/1159 where error was not + explicit when the check had invalid configuration or code