From b62a3e70bbd8f745227755aea1961c63f5931416 Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Fri, 1 Dec 2023 22:41:06 +0100 Subject: [PATCH] skip analytics for the integration tests setting the env var DO_NOT_TRACK (#3794) Co-authored-by: Pantani --- ignite/internal/analytics/analytics.go | 17 +++++++++-------- ignite/pkg/gacli/gacli.go | 3 ++- integration/env.go | 11 ++++++++++- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/ignite/internal/analytics/analytics.go b/ignite/internal/analytics/analytics.go index 9da5a77760..6bc8367caa 100644 --- a/ignite/internal/analytics/analytics.go +++ b/ignite/internal/analytics/analytics.go @@ -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 { @@ -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 @@ -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, } @@ -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 diff --git a/ignite/pkg/gacli/gacli.go b/ignite/pkg/gacli/gacli.go index 3e1373c125..7237bcd50b 100644 --- a/ignite/pkg/gacli/gacli.go +++ b/ignite/pkg/gacli/gacli.go @@ -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) @@ -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{ diff --git a/integration/env.go b/integration/env.go index 394a818464..477fe00b04 100644 --- a/integration/env.go +++ b/integration/env.go @@ -25,7 +25,7 @@ import ( ) const ( - ConfigYML = "config.yml" + envDoNotTrack = "DO_NOT_TRACK" ) var ( @@ -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() { @@ -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)) }