trackCommandCompleted reports how the command ended: success, failure (with the class of the error) or user cancellation.
( ctx context.Context, cmd *cobra.Command, code exitCode, err error, elapsed time.Duration, )
| 287 | // trackCommandCompleted reports how the command ended: success, failure (with |
| 288 | // the class of the error) or user cancellation. |
| 289 | func trackCommandCompleted( |
| 290 | ctx context.Context, |
| 291 | cmd *cobra.Command, |
| 292 | code exitCode, |
| 293 | err error, |
| 294 | elapsed time.Duration, |
| 295 | ) { |
| 296 | if cmd == nil || !cmdutil.ShouldTrackUsage(cmd) { |
| 297 | return |
| 298 | } |
| 299 | // An empty command path means PersistentPreRunE never ran (--help, |
| 300 | // --version, unknown flag or command, failed auth check): no Command |
| 301 | // Invoked was sent, so don't send an orphan Command Completed either. |
| 302 | metadata := telemetry.GetEventMetadata(ctx) |
| 303 | if metadata == nil || metadata.CommandPath == "" { |
| 304 | return |
| 305 | } |
| 306 | client := telemetry.GetTelemetryClient(ctx) |
| 307 | if client == nil { |
| 308 | return |
| 309 | } |
| 310 | |
| 311 | props := map[string]any{ |
| 312 | "succeeded": code == exitOK, |
| 313 | "exit_code": int(code), |
| 314 | "duration_ms": elapsed.Milliseconds(), |
| 315 | } |
| 316 | if err != nil { |
| 317 | props["error_class"] = telemetry.ErrorClass(err) |
| 318 | props["user_cancelled"] = cmdutil.IsUserCancellation(err) |
| 319 | } |
| 320 | |
| 321 | _ = client.Track(ctx, telemetry.EventCommandCompleted, props) |
| 322 | } |
| 323 | |
| 324 | // closeTelemetry flushes the pending telemetry events, giving up after a |
| 325 | // short timeout so an unreachable telemetry endpoint never delays exit. |