Skip to content

Commit

Permalink
move telemetry to the internal package
Browse files Browse the repository at this point in the history
  • Loading branch information
Pantani authored and Pantani committed Dec 1, 2023
1 parent caa2d07 commit c14c612
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 24 deletions.
10 changes: 3 additions & 7 deletions ignite/cmd/ignite/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,36 @@ 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())
}

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())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package analytics

import (
"encoding/json"
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion ignite/pkg/gacli/gacli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit c14c612

Please sign in to comment.