/
send_events.go
216 lines (198 loc) · 9.18 KB
/
send_events.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
213
214
215
216
package ldcomponents
import (
"strings"
"time"
"gopkg.in/launchdarkly/go-sdk-common.v2/lduser"
"gopkg.in/launchdarkly/go-sdk-common.v2/ldvalue"
ldevents "gopkg.in/launchdarkly/go-sdk-events.v1"
"gopkg.in/launchdarkly/go-server-sdk.v5/interfaces"
"gopkg.in/launchdarkly/go-server-sdk.v5/internal"
)
const (
// DefaultEventsBaseURI is the default value for EventProcessorBuilder.BaseURI.
DefaultEventsBaseURI = "https://events.launchdarkly.com"
// DefaultEventsCapacity is the default value for EventProcessorBuilder.Capacity.
DefaultEventsCapacity = 10000
// DefaultDiagnosticRecordingInterval is the default value for EventProcessorBuilder.DiagnosticRecordingInterval.
DefaultDiagnosticRecordingInterval = 15 * time.Minute
// DefaultFlushInterval is the default value for EventProcessorBuilder.FlushInterval.
DefaultFlushInterval = 5 * time.Second
// DefaultUserKeysCapacity is the default value for EventProcessorBuilder.UserKeysCapacity.
DefaultUserKeysCapacity = 1000
// DefaultUserKeysFlushInterval is the default value for EventProcessorBuilder.UserKeysFlushInterval.
DefaultUserKeysFlushInterval = 5 * time.Minute
// MinimumDiagnosticRecordingInterval is the minimum value for EventProcessorBuilder.DiagnosticRecordingInterval.
MinimumDiagnosticRecordingInterval = 60 * time.Second
)
// EventProcessorBuilder provides methods for configuring analytics event behavior.
//
// See SendEvents for usage.
type EventProcessorBuilder struct {
allAttributesPrivate bool
baseURI string
capacity int
diagnosticRecordingInterval time.Duration
flushInterval time.Duration
inlineUsersInEvents bool
logUserKeyInErrors bool
privateAttributeNames []lduser.UserAttribute
userKeysCapacity int
userKeysFlushInterval time.Duration
}
// SendEvents returns a configuration builder for analytics event delivery.
//
// The default configuration has events enabled with default settings. If you want to customize this
// behavior, call this method to obtain a builder, change its properties with the EventProcessorBuilder
// methods, and store it in Config.Events:
//
// config := ld.Config{
// Events: ldcomponents.SendEvents().Capacity(5000).FlushInterval(2 * time.Second),
// }
//
// To disable analytics events, use NoEvents instead of SendEvents.
func SendEvents() *EventProcessorBuilder {
return &EventProcessorBuilder{
baseURI: DefaultEventsBaseURI,
capacity: DefaultEventsCapacity,
diagnosticRecordingInterval: DefaultDiagnosticRecordingInterval,
flushInterval: DefaultFlushInterval,
userKeysCapacity: DefaultUserKeysCapacity,
userKeysFlushInterval: DefaultUserKeysFlushInterval,
}
}
// CreateEventProcessor is called by the SDK to create the event processor instance.
func (b *EventProcessorBuilder) CreateEventProcessor(
context interfaces.ClientContext,
) (ldevents.EventProcessor, error) {
loggers := context.GetLogging().GetLoggers()
eventSender := ldevents.NewServerSideEventSender(context.GetHTTP().CreateHTTPClient(),
context.GetBasic().SDKKey, b.baseURI, context.GetHTTP().GetDefaultHeaders(), loggers)
eventsConfig := ldevents.EventsConfiguration{
AllAttributesPrivate: b.allAttributesPrivate,
Capacity: b.capacity,
DiagnosticRecordingInterval: b.diagnosticRecordingInterval,
EventSender: eventSender,
FlushInterval: b.flushInterval,
InlineUsersInEvents: b.inlineUsersInEvents,
Loggers: loggers,
LogUserKeyInErrors: b.logUserKeyInErrors,
PrivateAttributeNames: b.privateAttributeNames,
UserKeysCapacity: b.userKeysCapacity,
UserKeysFlushInterval: b.userKeysFlushInterval,
}
if hdm, ok := context.(internal.HasDiagnosticsManager); ok {
eventsConfig.DiagnosticsManager = hdm.GetDiagnosticsManager()
}
return ldevents.NewDefaultEventProcessor(eventsConfig), nil
}
// AllAttributesPrivate sets or not all optional user attributes should be hidden from LaunchDarkly.
//
// If this is true, all user attribute values (other than the key) will be private, not just the attributes
// specified with PrivateAttributeNames or on a per-user basis with UserBuilder methods. By default, it is false.
func (b *EventProcessorBuilder) AllAttributesPrivate(value bool) *EventProcessorBuilder {
b.allAttributesPrivate = value
return b
}
// BaseURI sets a custom base URI for the events service.
//
// You will only need to change this value in the following cases:
//
// 1. You are using the Relay Proxy (https://docs.launchdarkly.com/docs/the-relay-proxy). Set BaseURI to the base URI of
// the Relay Proxy instance.
//
// 2. You are connecting to a test server or anything else other than the standard LaunchDarkly service.
func (b *EventProcessorBuilder) BaseURI(baseURI string) *EventProcessorBuilder {
if baseURI == "" {
b.baseURI = DefaultEventsBaseURI
} else {
b.baseURI = strings.TrimRight(baseURI, "/")
}
return b
}
// Capacity sets the capacity of the events buffer.
//
// The client buffers up to this many events in memory before flushing. If the capacity is exceeded before
// the buffer is flushed (see FlushInterval), events will be discarded. Increasing the capacity means that
// events are less likely to be discarded, at the cost of consuming more memory.
//
// The default value is DefaultEventsCapacity.
func (b *EventProcessorBuilder) Capacity(capacity int) *EventProcessorBuilder {
b.capacity = capacity
return b
}
// DiagnosticRecordingInterval sets the interval at which periodic diagnostic data is sent.
//
// The default value is DefaultDiagnosticRecordingInterval; the minimum value is MinimumDiagnosticRecordingInterval.
// This property is ignored if Config.DiagnosticOptOut is set to true.
func (b *EventProcessorBuilder) DiagnosticRecordingInterval(interval time.Duration) *EventProcessorBuilder {
if interval < MinimumDiagnosticRecordingInterval {
b.diagnosticRecordingInterval = MinimumDiagnosticRecordingInterval
} else {
b.diagnosticRecordingInterval = interval
}
return b
}
// FlushInterval sets the interval between flushes of the event buffer.
//
// Decreasing the flush interval means that the event buffer is less likely to reach capacity (see Capacity).
//
// The default value is DefaultFlushInterval.
func (b *EventProcessorBuilder) FlushInterval(interval time.Duration) *EventProcessorBuilder {
b.flushInterval = interval
return b
}
// InlineUsersInEvents sets whether to include full user details in every analytics event.
//
// The default is false: events will only include the user key, except for one "index" event that provides
// the full details for the user.
func (b *EventProcessorBuilder) InlineUsersInEvents(value bool) *EventProcessorBuilder {
b.inlineUsersInEvents = value
return b
}
// PrivateAttributeNames marks a set of attribute names as always private.
//
// Any users sent to LaunchDarkly with this configuration active will have attributes with these
// names removed. This is in addition to any attributes that were marked as private for an
// individual user with UserBuilder methods. Setting AllAttributePrivate to true overrides this.
//
// config := ld.Config{
// Events: ldcomponents.SendEvents().
// PrivateAttributeNames(lduser.EmailAttribute, lduser.UserAttribute("some-custom-attribute")),
// }
func (b *EventProcessorBuilder) PrivateAttributeNames(attributes ...lduser.UserAttribute) *EventProcessorBuilder {
b.privateAttributeNames = attributes
return b
}
// UserKeysCapacity sets the number of user keys that the event processor can remember at any one time.
//
// To avoid sending duplicate user details in analytics events, the SDK maintains a cache of recently
// seen user keys, expiring at an interval set by UserKeysFlushInterval.
//
// The default value is DefaultUserKeysCapacity.
func (b *EventProcessorBuilder) UserKeysCapacity(userKeysCapacity int) *EventProcessorBuilder {
b.userKeysCapacity = userKeysCapacity
return b
}
// UserKeysFlushInterval sets the interval at which the event processor will reset its cache of known user keys.
//
// The default value is DefaultUserKeysFlushInterval.
func (b *EventProcessorBuilder) UserKeysFlushInterval(interval time.Duration) *EventProcessorBuilder {
b.userKeysFlushInterval = interval
return b
}
// DescribeConfiguration is used internally by the SDK to inspect the configuration.
func (b *EventProcessorBuilder) DescribeConfiguration() ldvalue.Value {
return ldvalue.ObjectBuild().
Set("allAttributesPrivate", ldvalue.Bool(b.allAttributesPrivate)).
Set("customEventsURI", ldvalue.Bool(b.baseURI != "" && b.baseURI != DefaultEventsBaseURI)).
Set("diagnosticRecordingIntervalMillis", durationToMillisValue(b.diagnosticRecordingInterval)).
Set("eventsCapacity", ldvalue.Int(b.capacity)).
Set("eventsFlushIntervalMillis", durationToMillisValue(b.flushInterval)).
Set("inlineUsersInEvents", ldvalue.Bool(b.inlineUsersInEvents)).
Set("userKeysCapacity", ldvalue.Int(b.userKeysCapacity)).
Set("userKeysFlushIntervalMillis", durationToMillisValue(b.userKeysFlushInterval)).
Build()
}
func durationToMillisValue(d time.Duration) ldvalue.Value {
return ldvalue.Float64(float64(uint64(d / time.Millisecond)))
}