Skip to content

Commit

Permalink
skip analytics for the integration tests setting the env var DO_NOT_T…
Browse files Browse the repository at this point in the history
…RACK (#3794)

Co-authored-by: Pantani <Pantani>
  • Loading branch information
Pantani committed Dec 1, 2023
1 parent 4001371 commit b62a3e7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
17 changes: 9 additions & 8 deletions ignite/internal/analytics/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func WithError(error error) Option {
}
}

// SendMetric send command metrics to analytics.
func SendMetric(wg *sync.WaitGroup, args []string, opts ...Option) {
// only the app name
if len(args) <= 1 {
Expand All @@ -66,17 +67,10 @@ func SendMetric(wg *sync.WaitGroup, args []string, opts ...Option) {
o(&opt)
}

envDoNotTrackVar := os.Getenv(envDoNotTrack)
if envDoNotTrackVar == "1" || strings.ToLower(envDoNotTrackVar) == "true" {
return
}

if args[1] == "version" {
return
}

fullCmd := strings.Join(args[1:], " ")

dntInfo, err := checkDNT()
if err != nil || dntInfo.DoNotTrack {
return
Expand All @@ -85,7 +79,7 @@ func SendMetric(wg *sync.WaitGroup, args []string, opts ...Option) {
met := gacli.Metric{
OS: runtime.GOOS,
Arch: runtime.GOARCH,
FullCmd: fullCmd,
FullCmd: strings.Join(args[1:], " "),
SessionID: dntInfo.Name,
Version: version.Version,
}
Expand All @@ -106,7 +100,14 @@ func SendMetric(wg *sync.WaitGroup, args []string, opts ...Option) {
}()
}

// checkDNT check if the user allow to track data or if the DO_NOT_TRACK
// env var is set https://consoledonottrack.com/
func checkDNT() (anonIdentity, error) {
envDoNotTrackVar := os.Getenv(envDoNotTrack)
if envDoNotTrackVar == "1" || strings.ToLower(envDoNotTrackVar) == "true" {
return anonIdentity{DoNotTrack: true}, nil
}

home, err := os.UserHomeDir()
if err != nil {
return anonIdentity{}, err
Expand Down
3 changes: 2 additions & 1 deletion ignite/pkg/gacli/gacli.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func New(endpoint string, opts ...Option) Client {
return c
}

// Send sends metrics to analytics.
// Send sends metric event to analytics.
func (c Client) Send(body Body) error {
// encode body
encoded, err := json.Marshal(body)
Expand Down Expand Up @@ -109,6 +109,7 @@ func (c Client) Send(body Body) error {
return nil
}

// SendMetric build the metrics and send to analytics.
func (c Client) SendMetric(metric Metric) error {
metric.EngagementTimeMsec = "100"
return c.Send(Body{
Expand Down
11 changes: 10 additions & 1 deletion integration/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
)

const (
ConfigYML = "config.yml"
envDoNotTrack = "DO_NOT_TRACK"
)

var (
Expand Down Expand Up @@ -57,6 +57,7 @@ func New(t *testing.T) Env {
// set an other one thanks to env var.
cfgDir := path.Join(t.TempDir(), ".ignite")
env.SetConfigDir(cfgDir)
enableDoNotTrackEnv()

t.Cleanup(cancel)
compileBinaryOnce.Do(func() {
Expand Down Expand Up @@ -167,6 +168,14 @@ func (e Env) RequireExpectations() {
e.Must(e.HasFailed())
}

// enableDoNotTrackEnv set true the DO_NOT_TRACK env var.
func enableDoNotTrackEnv() {
err := os.Setenv(envDoNotTrack, "true")
if err != nil {
panic(fmt.Sprintf("error set %s env: %v", envDoNotTrack, err))
}
}

func Contains(s, partial string) bool {
return strings.Contains(s, strings.TrimSpace(partial))
}
Expand Down

0 comments on commit b62a3e7

Please sign in to comment.