Skip to content

Commit

Permalink
Improve check cmd output, fixes #1159
Browse files Browse the repository at this point in the history
  • Loading branch information
mfpierre committed Feb 8, 2018
1 parent ca6610b commit 64b4556
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
33 changes: 26 additions & 7 deletions cmd/agent/app/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
}
Expand Down
5 changes: 5 additions & 0 deletions releasenotes/notes/improve-check-cli-e08f666abf9157f4.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 64b4556

Please sign in to comment.