MCPcopy Create free account
hub / github.com/cli/cli / RecordTelemetry

Function RecordTelemetry

pkg/cmdutil/telemetry.go:13–45  ·  view source on GitHub ↗

RecordTelemetry instruments a command with an invocation-owned telemetry event.

(cmd *cobra.Command, telemetry ghtelemetry.EventRecorder)

Source from the content-addressed store, hash-verified

11
12// RecordTelemetry instruments a command with an invocation-owned telemetry event.
13func 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
47func telemetryFlags(cmd *cobra.Command) string {
48 var flags []string

Callers 2

TestRecordTelemetryFunction · 0.92

Calls 4

UpsertDimensionsMethod · 0.95
isTelemetryDisabledFunction · 0.85
telemetryFlagsFunction · 0.85
BeginMethod · 0.65

Tested by 1

TestRecordTelemetryFunction · 0.74