-
Notifications
You must be signed in to change notification settings - Fork 5
/
event.go
212 lines (191 loc) · 5.2 KB
/
event.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
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
package featureprobe
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"sync"
"time"
)
type EventRecorder struct {
auth string
eventsUrl string
flushInterval time.Duration
incomingEvents []interface{}
access Access
httpClient http.Client
mu sync.Mutex
wg sync.WaitGroup
startOnce sync.Once
stopOnce sync.Once
stopChan chan struct{}
ticker *time.Ticker
}
type AccessEvent struct {
Kind string `json:"kind"`
Time int64 `json:"time"`
User string `json:"user"`
Key string `json:"key"`
Value interface{} `json:"value"`
VariationIndex *int `json:"variationIndex"`
Version *uint64 `json:"version"`
}
type DebugEvent struct {
Kind string `json:"kind"`
Time int64 `json:"time"`
User string `json:"user"`
UserDetail map[string]interface{} `json:"userDetail"`
Key string `json:"key"`
Value interface{} `json:"value"`
VariationIndex *int `json:"variationIndex"`
RuleIndex *int `json:"ruleIndex"`
Version *uint64 `json:"version"`
Reason string `json:"reason"`
}
type CustomEvent struct {
Kind string `json:"kind"`
Time int64 `json:"time"`
User string `json:"user"`
Name string `json:"name"`
Value *float64 `json:"value"`
}
type PackedData struct {
Events []interface{} `json:"events"`
Access Access `json:"access"`
}
type Access struct {
StartTime int64 `json:"startTime"`
EndTime int64 `json:"endTime"`
Counters map[string][]ToggleCounter `json:"counters"`
}
type ToggleCounter struct {
Value interface{} `json:"value"`
Version *uint64 `json:"version"`
Index *int `json:"index"`
Count int `json:"count"`
}
type Variation struct {
Key string `json:"key"`
Index *int `json:"index"`
Version *uint64 `json:"version"`
}
type CountValue struct {
Count int `json:"count"`
Value interface{} `json:"value"`
}
func NewEventRecorder(eventsUrl string, flushInterval time.Duration, auth string) EventRecorder {
return EventRecorder{
auth: auth,
eventsUrl: eventsUrl,
flushInterval: flushInterval,
incomingEvents: []interface{}{},
access: newAccess(),
httpClient: newHttpClient(flushInterval),
stopChan: make(chan struct{}),
}
}
func newAccess() Access {
return Access{
Counters: make(map[string][]ToggleCounter),
}
}
func nowToggleCounter(value interface{}, version *uint64, index *int) ToggleCounter {
return ToggleCounter{
value,
version,
index,
1,
}
}
func (e *EventRecorder) Start() {
e.wg.Add(1)
e.startOnce.Do(func() {
e.ticker = time.NewTicker(e.flushInterval)
go func() {
for {
select {
case <-e.stopChan:
e.doFlush()
e.wg.Done()
return
case <-e.ticker.C:
e.doFlush()
}
}
}()
})
}
func (e *EventRecorder) doFlush() {
events := make([]interface{}, 0)
e.mu.Lock()
events, e.incomingEvents = e.incomingEvents, events
packedData := e.buildPackedData(events)
e.access = newAccess()
e.mu.Unlock()
if len(events) == 0 && len(packedData[0].Access.Counters) == 0 {
return
}
body, _ := json.Marshal(packedData)
req, err := http.NewRequest(http.MethodPost, e.eventsUrl, bytes.NewBuffer(body))
if err != nil {
fmt.Printf("%s\n", err)
return
}
req.Header.Add("Authorization", e.auth)
req.Header.Set("Content-Type", "application/json")
req.Header.Add("User-Agent", USER_AGENT)
_, err = e.httpClient.Do(req)
if err != nil {
fmt.Printf("Report event fails: %s\n", err)
}
}
func (e *EventRecorder) buildPackedData(events []interface{}) []PackedData {
e.access.EndTime = time.Now().UnixNano() / 1e6
p := PackedData{Access: e.access, Events: events}
return []PackedData{p}
}
func (e *EventRecorder) addAccess(event AccessEvent) {
if len(e.access.Counters) == 0 {
e.access.StartTime = time.Now().UnixNano() / 1e6
}
counters, ok := e.access.Counters[event.Key]
if ok {
for index, counter := range counters {
if *counter.Version == *event.Version && *counter.Index == *event.VariationIndex {
counters[index].Count = counter.Count + 1
return
}
}
e.access.Counters[event.Key] = append(counters,
nowToggleCounter(event.Value, event.Version, event.VariationIndex))
} else {
e.access.Counters[event.Key] = []ToggleCounter{
nowToggleCounter(event.Value, event.Version, event.VariationIndex)}
}
}
func (e *EventRecorder) RecordAccess(event AccessEvent, trackAccessEvents bool) {
e.mu.Lock()
if trackAccessEvents {
e.incomingEvents = append(e.incomingEvents, event)
}
e.addAccess(event)
e.mu.Unlock()
}
func (e *EventRecorder) RecordDebugAccess(debugEvent DebugEvent) {
e.mu.Lock()
e.incomingEvents = append(e.incomingEvents, debugEvent)
e.mu.Unlock()
}
func (e *EventRecorder) RecordCustom(event CustomEvent) {
e.mu.Lock()
e.incomingEvents = append(e.incomingEvents, event)
e.mu.Unlock()
}
func (e *EventRecorder) Stop() {
if e.stopChan != nil {
e.stopOnce.Do(func() {
close(e.stopChan)
})
}
e.wg.Wait()
}