-
Notifications
You must be signed in to change notification settings - Fork 290
/
tilt_analytics.go
144 lines (124 loc) · 3.4 KB
/
tilt_analytics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package analytics
import (
"time"
"github.com/tilt-dev/wmclient/pkg/analytics"
)
const TagVersion = "version"
const TagOS = "os"
const TagGitRepoHash = "git.origin"
const TagSubcommand = "subcommand"
// An Analytics that allows opting in/out at runtime.
type TiltAnalytics struct {
opter AnalyticsOpter
a analytics.Analytics
tiltVersion string
// We make this a constant pointer to a struct.
// That way, the struct returned by WithoutGlobalTags() can
// point to the same opt set.
opt *optSet
}
type optSet struct {
env analytics.Opt
user analytics.Opt
tiltfile analytics.Opt
}
// An AnalyticsOpter can record a user's choice (opt-in or opt-out)
// in re: Tilt recording analytics.
type AnalyticsOpter interface {
SetUserOpt(opt analytics.Opt) error
ReadUserOpt() (analytics.Opt, error)
}
func NewTiltAnalytics(opter AnalyticsOpter, a analytics.Analytics, tiltVersion string) (*TiltAnalytics, error) {
userOpt, err := opter.ReadUserOpt()
if err != nil {
return nil, err
}
envOpt := analytics.OptDefault
if ok, _ := IsAnalyticsDisabledFromEnv(); ok {
envOpt = analytics.OptOut
}
return &TiltAnalytics{
opter: opter,
a: a,
tiltVersion: tiltVersion,
opt: &optSet{
env: envOpt,
user: userOpt,
tiltfile: analytics.OptDefault,
},
}, nil
}
// NOTE: if you need a ctx as well, use testutils.CtxAndAnalyticsForTest so that you get
// a ctx with the correct analytics baked in.
func NewMemoryTiltAnalyticsForTest(opter AnalyticsOpter) (*analytics.MemoryAnalytics, *TiltAnalytics) {
ma := analytics.NewMemoryAnalytics()
ta, err := NewTiltAnalytics(opter, ma, "v0.0.0")
if err != nil {
panic(err)
}
ta.opt.env = analytics.OptDefault
return ma, ta
}
func (ta *TiltAnalytics) GlobalTag(name string) (string, bool) {
return ta.a.GlobalTag(name)
}
func (ta *TiltAnalytics) MachineHash() string {
id, _ := ta.GlobalTag(analytics.TagMachine)
return id
}
func (ta *TiltAnalytics) GitRepoHash() string {
id, _ := ta.GlobalTag(TagGitRepoHash)
return id
}
func (ta *TiltAnalytics) Count(name string, tags map[string]string, n int) {
if ta.EffectiveOpt() != analytics.OptOut {
ta.a.Count(name, tags, n)
}
}
func (ta *TiltAnalytics) Incr(name string, tags map[string]string) {
if ta.EffectiveOpt() != analytics.OptOut {
ta.a.Incr(name, tags)
}
}
func (ta *TiltAnalytics) Timer(name string, dur time.Duration, tags map[string]string) {
if ta.EffectiveOpt() != analytics.OptOut {
ta.a.Timer(name, dur, tags)
}
}
func (ta *TiltAnalytics) Flush(timeout time.Duration) {
ta.a.Flush(timeout)
}
func (ta *TiltAnalytics) UserOpt() analytics.Opt {
return ta.opt.user
}
func (ta *TiltAnalytics) TiltfileOpt() analytics.Opt {
return ta.opt.tiltfile
}
func (ta *TiltAnalytics) EffectiveOpt() analytics.Opt {
if ta.opt.env != analytics.OptDefault {
return ta.opt.env
}
if ta.opt.tiltfile != analytics.OptDefault {
return ta.opt.tiltfile
}
return ta.opt.user
}
func (ta *TiltAnalytics) SetUserOpt(opt analytics.Opt) error {
if opt == ta.opt.user {
return nil
}
ta.opt.user = opt
return ta.opter.SetUserOpt(opt)
}
func (ta *TiltAnalytics) SetTiltfileOpt(opt analytics.Opt) {
ta.opt.tiltfile = opt
}
func (ta *TiltAnalytics) WithoutGlobalTags() analytics.Analytics {
return &TiltAnalytics{
opter: ta.opter,
a: ta.a.WithoutGlobalTags(),
tiltVersion: ta.tiltVersion,
opt: ta.opt,
}
}
var _ analytics.Analytics = &TiltAnalytics{}