-
Notifications
You must be signed in to change notification settings - Fork 290
/
analytics_reporter.go
101 lines (87 loc) · 2.5 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
package engine
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.Hour * 1
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() {
for {
select {
case <-time.After(analyticsReportingInterval):
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 int
for _, m := range st.Manifests() {
if m.IsK8s() {
k8sCount++
if len(m.ImageTargets) == 0 {
unbuiltCount++
}
}
if m.IsDC() {
dcCount++
}
for _, it := range m.ImageTargets {
if !it.AnyFastBuildInfo().Empty() {
anyFastbuildCount++
if it.IsFastBuild() {
fastbuildBaseCount++
}
break
}
if !it.AnyLiveUpdateInfo().Empty() {
liveUpdateCount++
}
}
}
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["tiltfile.error"] = tiltfileIsInError
ar.a.Incr("up.running", stats)
}