@@ -253,10 +253,20 @@ func main() {
253
253
254
254
root .AddCommand (command )
255
255
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 )
258
261
}
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
+ })
260
270
}
261
271
262
272
func customizeCliForACI (command * cobra.Command , proxy * api.ServiceProxy ) {
@@ -275,33 +285,64 @@ func customizeCliForACI(command *cobra.Command, proxy *api.ServiceProxy) {
275
285
}
276
286
}
277
287
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
+ ) {
279
298
// if user canceled request, simply exit without any error message
280
299
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
+ )
282
309
os .Exit (130 )
283
310
}
284
311
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
+ )
287
320
}
288
321
289
322
// Context should always be handled by new CLI
290
323
requiredCmd , _ , _ := root .Find (os .Args [1 :])
291
324
if requiredCmd != nil && isContextAgnosticCommand (requiredCmd ) {
292
- exit (currentContext , err , ctype )
325
+ exit (currentContext , err , ctype , start , duration )
293
326
}
294
327
mobycli .ExecIfDefaultCtxType (ctx , root )
295
328
296
329
checkIfUnknownCommandExistInDefaultContext (err , currentContext , ctype )
297
330
298
- exit (currentContext , err , ctype )
331
+ exit (currentContext , err , ctype , start , duration )
299
332
}
300
333
301
- func exit (ctx string , err error , ctype string ) {
334
+ func exit (ctx string , err error , ctype string , start time. Time , duration time. Duration ) {
302
335
if exit , ok := err .(cli.StatusError ); ok {
303
336
// 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
+ )
305
346
os .Exit (exit .StatusCode )
306
347
}
307
348
@@ -316,7 +357,15 @@ func exit(ctx string, err error, ctype string) {
316
357
metricsStatus = metrics .CommandSyntaxFailure .MetricsStatus
317
358
exitCode = metrics .CommandSyntaxFailure .ExitCode
318
359
}
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
+ )
320
369
321
370
if errors .Is (err , api .ErrLoginRequired ) {
322
371
fmt .Fprintln (os .Stderr , err )
@@ -351,7 +400,11 @@ func checkIfUnknownCommandExistInDefaultContext(err error, currentContext string
351
400
352
401
if mobycli .IsDefaultContextCommand (dockerCommand ) {
353
402
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
+ })
355
408
os .Exit (1 )
356
409
}
357
410
}
0 commit comments