Skip to content

Commit

Permalink
Simplify clientID generation for analytics (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
tamalsaha authored Dec 18, 2017
1 parent a45937f commit 4e29b70
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 39 deletions.
5 changes: 0 additions & 5 deletions backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/appscode/go/log"
"github.com/appscode/kutil/meta"
"github.com/appscode/kutil/tools/analytics"
cs "github.com/appscode/stash/client/typed/stash/v1alpha1"
"github.com/appscode/stash/pkg/backup"
"github.com/appscode/stash/pkg/util"
Expand Down Expand Up @@ -43,10 +42,6 @@ func NewCmdBackup() *cobra.Command {
kubeClient := kubernetes.NewForConfigOrDie(config)
stashClient := cs.NewForConfigOrDie(config)

if meta.PossiblyInCluster() {
sendAnalytics(cmd, analytics.ClientID(kubeClient.CoreV1().Nodes()))
}

opt.NodeName = os.Getenv("NODE_NAME")
if opt.NodeName == "" {
log.Fatalln(`Missing ENV var "NODE_NAME"`)
Expand Down
5 changes: 0 additions & 5 deletions check.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmds
import (
"github.com/appscode/go/log"
"github.com/appscode/kutil/meta"
"github.com/appscode/kutil/tools/analytics"
cs "github.com/appscode/stash/client/typed/stash/v1alpha1"
"github.com/appscode/stash/pkg/check"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -32,10 +31,6 @@ func NewCmdCheck() *cobra.Command {
kubeClient := kubernetes.NewForConfigOrDie(config)
stashClient := cs.NewForConfigOrDie(config)

if meta.PossiblyInCluster() {
sendAnalytics(cmd, analytics.ClientID(kubeClient.CoreV1().Nodes()))
}

c := check.New(kubeClient, stashClient, opt)
if err = c.Run(); err != nil {
log.Fatal(err)
Expand Down
5 changes: 0 additions & 5 deletions recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmds
import (
"github.com/appscode/go/log"
"github.com/appscode/kutil/meta"
"github.com/appscode/kutil/tools/analytics"
cs "github.com/appscode/stash/client/typed/stash/v1alpha1"
"github.com/appscode/stash/pkg/recovery"
"github.com/spf13/cobra"
Expand All @@ -30,10 +29,6 @@ func NewCmdRecover() *cobra.Command {
kubeClient := kubernetes.NewForConfigOrDie(config)
stashClient := cs.NewForConfigOrDie(config)

if meta.PossiblyInCluster() {
sendAnalytics(cmd, analytics.ClientID(kubeClient.CoreV1().Nodes()))
}

c := recovery.New(kubeClient, stashClient, meta.Namespace(), recoveryName)
c.Run()
},
Expand Down
28 changes: 10 additions & 18 deletions root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import (
"log"
"strings"

"github.com/appscode/go/analytics"
v "github.com/appscode/go/version"
"github.com/appscode/kutil/meta"
"github.com/appscode/kutil/tools/analytics"
"github.com/appscode/stash/client/scheme"
"github.com/jpillora/go-ogle-analytics"
"github.com/spf13/cobra"
Expand All @@ -20,6 +19,9 @@ const (
)

func NewRootCmd() *cobra.Command {
var (
enableAnalytics = true
)
var rootCmd = &cobra.Command{
Use: "stash",
Short: `Stash by AppsCode - Backup your Kubernetes Volumes`,
Expand All @@ -29,8 +31,12 @@ func NewRootCmd() *cobra.Command {
c.Flags().VisitAll(func(flag *pflag.Flag) {
log.Printf("FLAG: --%s=%q", flag.Name, flag.Value)
})
if !meta.PossiblyInCluster() {
sendAnalytics(c, analytics.ClientID())
if enableAnalytics && gaTrackingCode != "" {
if client, err := ga.NewClient(gaTrackingCode); err == nil {
client.ClientID(analytics.ClientID())
parts := strings.Split(c.CommandPath(), " ")
client.Send(ga.NewEvent(parts[0], strings.Join(parts[1:], "/")).Label(v.Version.Version))
}
}
scheme.AddToScheme(clientsetscheme.Scheme)
},
Expand All @@ -47,17 +53,3 @@ func NewRootCmd() *cobra.Command {
rootCmd.AddCommand(NewCmdCheck())
return rootCmd
}

var (
enableAnalytics = true
)

func sendAnalytics(c *cobra.Command, clientID string) {
if enableAnalytics && gaTrackingCode != "" {
if client, err := ga.NewClient(gaTrackingCode); err == nil {
client.ClientID(clientID)
parts := strings.Split(c.CommandPath(), " ")
client.Send(ga.NewEvent(parts[0], strings.Join(parts[1:], "/")).Label(v.Version.Version))
}
}
}
6 changes: 0 additions & 6 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"github.com/appscode/go/log"
stringz "github.com/appscode/go/strings"
v "github.com/appscode/go/version"
"github.com/appscode/kutil/meta"
"github.com/appscode/kutil/tools/analytics"
"github.com/appscode/pat"
api "github.com/appscode/stash/apis/stash"
cs "github.com/appscode/stash/client/typed/stash/v1alpha1"
Expand Down Expand Up @@ -53,10 +51,6 @@ func NewCmdRun() *cobra.Command {
stashClient := cs.NewForConfigOrDie(config)
crdClient := crd_cs.NewForConfigOrDie(config)

if meta.PossiblyInCluster() {
sendAnalytics(cmd, analytics.ClientID(kubeClient.CoreV1().Nodes()))
}

// get kube api server version
version, err := kubeClient.Discovery().ServerVersion()
if err != nil {
Expand Down

0 comments on commit 4e29b70

Please sign in to comment.