-
Notifications
You must be signed in to change notification settings - Fork 18
/
ldclient_events.go
353 lines (315 loc) · 11.9 KB
/
ldclient_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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package ldclient
import (
gocontext "context"
"github.com/launchdarkly/go-sdk-common/v3/ldcontext"
"github.com/launchdarkly/go-sdk-common/v3/ldmigration"
"github.com/launchdarkly/go-sdk-common/v3/ldreason"
"github.com/launchdarkly/go-sdk-common/v3/ldvalue"
ldevents "github.com/launchdarkly/go-sdk-events/v3"
ldeval "github.com/launchdarkly/go-server-sdk-evaluation/v3"
"github.com/launchdarkly/go-server-sdk/v7/interfaces"
"github.com/launchdarkly/go-server-sdk/v7/interfaces/flagstate"
"github.com/launchdarkly/go-server-sdk/v7/ldcomponents"
"github.com/launchdarkly/go-server-sdk/v7/subsystems"
)
// This file contains internal support code for LDClient's interactions with the analytics event pipeline.
//
// General implementation notes:
//
// Under normal circumstances, an analytics event is generated whenever 1. a flag is evaluated explicitly
// with a Variation method, 2. a flag is evaluated indirectly as a prerequisite, or 3. the application
// explicitly generates an event by calling Identify or Track. This event is submitted to the configured
// EventProcessor's SendEvent method; the EventProcessor then does any necessary processing and eventually
// delivers the event data to LaunchDarkly, either as a full event or in summary data. The implementation
// of that logic is all in go-sdk-events (since it can be used outside of the SDK, as in ld-relay).
//
// When events are disabled, the EventProcessor is a null implementation that does nothing. It is safe to
// use that object, but LDClient still refrains from doing so if it knows events are disabled so that we
// can avoid a little bit of computational overhead. That's the reason for the IsNullEventProcessorFactory
// method.
type nullEventProcessorFactoryDescription interface {
IsNullEventProcessorFactory() bool
}
func isNullEventProcessorFactory(f subsystems.ComponentConfigurer[ldevents.EventProcessor]) bool {
if nf, ok := f.(nullEventProcessorFactoryDescription); ok {
return nf.IsNullEventProcessorFactory()
}
return false
}
func getEventProcessorFactory(config Config) subsystems.ComponentConfigurer[ldevents.EventProcessor] {
if config.Offline {
return ldcomponents.NoEvents()
}
if config.Events == nil {
return ldcomponents.SendEvents()
}
return config.Events
}
// This struct is used during evaluations to keep track of the event generation strategy we are using
// (with or without evaluation reasons). It captures all of the relevant state so that we do not need to
// create any more stateful objects, such as closures, to generate events during an evaluation. See
// CONTRIBUTING.md for performance issues with closures.
type eventsScope struct {
disabled bool
factory ldevents.EventFactory
prerequisiteEventRecorder ldeval.PrerequisiteFlagEventRecorder
}
func newDisabledEventsScope() eventsScope {
return eventsScope{disabled: true}
}
func newEventsScope(client *LDClient, withReasons bool) eventsScope {
factory := ldevents.NewEventFactory(withReasons, nil)
return eventsScope{
factory: factory,
prerequisiteEventRecorder: func(params ldeval.PrerequisiteFlagEvent) {
client.eventProcessor.RecordEvaluation(factory.NewEvaluationData(
ldevents.FlagEventProperties{
Key: params.PrerequisiteFlag.Key,
Version: params.PrerequisiteFlag.Version,
RequireFullEvent: params.PrerequisiteFlag.TrackEvents,
DebugEventsUntilDate: params.PrerequisiteFlag.DebugEventsUntilDate,
},
ldevents.Context(params.Context),
params.PrerequisiteResult.Detail,
params.PrerequisiteResult.IsExperiment,
ldvalue.Null(),
params.TargetFlagKey,
params.PrerequisiteFlag.SamplingRatio,
params.ExcludeFromSummaries,
))
},
}
}
// This implementation of interfaces.LDClientInterface delegates all client operations to the
// underlying LDClient, but suppresses the generation of analytics events.
type clientEventsDisabledDecorator struct {
client *LDClient
scope eventsScope
}
func newClientEventsDisabledDecorator(client *LDClient) interfaces.LDClientInterface {
return &clientEventsDisabledDecorator{client: client, scope: newDisabledEventsScope()}
}
func (c *clientEventsDisabledDecorator) BoolVariation(
key string,
context ldcontext.Context,
defaultVal bool,
) (bool, error) {
detail, _, err := c.client.variationWithHooks(gocontext.TODO(), key, context, ldvalue.Bool(defaultVal),
true, c.scope, boolVarFuncName)
return detail.Value.BoolValue(), err
}
func (c *clientEventsDisabledDecorator) BoolVariationDetail(key string, context ldcontext.Context, defaultVal bool) (
bool, ldreason.EvaluationDetail, error) {
detail, _, err := c.client.variationWithHooks(gocontext.TODO(), key, context, ldvalue.Bool(defaultVal),
true, c.scope, boolVarDetailFuncName)
return detail.Value.BoolValue(), detail, err
}
func (c *clientEventsDisabledDecorator) BoolVariationCtx(
ctx gocontext.Context,
key string,
context ldcontext.Context,
defaultVal bool,
) (bool, error) {
detail, _, err := c.client.variationWithHooks(ctx, key, context, ldvalue.Bool(defaultVal),
true, c.scope, boolVarExFuncName)
return detail.Value.BoolValue(), err
}
func (c *clientEventsDisabledDecorator) BoolVariationDetailCtx(
ctx gocontext.Context,
key string,
context ldcontext.Context,
defaultVal bool,
) (
bool, ldreason.EvaluationDetail, error) {
detail, _, err := c.client.variationWithHooks(ctx, key, context, ldvalue.Bool(defaultVal),
true, c.scope, boolVarDetailExFuncName)
return detail.Value.BoolValue(), detail, err
}
func (c *clientEventsDisabledDecorator) IntVariation(
key string,
context ldcontext.Context,
defaultVal int,
) (int, error) {
detail, _, err := c.client.variationWithHooks(gocontext.TODO(), key, context, ldvalue.Int(defaultVal),
true, c.scope, intVarFuncName)
return detail.Value.IntValue(), err
}
func (c *clientEventsDisabledDecorator) IntVariationDetail(key string, context ldcontext.Context, defaultVal int) (
int, ldreason.EvaluationDetail, error) {
detail, _, err := c.client.variationWithHooks(gocontext.TODO(), key, context, ldvalue.Int(defaultVal),
true, c.scope, intVarDetailFuncName)
return detail.Value.IntValue(), detail, err
}
func (c *clientEventsDisabledDecorator) IntVariationCtx(
ctx gocontext.Context,
key string,
context ldcontext.Context,
defaultVal int,
) (int, error) {
detail, _, err := c.client.variationWithHooks(ctx, key, context, ldvalue.Int(defaultVal),
true, c.scope, intVarExFuncName)
return detail.Value.IntValue(), err
}
func (c *clientEventsDisabledDecorator) IntVariationDetailCtx(
ctx gocontext.Context,
key string,
context ldcontext.Context,
defaultVal int,
) (
int, ldreason.EvaluationDetail, error) {
detail, _, err := c.client.variationWithHooks(ctx, key, context, ldvalue.Int(defaultVal),
true, c.scope, intVarDetailExFuncName)
return detail.Value.IntValue(), detail, err
}
func (c *clientEventsDisabledDecorator) Float64Variation(key string, context ldcontext.Context, defaultVal float64) (
float64, error) {
detail, _, err := c.client.variationWithHooks(gocontext.TODO(), key, context, ldvalue.Float64(defaultVal),
true, c.scope, floatVarFuncName)
return detail.Value.Float64Value(), err
}
func (c *clientEventsDisabledDecorator) Float64VariationDetail(
key string,
context ldcontext.Context,
defaultVal float64,
) (
float64, ldreason.EvaluationDetail, error) {
detail, _, err := c.client.variationWithHooks(gocontext.TODO(), key, context, ldvalue.Float64(defaultVal),
true, c.scope, floatVarDetailFuncName)
return detail.Value.Float64Value(), detail, err
}
func (c *clientEventsDisabledDecorator) Float64VariationCtx(
ctx gocontext.Context,
key string,
context ldcontext.Context,
defaultVal float64,
) (
float64, error) {
detail, _, err := c.client.variationWithHooks(ctx, key, context, ldvalue.Float64(defaultVal),
true, c.scope, floatVarExFuncName)
return detail.Value.Float64Value(), err
}
func (c *clientEventsDisabledDecorator) Float64VariationDetailCtx(
ctx gocontext.Context,
key string,
context ldcontext.Context,
defaultVal float64,
) (
float64, ldreason.EvaluationDetail, error) {
detail, _, err := c.client.variationWithHooks(ctx, key, context, ldvalue.Float64(defaultVal),
true, c.scope, floatVarDetailExFuncName)
return detail.Value.Float64Value(), detail, err
}
func (c *clientEventsDisabledDecorator) StringVariation(key string, context ldcontext.Context, defaultVal string) (
string, error) {
detail, _, err := c.client.variationWithHooks(gocontext.TODO(), key, context, ldvalue.String(defaultVal),
true, c.scope, stringVarExFuncName)
return detail.Value.StringValue(), err
}
func (c *clientEventsDisabledDecorator) StringVariationDetail(
key string,
context ldcontext.Context,
defaultVal string,
) (
string, ldreason.EvaluationDetail, error) {
detail, _, err := c.client.variationWithHooks(gocontext.TODO(), key, context, ldvalue.String(defaultVal),
true, c.scope, stringVarDetailFuncName)
return detail.Value.StringValue(), detail, err
}
func (c *clientEventsDisabledDecorator) StringVariationCtx(
ctx gocontext.Context,
key string,
context ldcontext.Context,
defaultVal string,
) (
string, error) {
detail, _, err := c.client.variationWithHooks(ctx, key, context, ldvalue.String(defaultVal), true, c.scope,
stringVarExFuncName)
return detail.Value.StringValue(), err
}
func (c *clientEventsDisabledDecorator) StringVariationDetailCtx(
ctx gocontext.Context,
key string,
context ldcontext.Context,
defaultVal string,
) (
string, ldreason.EvaluationDetail, error) {
detail, _, err := c.client.variationWithHooks(ctx, key, context, ldvalue.String(defaultVal), true, c.scope,
stringVarDetailExFuncName)
return detail.Value.StringValue(), detail, err
}
func (c *clientEventsDisabledDecorator) MigrationVariation(
key string, context ldcontext.Context, defaultStage ldmigration.Stage,
) (ldmigration.Stage, interfaces.LDMigrationOpTracker, error) {
return c.client.migrationVariation(gocontext.TODO(), key, context, defaultStage, c.scope, migrationVarFuncName)
}
func (c *clientEventsDisabledDecorator) MigrationVariationCtx(
ctx gocontext.Context,
key string,
context ldcontext.Context,
defaultStage ldmigration.Stage,
) (ldmigration.Stage, interfaces.LDMigrationOpTracker, error) {
return c.client.migrationVariation(ctx, key, context, defaultStage, c.scope, migrationVarExFuncName)
}
func (c *clientEventsDisabledDecorator) JSONVariation(key string, context ldcontext.Context, defaultVal ldvalue.Value) (
ldvalue.Value, error) {
detail, _, err := c.client.variationWithHooks(
gocontext.TODO(),
key,
context,
defaultVal,
true,
c.scope,
jsonVarFuncName,
)
return detail.Value, err
}
func (c *clientEventsDisabledDecorator) JSONVariationDetail(
key string,
context ldcontext.Context,
defaultVal ldvalue.Value,
) (
ldvalue.Value, ldreason.EvaluationDetail, error) {
detail, _, err := c.client.variationWithHooks(
gocontext.TODO(),
key,
context,
defaultVal,
true,
c.scope,
jsonVarDetailFuncName,
)
return detail.Value, detail, err
}
func (c *clientEventsDisabledDecorator) AllFlagsState(
context ldcontext.Context,
options ...flagstate.Option,
) flagstate.AllFlags {
// Currently AllFlagsState never generates events anyway, so nothing is different here
return c.client.AllFlagsState(context, options...)
}
func (c *clientEventsDisabledDecorator) Identify(context ldcontext.Context) error {
return nil
}
func (c *clientEventsDisabledDecorator) TrackEvent(eventName string, context ldcontext.Context) error {
return nil
}
func (c *clientEventsDisabledDecorator) TrackData(
eventName string,
context ldcontext.Context,
data ldvalue.Value,
) error {
return nil
}
func (c *clientEventsDisabledDecorator) TrackMetric(eventName string, context ldcontext.Context, metricValue float64,
data ldvalue.Value) error {
return nil
}
func (c *clientEventsDisabledDecorator) TrackMigrationOp(event ldevents.MigrationOpEventData) error {
return nil
}
func (c *clientEventsDisabledDecorator) WithEventsDisabled(disabled bool) interfaces.LDClientInterface {
if disabled {
return c
}
return c.client
}