RecordTelemetry instruments a command with an invocation-owned telemetry event.
(cmd *cobra.Command, telemetry ghtelemetry.EventRecorder)
| 11 | |
| 12 | // RecordTelemetry instruments a command with an invocation-owned telemetry event. |
| 13 | func RecordTelemetry(cmd *cobra.Command, telemetry ghtelemetry.EventRecorder) { |
| 14 | if isTelemetryDisabled(cmd) { |
| 15 | return |
| 16 | } |
| 17 | |
| 18 | if cmd.RunE == nil { |
| 19 | return |
| 20 | } |
| 21 | |
| 22 | var event ghtelemetry.PendingEvent |
| 23 | currentArgs := cmd.Args |
| 24 | cmd.Args = func(cmd *cobra.Command, args []string) error { |
| 25 | event = telemetry.Begin(ghtelemetry.Event{ |
| 26 | Type: "command_invocation", |
| 27 | Dimensions: ghtelemetry.Dimensions{ |
| 28 | "command": cmd.CommandPath(), |
| 29 | "flags": telemetryFlags(cmd), |
| 30 | }, |
| 31 | }) |
| 32 | if currentArgs != nil { |
| 33 | return currentArgs(cmd, args) |
| 34 | } |
| 35 | return nil |
| 36 | } |
| 37 | |
| 38 | currentRunE := cmd.RunE |
| 39 | cmd.RunE = func(cmd *cobra.Command, args []string) error { |
| 40 | runErr := currentRunE(cmd, args) |
| 41 | // Commands with DisableFlagParsing may parse their flags inside RunE. |
| 42 | event.UpsertDimensions(ghtelemetry.Dimensions{"flags": telemetryFlags(cmd)}) |
| 43 | return runErr |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func telemetryFlags(cmd *cobra.Command) string { |
| 48 | var flags []string |