diff --git a/metrics/client.go b/metrics/client.go index 3a603a0fc..a6947e0f9 100644 --- a/metrics/client.go +++ b/metrics/client.go @@ -64,10 +64,24 @@ func NewClient() Client { } func (c *client) Send(command Command) { - req, err := json.Marshal(command) - if err != nil { - return - } + wasIn := make(chan bool) + + // Fire and forget, we don't want to slow down the user waiting for DD + // metrics endpoint to respond. We could lose some events but that's ok. + go func() { + defer func() { + _ = recover() + }() + + wasIn <- true + + req, err := json.Marshal(command) + if err != nil { + return + } + + _, _ = c.httpClient.Post("http://localhost/usage", "application/json", bytes.NewBuffer(req)) + }() + <-wasIn - _, _ = c.httpClient.Post("http://localhost/usage", "application/json", bytes.NewBuffer(req)) } diff --git a/metrics/metrics.go b/metrics/metrics.go index 4a0dd1408..333b0f0d4 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -78,28 +78,15 @@ const ( // Track sends the tracking analytics to Docker Desktop func Track(context string, args []string, flags *flag.FlagSet) { - wasIn := make(chan bool) - - // Fire and forget, we don't want to slow down the user waiting for DD - // metrics endpoint to respond. We could lose some events but that's ok. - go func() { - defer func() { - _ = recover() - }() - - wasIn <- true - - command := getCommand(args, flags) - if command != "" { - c := NewClient() - c.Send(Command{ - Command: command, - Context: context, - Source: CLISource, - }) - } - }() - <-wasIn + command := getCommand(args, flags) + if command != "" { + c := NewClient() + c.Send(Command{ + Command: command, + Context: context, + Source: CLISource, + }) + } } func getCommand(args []string, flags *flag.FlagSet) string {