-
Notifications
You must be signed in to change notification settings - Fork 0
feat(telemetry): add basic telemetry system #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
829a990
3a0b3e0
d84e984
4861115
1513c9f
445e20f
11536a6
1cd0d03
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ import ( | |
| "regexp" | ||
| "slices" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/fatih/color" | ||
| "github.com/platformsh/platformify/commands" | ||
|
|
@@ -22,6 +23,7 @@ import ( | |
| "github.com/upsun/cli/internal/config" | ||
| "github.com/upsun/cli/internal/config/alt" | ||
| "github.com/upsun/cli/internal/legacy" | ||
| "github.com/upsun/cli/internal/telemetry" | ||
| ) | ||
|
|
||
| // Execute is the main entrypoint to run the CLI. | ||
|
|
@@ -88,11 +90,23 @@ func newRootCommand(cnf *config.Config, assets *vendorization.VendorAssets) *cob | |
| }, | ||
| Run: func(cmd *cobra.Command, _ []string) { | ||
| c := makeLegacyCLIWrapper(cnf, cmd.OutOrStdout(), cmd.ErrOrStderr(), cmd.InOrStdin()) | ||
| if err := c.Exec(cmd.Context(), os.Args[1:]...); err != nil { | ||
| err := c.Exec(cmd.Context(), os.Args[1:]...) | ||
|
|
||
| if err != nil { | ||
| exitWithError(err) | ||
| } | ||
|
|
||
| waitForTelemetry(cmd.Context(), cnf, os.Args[1:]) | ||
| }, | ||
| PersistentPostRun: func(cmd *cobra.Command, _ []string) { | ||
| // Send telemetry for Go-native subcommands only | ||
| // Legacy commands send telemetry in Run before exitWithError | ||
| // cmd.Parent() != nil means this is a subcommand, not the root | ||
| if cmd.Parent() != nil { | ||
| telemetry.SendTelemetryEvent(cmd.Context(), cnf, cmd.Use) | ||
| // Fire and forget for native commands | ||
| } | ||
|
|
||
| checkShellConfigLeftovers(cmd.ErrOrStderr(), cnf) | ||
| select { | ||
| case rel := <-updateMessageChan: | ||
|
|
@@ -118,9 +132,13 @@ func newRootCommand(cnf *config.Config, assets *vendorization.VendorAssets) *cob | |
| } | ||
|
|
||
| c := makeLegacyCLIWrapper(cnf, cmd.OutOrStdout(), cmd.ErrOrStderr(), cmd.InOrStdin()) | ||
| if err := c.Exec(cmd.Context(), args...); err != nil { | ||
| err := c.Exec(cmd.Context(), args...) | ||
|
|
||
| if err != nil { | ||
| exitWithError(err) | ||
| } | ||
|
|
||
| // Don't send telemetry for help commands | ||
| }) | ||
|
|
||
| cmd.PersistentFlags().BoolP("version", "V", false, fmt.Sprintf("Displays the %s version", cnf.Application.Name)) | ||
|
|
@@ -276,3 +294,15 @@ func makeLegacyCLIWrapper(cnf *config.Config, stdout, stderr io.Writer, stdin io | |
| Stdin: stdin, | ||
| } | ||
| } | ||
|
|
||
| func waitForTelemetry(ctx context.Context, cnf *config.Config, args []string) { | ||
| command := telemetry.ExtractCommand(args) | ||
| done := telemetry.SendTelemetryEvent(ctx, cnf, command) | ||
|
|
||
| select { | ||
| case <-done: | ||
| // Telemetry completed | ||
| case <-time.After(2 * time.Second): | ||
| // Timeout - proceed anyway to avoid blocking user | ||
| } | ||
|
Comment on lines
+298
to
+307
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In Cobra commands,
cmd.Usecan include placeholders like "init [flags]" (e.g.initis defined asUse: "init [flags]"), which won't match the exact strings inIsTracked(). As a result, telemetry won't be sent for tracked Go-native commands. Usecmd.Name()(orcmd.CommandPath()if you want full path) instead ofcmd.Usewhen reporting the command identifier.