Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[process-agent] Implement human readable check output #11480

Merged
merged 15 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions cmd/process-agent/app/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
"context"
"encoding/json"
"fmt"
"os"
"time"

"github.com/fatih/color"
"github.com/spf13/cobra"

"github.com/DataDog/agent-payload/v5/process"
Expand All @@ -28,17 +30,24 @@ import (
"github.com/DataDog/datadog-agent/pkg/workloadmeta"
)

func init() {
CheckCmd.Flags().BoolVar(&checkOutputJSON, "json", false, "Output check results in JSON")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we plan to add other output types? Boolean might not be the best choice if so.

For context, many CLIs use a -o <format> flag, so we might want an enum for the output format. The other consideration is that the core agent check has two flags to control the output, --json and --table, and --json supersedes --table (so that is similar to what we're doing here)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the -o suggestion

Copy link
Contributor Author

@xornivore xornivore Mar 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's stick with --json bool for now, may not be the most extensible but there is consistency with status command in the core agent. Other formats could be interesting, but having one machine-readable format can be sufficient for now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the record, the core agent also has --table, which we might want to add in the future. Their solution to the problem is having --json take precedence over --table

}

// CheckCmd is a command that runs the process-agent version data
var CheckCmd = &cobra.Command{
Use: "check",
Short: "Run a specific check and print the results. Choose from: process, rtprocess, container, rtcontainer, connections, process_discovery",
Use: "check",
Short: "Run a specific check and print the results. Choose from: process, rtprocess, container, rtcontainer, connections, process_discovery",

Args: cobra.ExactArgs(1),
RunE: runCheckCmd,
SilenceUsage: true,
}

const loggerName ddconfig.LoggerName = "PROCESS"

var checkOutputJSON = false

func runCheckCmd(cmd *cobra.Command, args []string) error {
// We need to load in the system probe environment variables before we load the config, otherwise an
// "Unknown environment variable" warning will show up whenever valid system probe environment variables are defined.
Expand Down Expand Up @@ -134,7 +143,7 @@ func runCheck(cfg *config.AgentConfig, ch checks.Check) error {
if err != nil {
return fmt.Errorf("collection error: %s", err)
}
return printResults(msgs)
return printResults(ch.Name(), msgs)
}

func runCheckAsRealTime(cfg *config.AgentConfig, ch checks.CheckWithRealTime) error {
Expand Down Expand Up @@ -165,7 +174,7 @@ func runCheckAsRealTime(cfg *config.AgentConfig, ch checks.CheckWithRealTime) er
return fmt.Errorf("collection error: %s", err)
}

return printResults(run.RealTime)
return printResults(ch.RealTimeName(), run.RealTime)
}

func printResultsBanner(name string) {
Expand All @@ -174,7 +183,22 @@ func printResultsBanner(name string) {
fmt.Printf("-----------------------------\n\n")
}

func printResults(msgs []process.MessageBody) error {
func printResults(check string, msgs []process.MessageBody) error {
if checkOutputJSON {
return printResultsJSON(msgs)
}

err := checks.HumanFormat(check, msgs, os.Stdout)
switch err {
case checks.ErrNoHumanFormat:
fmt.Println(color.YellowString("Printing output in JSON format for %s\n", check))
return printResultsJSON(msgs)
default:
return err
}
}

func printResultsJSON(msgs []process.MessageBody) error {
for _, m := range msgs {
b, err := json.MarshalIndent(m, "", " ")
if err != nil {
Expand Down
Loading