Skip to content

Commit

Permalink
Add observability for flags used in commands (#1263)
Browse files Browse the repository at this point in the history
* Add observability for flags used in commands

* Only add flags to telemetry if telemetry is available
  • Loading branch information
tomas-stripe authored Oct 16, 2024
1 parent ccabc70 commit 2206d0a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
log "github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"

"github.com/stripe/stripe-cli/pkg/cmd/resource"
Expand Down Expand Up @@ -60,6 +61,13 @@ var rootCmd = &cobra.Command{
telemetryMetadata.SetCobraCommandContext(cmd)
telemetryMetadata.SetMerchant(merchant)
telemetryMetadata.SetUserAgent(useragent.GetEncodedUserAgent())

flags := []string{}
cmd.Flags().Visit(func(flag *pflag.Flag) {
flags = append(flags, flag.Name)
})
flagsStr := strings.Join(flags, ",")
telemetryMetadata.SetCommandFlags(flagsStr)
}

// plugins send their own telemetry due to having richer context than the CLI does
Expand Down
6 changes: 6 additions & 0 deletions pkg/stripe/analytics_telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type CLIAnalyticsEventMetadata struct {
InvocationID string `url:"invocation_id"` // The invocation id is unique to each context object and represents all events coming from one command / gRPC method call
UserAgent string `url:"user_agent"` // the application that is used to create this request
CommandPath string `url:"command_path"` // the command or gRPC method that initiated this request
CommandFlags string `url:"command_flags"` // Comma-separated list of flags that were passed to the command (only includes flag names, not their values)
Merchant string `url:"merchant"` // the merchant ID: ex. acct_xxxx
CLIVersion string `url:"cli_version"` // the version of the CLI
OS string `url:"os"` // the OS of the system
Expand Down Expand Up @@ -133,6 +134,11 @@ func (e *CLIAnalyticsEventMetadata) SetCommandPath(commandPath string) {
e.CommandPath = commandPath
}

// SetCommandFlags sets the flags on the CLIAnalyticsEventContext object
func (e *CLIAnalyticsEventMetadata) SetCommandFlags(commandFlags string) {
e.CommandFlags = commandFlags
}

// SendAPIRequestEvent is a special function for API requests
func (a *AnalyticsTelemetryClient) SendAPIRequestEvent(ctx context.Context, requestID string, livemode bool) (*http.Response, error) {
a.wg.Add(1)
Expand Down

0 comments on commit 2206d0a

Please sign in to comment.