-
Notifications
You must be signed in to change notification settings - Fork 4
/
events_output.go
184 lines (155 loc) · 5.75 KB
/
events_output.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
package ldevents
import (
"gopkg.in/launchdarkly/go-jsonstream.v1/jwriter"
"gopkg.in/launchdarkly/go-sdk-common.v2/ldtime"
)
// In this file we create the analytics event JSON data that we send to LaunchDarkly. This does not
// always correspond to the shape of the event objects that are fed into EventProcessor.
// Event types
const (
FeatureRequestEventKind = "feature"
FeatureDebugEventKind = "debug"
CustomEventKind = "custom"
IdentifyEventKind = "identify"
AliasEventKind = "alias"
IndexEventKind = "index"
SummaryEventKind = "summary"
)
type eventOutputFormatter struct {
userFilter userFilter
config EventsConfiguration
}
func (ef eventOutputFormatter) makeOutputEvents(events []commonEvent, summary eventSummary) ([]byte, int) {
n := len(events)
w := jwriter.NewWriter()
arr := w.Array()
for _, e := range events {
ef.writeOutputEvent(&w, e)
}
if len(summary.counters) > 0 {
ef.writeSummaryEvent(&w, summary)
n++
}
if n > 0 {
arr.End()
return w.Bytes(), n
}
return nil, 0
}
func (ef eventOutputFormatter) writeOutputEvent(w *jwriter.Writer, evt commonEvent) {
obj := w.Object()
switch evt := evt.(type) {
case FeatureRequestEvent:
kind := FeatureRequestEventKind
if evt.Debug {
kind = FeatureDebugEventKind
}
beginEventFields(&obj, kind, evt.BaseEvent.CreationDate)
obj.Name("key").String(evt.Key)
obj.Maybe("version", evt.Version.IsDefined()).Int(evt.Version.IntValue())
writeContextKind(&obj, evt.User.GetAnonymous())
if ef.config.InlineUsersInEvents || evt.Debug {
ef.userFilter.writeUser(obj.Name("user"), evt.User)
} else {
obj.Name("userKey").String(evt.User.GetKey())
}
obj.Maybe("variation", evt.Variation.IsDefined()).Int(evt.Variation.IntValue())
evt.Value.WriteToJSONWriter(obj.Name("value"))
evt.Default.WriteToJSONWriter(obj.Name("default"))
obj.Maybe("prereqOf", evt.PrereqOf.IsDefined()).String(evt.PrereqOf.StringValue())
if evt.Reason.GetKind() != "" {
evt.Reason.WriteToJSONWriter(obj.Name("reason"))
}
case CustomEvent:
beginEventFields(&obj, CustomEventKind, evt.BaseEvent.CreationDate)
obj.Name("key").String(evt.Key)
if !evt.Data.IsNull() {
evt.Data.WriteToJSONWriter(obj.Name("data"))
}
writeContextKind(&obj, evt.User.GetAnonymous())
if ef.config.InlineUsersInEvents {
ef.userFilter.writeUser(obj.Name("user"), evt.User)
} else {
obj.Name("userKey").String(evt.User.GetKey())
}
obj.Maybe("metricValue", evt.HasMetric).Float64(evt.MetricValue)
case IdentifyEvent:
beginEventFields(&obj, IdentifyEventKind, evt.BaseEvent.CreationDate)
obj.Name("key").String(evt.User.GetKey())
ef.userFilter.writeUser(obj.Name("user"), evt.User)
case AliasEvent:
beginEventFields(&obj, AliasEventKind, evt.CreationDate)
obj.Name("key").String(evt.CurrentKey)
obj.Name("contextKind").String(evt.CurrentKind)
obj.Name("previousKey").String(evt.PreviousKey)
obj.Name("previousContextKind").String(evt.PreviousKind)
case indexEvent:
beginEventFields(&obj, IndexEventKind, evt.BaseEvent.CreationDate)
ef.userFilter.writeUser(obj.Name("user"), evt.User)
}
obj.End()
}
func beginEventFields(obj *jwriter.ObjectState, kind string, creationDate ldtime.UnixMillisecondTime) {
obj.Name("kind").String(kind)
obj.Name("creationDate").Float64(float64(creationDate))
}
func writeContextKind(obj *jwriter.ObjectState, anonymous bool) {
obj.Maybe("contextKind", anonymous).String("anonymousUser")
}
// Transforms the summary data into the format used for event sending.
func (ef eventOutputFormatter) writeSummaryEvent(w *jwriter.Writer, snapshot eventSummary) {
obj := w.Object()
obj.Name("kind").String(SummaryEventKind)
obj.Name("startDate").Float64(float64(snapshot.startDate))
obj.Name("endDate").Float64(float64(snapshot.endDate))
flagsObj := obj.Name("features").Object()
// snapshot.counters contains composite keys in any order, part of which is the flag key; we want to group
// them together by flag key. The following multi-pass logic allows us to do this without using maps. It is
// based on the corresponding Java SDK logic in EventOutputFormatter.java.
unprocessedKeys := make([]counterKey, 0, 100)
// starting with a fixed capacity allows this slice to live on the stack unless it grows past that limit
for key := range snapshot.counters {
unprocessedKeys = append(unprocessedKeys, key)
}
for i, key := range unprocessedKeys {
if key.key == "" {
continue // we've already consumed this one when we saw the same flag key further up
}
// Now we've got a flag key that we haven't seen before
flagKey := key.key
firstValue := snapshot.counters[key]
flagObj := flagsObj.Name(flagKey).Object()
firstValue.flagDefault.WriteToJSONWriter(flagObj.Name("default"))
countersArr := flagObj.Name("counters").Array()
// Iterate over the remainder of the list to find any more entries for the same flag key.
for j := i; j < len(unprocessedKeys); j++ {
var anotherKey counterKey
var anotherValue *counterValue
if j == i {
anotherKey = key
anotherValue = firstValue
} else {
anotherKey = unprocessedKeys[j]
if anotherKey.key != flagKey {
continue
}
anotherValue = snapshot.counters[anotherKey]
unprocessedKeys[j].key = "" // clear this one so we'll skip it in the next pass
}
counterObj := countersArr.Object()
counterObj.Maybe("variation", anotherKey.variation.IsDefined()).Int(anotherKey.variation.IntValue())
if anotherKey.version.IsDefined() {
counterObj.Name("version").Int(anotherKey.version.IntValue())
} else {
counterObj.Name("unknown").Bool(true)
}
anotherValue.flagValue.WriteToJSONWriter(counterObj.Name("value"))
counterObj.Name("count").Int(anotherValue.count)
counterObj.End()
}
countersArr.End()
flagObj.End()
}
flagsObj.End()
obj.End()
}