Do not send metrics for help commands

Signed-off-by: Guillaume Tardif <guillaume.tardif@docker.com>
This commit is contained in:
Guillaume Tardif 2020-10-08 14:36:40 +02:00
parent 3e93a690d2
commit a09983b474
4 changed files with 24 additions and 10 deletions

View File

@ -19,7 +19,7 @@ package metrics
var commandFlags = []string{
//added to catch scan details
"--version", "--login",
"--help", "-h"}
}
// Generated with generatecommands/main.go
var managementCommands = []string{

View File

@ -51,6 +51,9 @@ func GetCommand(args []string) string {
result := ""
onlyFlags := false
for _, arg := range args {
if arg == "--help" {
return ""
}
if arg == "--" {
break
}

View File

@ -58,6 +58,11 @@ func TestGetCommand(t *testing.T) {
args: []string{"image", "ls"},
expected: "image ls",
},
{
name: "management command with flag",
args: []string{"image", "--test", "ls"},
expected: "image ls",
},
{
name: "management subcommand with flag",
args: []string{"image", "ls", "-q"},
@ -148,7 +153,7 @@ func TestGetCommand(t *testing.T) {
}
}
func TestFlags(t *testing.T) {
func TestIgnoreHelpCommands(t *testing.T) {
testCases := []struct {
name string
args []string
@ -157,22 +162,17 @@ func TestFlags(t *testing.T) {
{
name: "help",
args: []string{"--help"},
expected: "--help",
expected: "",
},
{
name: "help on run",
args: []string{"run", "--help"},
expected: "run --help",
},
{
name: "-h on run",
args: []string{"run", "-h"},
expected: "run -h",
expected: "",
},
{
name: "help on compose up",
args: []string{"compose", "up", "--help"},
expected: "compose up --help",
expected: "",
},
}

View File

@ -164,6 +164,17 @@ func TestContextMetrics(t *testing.T) {
s.Start()
defer s.Stop()
t.Run("do not send metrics on help commands", func(t *testing.T) {
s.ResetUsage()
c.RunDockerCmd("--help")
c.RunDockerCmd("ps", "--help")
c.RunDockerCmd("run", "--help")
usage := s.GetUsage()
assert.Equal(t, 0, len(usage))
})
t.Run("metrics on default context", func(t *testing.T) {
s.ResetUsage()