-
Notifications
You must be signed in to change notification settings - Fork 288
/
analytics_reporter.go
128 lines (113 loc) · 3.37 KB
/
analytics_reporter.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
package analytics
import (
"context"
"strconv"
"time"
"github.com/windmilleng/tilt/internal/analytics"
"github.com/windmilleng/tilt/internal/store"
)
// How often to periodically report data for analytics while Tilt is running
const analyticsReportingInterval = time.Minute * 15
type AnalyticsReporter struct {
a *analytics.TiltAnalytics
store *store.Store
started bool
}
func (ar *AnalyticsReporter) OnChange(ctx context.Context, st store.RStore) {
if ar.started {
return
}
state := st.RLockState()
defer st.RUnlockState()
// wait until state has been kinda initialized
if !state.TiltStartTime.IsZero() && state.LastTiltfileError() == nil {
ar.started = true
go func() {
select {
case <-time.After(time.Minute * 2):
ar.report() // report once pretty soon after startup...
case <-ctx.Done():
return
}
for {
select {
case <-time.After(analyticsReportingInterval):
// and once every <interval> thereafter
ar.report()
case <-ctx.Done():
return
}
}
}()
}
}
var _ store.Subscriber = &AnalyticsReporter{}
func ProvideAnalyticsReporter(a *analytics.TiltAnalytics, st *store.Store) *AnalyticsReporter {
return &AnalyticsReporter{a, st, false}
}
func (ar *AnalyticsReporter) report() {
st := ar.store.RLockState()
defer ar.store.RUnlockState()
var dcCount, k8sCount, fastbuildBaseCount, anyFastbuildCount, liveUpdateCount,
unbuiltCount, sameImgMultiContainerLiveUpdate, multiImgLiveUpdate int
for _, m := range st.Manifests() {
var refInjectCounts map[string]int
if m.IsK8s() {
k8sCount++
refInjectCounts = m.K8sTarget().RefInjectCounts()
if len(m.ImageTargets) == 0 {
unbuiltCount++
}
}
if m.IsDC() {
dcCount++
}
var seenLU, multiImgLU, multiContainerLU bool
for _, it := range m.ImageTargets {
if !it.AnyFastBuildInfo().Empty() {
anyFastbuildCount++
if it.IsFastBuild() {
fastbuildBaseCount++
}
break
}
if !it.AnyLiveUpdateInfo().Empty() {
if !seenLU {
seenLU = true
liveUpdateCount++
} else if !multiImgLU {
multiImgLU = true
}
multiContainerLU = multiContainerLU ||
refInjectCounts[it.ConfigurationRef.String()] > 0
}
}
if multiContainerLU {
sameImgMultiContainerLiveUpdate++
}
if multiImgLU {
multiImgLiveUpdate++
}
}
stats := map[string]string{
"up.starttime": st.TiltStartTime.Format(time.RFC3339),
"builds.completed_count": strconv.Itoa(st.CompletedBuildCount),
}
tiltfileIsInError := "false"
if st.LastTiltfileError() != nil {
tiltfileIsInError = "true"
} else {
// only report when there's no tiltfile error, to avoid polluting aggregations
stats["resource.count"] = strconv.Itoa(len(st.ManifestDefinitionOrder))
stats["resource.dockercompose.count"] = strconv.Itoa(dcCount)
stats["resource.k8s.count"] = strconv.Itoa(k8sCount)
stats["resource.fastbuild.count"] = strconv.Itoa(fastbuildBaseCount)
stats["resource.anyfastbuild.count"] = strconv.Itoa(anyFastbuildCount)
stats["resource.liveupdate.count"] = strconv.Itoa(liveUpdateCount)
stats["resource.unbuiltresources.count"] = strconv.Itoa(unbuiltCount)
stats["resource.sameimagemultiplecontainerliveupdate.count"] = strconv.Itoa(sameImgMultiContainerLiveUpdate)
stats["resource.multipleimageliveupdate.count"] = strconv.Itoa(multiImgLiveUpdate)
}
stats["tiltfile.error"] = tiltfileIsInError
ar.a.Incr("up.running", stats)
}