diff --git a/ignite/cmd/ignite/main.go b/ignite/cmd/ignite/main.go index b7d784b816..f61a3f611b 100644 --- a/ignite/cmd/ignite/main.go +++ b/ignite/cmd/ignite/main.go @@ -5,21 +5,19 @@ import ( "errors" "fmt" "os" - "strings" "sync" ignitecmd "github.com/ignite/cli/ignite/cmd" chainconfig "github.com/ignite/cli/ignite/config/chain" + "github.com/ignite/cli/ignite/internal/analytics" "github.com/ignite/cli/ignite/pkg/clictx" "github.com/ignite/cli/ignite/pkg/cliui/colors" "github.com/ignite/cli/ignite/pkg/cliui/icons" - "github.com/ignite/cli/ignite/pkg/gacli" "github.com/ignite/cli/ignite/pkg/validation" "github.com/ignite/cli/ignite/pkg/xstrings" ) func main() { - gaclient = gacli.New(telemetryEndpoint) os.Exit(run()) } @@ -27,18 +25,16 @@ func run() int { const exitCodeOK, exitCodeError = 0, 1 var wg sync.WaitGroup - osArgs := strings.Join(os.Args, " ") - defer func() { if r := recover(); r != nil { - sendMetric(&wg, metric{err: fmt.Errorf("%v", r), command: osArgs}) + analytics.SendMetric(&wg, os.Args, analytics.WithError(fmt.Errorf("%v", r))) fmt.Println(r) os.Exit(exitCodeError) } }() if len(os.Args) > 1 { - sendMetric(&wg, metric{command: osArgs}) + analytics.SendMetric(&wg, os.Args) } ctx := clictx.From(context.Background()) diff --git a/ignite/cmd/ignite/analytics.go b/ignite/internal/analytics/analytics.go similarity index 78% rename from ignite/cmd/ignite/analytics.go rename to ignite/internal/analytics/analytics.go index 4b2bdb2ace..a690fccb26 100644 --- a/ignite/cmd/ignite/analytics.go +++ b/ignite/internal/analytics/analytics.go @@ -1,4 +1,4 @@ -package main +package analytics import ( "encoding/json" @@ -26,11 +26,9 @@ var gaclient gacli.Client type ( // metric represents an analytics metric. - metric struct { + options struct { // err sets metrics type as an error metric. err error - // command is the command name. - command string } // anonIdentity represents an analytics identity file. @@ -42,16 +40,43 @@ type ( } ) -func sendMetric(wg *sync.WaitGroup, m metric) { +func init() { + gaclient = gacli.New(telemetryEndpoint) +} + +// Option configures ChainCmd. +type Option func(*options) + +// WithError with application command error. +func WithError(error error) Option { + return func(m *options) { + m.err = error + } +} + +func SendMetric(wg *sync.WaitGroup, args []string, opts ...Option) { + // only the app name + if len(args) <= 1 { + return + } + + // apply analytics options. + var opt options + for _, o := range opts { + o(&opt) + } + envDoNotTrackVar := os.Getenv(envDoNotTrack) if envDoNotTrackVar == "1" || strings.ToLower(envDoNotTrackVar) == "true" { return } - if m.command == "ignite version" { + if args[1] == "version" { return } + fullCmd := strings.Join(args[1:], " ") + dntInfo, err := checkDNT() if err != nil || dntInfo.DoNotTrack { return @@ -60,24 +85,19 @@ func sendMetric(wg *sync.WaitGroup, m metric) { met := gacli.Metric{ OS: runtime.GOOS, Arch: runtime.GOARCH, - FullCmd: m.command, + FullCmd: fullCmd, SessionID: dntInfo.Name, Version: version.Version, } switch { - case m.err == nil: + case opt.err == nil: met.Status = "success" - case m.err != nil: + case opt.err != nil: met.Status = "error" - met.Error = m.err.Error() - } - - cmds := strings.Split(m.command, " ") - met.Cmd = cmds[0] - if len(cmds) > 0 { - met.Cmd = cmds[1] + met.Error = opt.err.Error() } + met.Cmd = args[1] wg.Add(1) go func() { diff --git a/ignite/pkg/gacli/gacli.go b/ignite/pkg/gacli/gacli.go index 3c9c93367b..3e1373c125 100644 --- a/ignite/pkg/gacli/gacli.go +++ b/ignite/pkg/gacli/gacli.go @@ -67,7 +67,7 @@ func New(endpoint string, opts ...Option) Client { Timeout: 1500 * time.Millisecond, }, } - // apply user options. + // apply analytics options. for _, o := range opts { o(&c) }