Skip to content
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

Telemetry: command path #236

Merged
merged 1 commit into from
Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@ All of the available shortcuts are listed in the [wiki page](https://github.com/

For dashboard pages, you can also add the `--livemode` flag to open the page directly in live mode.

## Telemetry

The Stripe CLI includes a telemetry feature that collects some usage data. This feature is enabled by default. To opt out of the telemetry feature, set the `STRIPE_CLI_TELEMETRY_OPTOUT` environment variable to `1` or `true`.

## Developing the Stripe CLI

If you're working on developing the CLI, it's recommended that you alias the go command to run the dev version. Place this in your shell rc file (such as `.bashrc` or `.zshrc`)
Expand Down
4 changes: 4 additions & 0 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/stripe/stripe-cli/pkg/cmd/resource"
"github.com/stripe/stripe-cli/pkg/config"
"github.com/stripe/stripe-cli/pkg/stripe"
"github.com/stripe/stripe-cli/pkg/version"
)

Expand Down Expand Up @@ -49,6 +50,9 @@ get started with pre-built samples (https://stripe.dev/samples).
getBanner(),
getLogin(&fs, &Config),
),
PersistentPreRun: func(cmd *cobra.Command, args []string) {
stripe.GetTelemetryInstance().SetCommandContext(cmd)
},
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand Down
7 changes: 7 additions & 0 deletions pkg/stripe/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ func (c *Client) PerformRequest(ctx context.Context, method, path string, params
req.Header.Set("User-Agent", useragent.GetEncodedUserAgent())
req.Header.Set("X-Stripe-Client-User-Agent", useragent.GetEncodedStripeUserAgent())

if !telemetryOptedOut(os.Getenv("STRIPE_CLI_TELEMETRY_OPTOUT")) {
telemetryHdr, err := getTelemetryHeader()
if err == nil {
req.Header.Set("Stripe-CLI-Telemetry", telemetryHdr)
}
}

if c.APIKey != "" {
req.Header.Set("Authorization", "Bearer "+c.APIKey)
}
Expand Down
71 changes: 71 additions & 0 deletions pkg/stripe/telemetry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package stripe

import (
"encoding/json"
"strings"
"sync"

"github.com/spf13/cobra"
)

//
// Public types
//

// CLITelemetry is the structure that holds telemetry data sent to Stripe in
// API requests.
type CLITelemetry struct {
CommandPath string `json:"command_path"`
}

// SetCommandContext sets the telemetry values for the command being executed.
func (t *CLITelemetry) SetCommandContext(cmd *cobra.Command) {
t.CommandPath = cmd.CommandPath()
}

//
// Public functions
//

// GetTelemetryInstance returns the CLITelemetry instance (initializing it
// first if necessary).
func GetTelemetryInstance() *CLITelemetry {
once.Do(func() {
instance = &CLITelemetry{}
})

return instance
}

//
// Private variables
//

var instance *CLITelemetry
var once sync.Once

//
// Private functions
//

func getTelemetryHeader() (string, error) {
telemetry := GetTelemetryInstance()
b, err := json.Marshal(telemetry)

if err != nil {
return "", err
}

return string(b), nil
}

// telemetryOptedOut returns true if the user has opted out of telemetry,
// false otherwise.
func telemetryOptedOut(optoutVar string) bool {
optoutVar = strings.ToLower(optoutVar)
if optoutVar == "1" || optoutVar == "true" {
return true
}

return false
}
35 changes: 35 additions & 0 deletions pkg/stripe/telemetry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package stripe

import (
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
)

func TestGetTelemetryInstance(t *testing.T) {
t1 := GetTelemetryInstance()
t2 := GetTelemetryInstance()
require.Equal(t, t1, t2)
}

func TestSetCommandContext(t *testing.T) {
tel := GetTelemetryInstance()
cmd := &cobra.Command{
Use: "foo",
}
tel.SetCommandContext(cmd)
require.Equal(t, "foo", tel.CommandPath)
}

func TestTelemetryOptedOut(t *testing.T) {
require.False(t, telemetryOptedOut(""))
require.False(t, telemetryOptedOut("0"))
require.False(t, telemetryOptedOut("false"))
require.False(t, telemetryOptedOut("False"))
require.False(t, telemetryOptedOut("FALSE"))
require.True(t, telemetryOptedOut("1"))
require.True(t, telemetryOptedOut("true"))
require.True(t, telemetryOptedOut("True"))
require.True(t, telemetryOptedOut("TRUE"))
}