@@ -253,10 +253,20 @@ func main() {
253253
254254 root .AddCommand (command )
255255
256- if err = root .ExecuteContext (ctx ); err != nil {
257- handleError (ctx , err , ctype , currentContext , cc , root )
256+ start := time .Now ().UTC ()
257+ err = root .ExecuteContext (ctx )
258+ duration := time .Since (start )
259+ if err != nil {
260+ handleError (ctx , err , ctype , currentContext , cc , root , start , duration )
258261 }
259- metricsClient .Track (ctype , os .Args [1 :], metrics .SuccessStatus )
262+ metricsClient .Track (
263+ metrics.CmdMeta {
264+ ContextType : ctype ,
265+ Args : os .Args [1 :],
266+ Status : metrics .SuccessStatus ,
267+ Start : start ,
268+ Duration : duration ,
269+ })
260270}
261271
262272func customizeCliForACI (command * cobra.Command , proxy * api.ServiceProxy ) {
@@ -275,33 +285,64 @@ func customizeCliForACI(command *cobra.Command, proxy *api.ServiceProxy) {
275285 }
276286}
277287
278- func handleError (ctx context.Context , err error , ctype string , currentContext string , cc * store.DockerContext , root * cobra.Command ) {
288+ func handleError (
289+ ctx context.Context ,
290+ err error ,
291+ ctype string ,
292+ currentContext string ,
293+ cc * store.DockerContext ,
294+ root * cobra.Command ,
295+ start time.Time ,
296+ duration time.Duration ,
297+ ) {
279298 // if user canceled request, simply exit without any error message
280299 if api .IsErrCanceled (err ) || errors .Is (ctx .Err (), context .Canceled ) {
281- metricsClient .Track (ctype , os .Args [1 :], metrics .CanceledStatus )
300+ metricsClient .Track (
301+ metrics.CmdMeta {
302+ ContextType : ctype ,
303+ Args : os .Args [1 :],
304+ Status : metrics .CanceledStatus ,
305+ Start : start ,
306+ Duration : duration ,
307+ },
308+ )
282309 os .Exit (130 )
283310 }
284311 if ctype == store .AwsContextType {
285- exit (currentContext , errors .Errorf (`%q context type has been renamed. Recreate the context by running:
286- $ docker context create %s <name>` , cc .Type (), store .EcsContextType ), ctype )
312+ exit (
313+ currentContext ,
314+ errors .Errorf (`%q context type has been renamed. Recreate the context by running:
315+ $ docker context create %s <name>` , cc .Type (), store .EcsContextType ),
316+ ctype ,
317+ start ,
318+ duration ,
319+ )
287320 }
288321
289322 // Context should always be handled by new CLI
290323 requiredCmd , _ , _ := root .Find (os .Args [1 :])
291324 if requiredCmd != nil && isContextAgnosticCommand (requiredCmd ) {
292- exit (currentContext , err , ctype )
325+ exit (currentContext , err , ctype , start , duration )
293326 }
294327 mobycli .ExecIfDefaultCtxType (ctx , root )
295328
296329 checkIfUnknownCommandExistInDefaultContext (err , currentContext , ctype )
297330
298- exit (currentContext , err , ctype )
331+ exit (currentContext , err , ctype , start , duration )
299332}
300333
301- func exit (ctx string , err error , ctype string ) {
334+ func exit (ctx string , err error , ctype string , start time. Time , duration time. Duration ) {
302335 if exit , ok := err .(cli.StatusError ); ok {
303336 // TODO(milas): shouldn't this use the exit code to determine status?
304- metricsClient .Track (ctype , os .Args [1 :], metrics .SuccessStatus )
337+ metricsClient .Track (
338+ metrics.CmdMeta {
339+ ContextType : ctype ,
340+ Args : os .Args [1 :],
341+ Status : metrics .SuccessStatus ,
342+ Start : start ,
343+ Duration : duration ,
344+ },
345+ )
305346 os .Exit (exit .StatusCode )
306347 }
307348
@@ -316,7 +357,15 @@ func exit(ctx string, err error, ctype string) {
316357 metricsStatus = metrics .CommandSyntaxFailure .MetricsStatus
317358 exitCode = metrics .CommandSyntaxFailure .ExitCode
318359 }
319- metricsClient .Track (ctype , os .Args [1 :], metricsStatus )
360+ metricsClient .Track (
361+ metrics.CmdMeta {
362+ ContextType : ctype ,
363+ Args : os .Args [1 :],
364+ Status : metricsStatus ,
365+ Start : start ,
366+ Duration : duration ,
367+ },
368+ )
320369
321370 if errors .Is (err , api .ErrLoginRequired ) {
322371 fmt .Fprintln (os .Stderr , err )
@@ -351,7 +400,11 @@ func checkIfUnknownCommandExistInDefaultContext(err error, currentContext string
351400
352401 if mobycli .IsDefaultContextCommand (dockerCommand ) {
353402 fmt .Fprintf (os .Stderr , "Command %q not available in current context (%s), you can use the \" default\" context to run this command\n " , dockerCommand , currentContext )
354- metricsClient .Track (contextType , os .Args [1 :], metrics .FailureStatus )
403+ metricsClient .Track (metrics.CmdMeta {
404+ ContextType : contextType ,
405+ Args : os .Args [1 :],
406+ Status : metrics .FailureStatus ,
407+ })
355408 os .Exit (1 )
356409 }
357410 }
0 commit comments