Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: skip analytics for the integration tests #3794

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading