-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathclient.go
More file actions
246 lines (211 loc) · 7.56 KB
/
client.go
File metadata and controls
246 lines (211 loc) · 7.56 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package sync
import (
"os"
"runtime/debug"
"strings"
"sync"
"github.com/ActiveState/cli/internal/analytics"
"github.com/ActiveState/cli/internal/analytics/client/sync/reporters"
anaConsts "github.com/ActiveState/cli/internal/analytics/constants"
"github.com/ActiveState/cli/internal/analytics/dimensions"
"github.com/ActiveState/cli/internal/condition"
"github.com/ActiveState/cli/internal/config"
"github.com/ActiveState/cli/internal/constants"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/installation/storage"
"github.com/ActiveState/cli/internal/instanceid"
"github.com/ActiveState/cli/internal/logging"
configMediator "github.com/ActiveState/cli/internal/mediators/config"
"github.com/ActiveState/cli/internal/multilog"
"github.com/ActiveState/cli/internal/osutils"
"github.com/ActiveState/cli/internal/output"
"github.com/ActiveState/cli/internal/rollbar"
"github.com/ActiveState/cli/internal/rtutils/ptr"
"github.com/ActiveState/cli/internal/singleton/uniqid"
"github.com/ActiveState/cli/internal/updater"
"github.com/ActiveState/cli/pkg/platform/authentication"
"github.com/ActiveState/cli/pkg/sysinfo"
)
type Reporter interface {
ID() string
Event(category, action, source, label string, dimensions *dimensions.Values) error
}
// Client instances send analytics events to GA and S3 endpoints without delay. It is only supposed to be used inside the `state-svc`. All other processes should use the DefaultClient.
type Client struct {
customDimensions *dimensions.Values
cfg *config.Instance
eventWaitGroup *sync.WaitGroup
sendReports bool
reporters []Reporter
sequence int
auth *authentication.Auth
source string
}
var _ analytics.Dispatcher = &Client{}
// New initializes the analytics instance with all custom dimensions known at this time
func New(source string, cfg *config.Instance, auth *authentication.Auth, out output.Outputer) *Client {
a := &Client{
eventWaitGroup: &sync.WaitGroup{},
sendReports: true,
auth: auth,
source: source,
}
installSource, err := storage.InstallSource()
if err != nil {
multilog.Error("Could not detect installSource: %s", errs.JoinMessage(err))
}
deviceID := uniqid.Text()
osName := sysinfo.OS().String()
osVersion := "unknown"
osvInfo, err := sysinfo.OSVersion()
if err != nil {
multilog.Log(logging.ErrorNoStacktrace, rollbar.Error)("Could not detect osVersion: %v", err)
}
if osvInfo != nil {
osVersion = osvInfo.Version
}
var sessionToken string
var tag string
if cfg != nil {
sessionToken = cfg.GetString(anaConsts.CfgSessionToken)
var ok bool
tag, ok = os.LookupEnv(constants.UpdateTagEnvVarName)
if !ok {
tag = cfg.GetString(updater.CfgUpdateTag)
}
a.cfg = cfg
}
a.readConfig()
configMediator.AddListener(constants.ReportAnalyticsConfig, a.readConfig)
userID := ""
if auth != nil && auth.UserID() != nil {
userID = string(*auth.UserID())
}
interactive := false
if out != nil {
interactive = out.Config().Interactive
}
customDimensions := &dimensions.Values{
Version: ptr.To(constants.Version),
ChannelName: ptr.To(constants.ChannelName),
OSName: ptr.To(osName),
OSVersion: ptr.To(osVersion),
InstallSource: ptr.To(installSource),
UniqID: ptr.To(deviceID),
SessionToken: ptr.To(sessionToken),
UpdateTag: ptr.To(tag),
UserID: ptr.To(userID),
Flags: ptr.To(dimensions.CalculateFlags()),
InstanceID: ptr.To(instanceid.ID()),
Command: ptr.To(osutils.ExecutableName()),
Sequence: ptr.To(0),
CI: ptr.To(condition.OnCI()),
Interactive: ptr.To(interactive),
ActiveStateCI: ptr.To(condition.InActiveStateCI()),
}
a.customDimensions = customDimensions
url := a.cfg.GetString(constants.AnalyticsPixelOverrideConfig)
// Register reporters
if condition.InTest() {
logging.Debug("Using test reporter")
a.NewReporter(reporters.NewTestReporter(reporters.TestReportFilepath(), url))
logging.Debug("Using test reporter as instructed by env")
} else if v := os.Getenv(constants.AnalyticsLogEnvVarName); v != "" {
a.NewReporter(reporters.NewTestReporter(v, url))
} else {
a.NewReporter(reporters.NewPixelReporter(url))
}
return a
}
func (a *Client) readConfig() {
doNotReport := (!a.cfg.Closed() && !a.cfg.GetBool(constants.ReportAnalyticsConfig)) ||
strings.ToLower(os.Getenv(constants.DisableAnalyticsEnvVarName)) == "true"
a.sendReports = !doNotReport
logging.Debug("Sending Google Analytics reports? %v", a.sendReports)
}
func (a *Client) NewReporter(rep Reporter) {
a.reporters = append(a.reporters, rep)
}
func (a *Client) Wait() {
a.eventWaitGroup.Wait()
}
// Events returns a channel to feed eventData directly to the report loop
func (a *Client) report(category, action, source, label string, dimensions *dimensions.Values) {
if !a.sendReports {
return
}
for _, reporter := range a.reporters {
if err := reporter.Event(category, action, source, label, dimensions); err != nil {
logging.Debug(
"Reporter failed: %s, category: %s, action: %s, error: %s",
reporter.ID(), category, action, errs.JoinMessage(err),
)
}
}
}
func (a *Client) Event(category, action string, dims ...*dimensions.Values) {
a.EventWithLabel(category, action, "", dims...)
}
func mergeDimensions(target *dimensions.Values, dims ...*dimensions.Values) *dimensions.Values {
actualDims := target.Clone()
for _, dim := range dims {
if dim == nil {
continue
}
actualDims.Merge(dim)
}
return actualDims
}
func (a *Client) EventWithLabel(category, action, label string, dims ...*dimensions.Values) {
a.EventWithSourceAndLabel(category, action, a.source, label, dims...)
}
// EventWithSource should only be used by clients forwarding events on behalf of another source.
// Otherwise, use Event().
func (a *Client) EventWithSource(category, action, source string, dims ...*dimensions.Values) {
a.EventWithSourceAndLabel(category, action, source, "", dims...)
}
// EventWithSourceAndLabel should only be used by clients forwarding events on behalf of another
// source (for example, state-svc forwarding events on behalf of State Tool or an executor).
// Otherwise, use EventWithLabel().
func (a *Client) EventWithSourceAndLabel(category, action, source, label string, dims ...*dimensions.Values) {
if a.customDimensions == nil {
if condition.InUnitTest() {
return
}
if !condition.BuiltViaCI() {
panic("Trying to send analytics without configuring the Analytics instance.")
}
multilog.Critical("Trying to send analytics event without configuring the Analytics instance.")
return
}
if a.auth != nil && a.auth.UserID() != nil {
a.customDimensions.UserID = ptr.To(string(*a.auth.UserID()))
}
a.customDimensions.Sequence = ptr.To(a.sequence)
actualDims := mergeDimensions(a.customDimensions, dims...)
if a.sequence == *actualDims.Sequence {
// Increment the sequence number unless dims overrides it (e.g. heartbeats use -1).
a.sequence++
}
if err := actualDims.PreProcess(); err != nil {
multilog.Critical("Analytics dimensions cannot be processed properly: %s", errs.JoinMessage(err))
}
a.eventWaitGroup.Add(1)
// We do not wait for the events to be processed, just scheduling them
go func() {
defer a.eventWaitGroup.Done()
defer func() { handlePanics(recover(), debug.Stack()) }()
a.report(category, action, source, label, actualDims)
}()
}
func handlePanics(err interface{}, stack []byte) {
if err == nil {
return
}
multilog.Error("Panic in state-svc analytics: %v", err)
logging.Debug("Stack: %s", string(stack))
}
func (a *Client) Close() {
a.Wait()
a.sendReports = false
}