forked from statsig-io/go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_initialize_response.go
More file actions
456 lines (433 loc) · 16.2 KB
/
client_initialize_response.go
File metadata and controls
456 lines (433 loc) · 16.2 KB
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
package statsig
import (
"math/rand"
"strings"
)
type GCIRSessionReplayTrigger struct {
Values *[]string `json:"values,omitempty"`
PassesSampling *bool `json:"passes_sampling,omitempty"`
}
type ClientInitializeResponse struct {
FeatureGates map[string]GateInitializeResponse `json:"feature_gates"`
DynamicConfigs map[string]ConfigInitializeResponse `json:"dynamic_configs"`
LayerConfigs map[string]LayerInitializeResponse `json:"layer_configs"`
SdkParams map[string]string `json:"sdkParams"`
HasUpdates bool `json:"has_updates"`
Generator string `json:"generator"`
EvaluatedKeys map[string]interface{} `json:"evaluated_keys"`
Time int64 `json:"time"`
SDKInfo SDKInfo `json:"sdkInfo"`
User User `json:"user"`
HashUsed string `json:"hash_used"`
CanRecordSession *bool `json:"can_record_session,omitempty"`
SessionRecordingRate *float64 `json:"session_recording_rate,omitempty"`
RecordingBlocked *bool `json:"recording_blocked,omitempty"`
PassesSessionRecordingTargeting *bool `json:"passes_session_recording_targeting,omitempty"`
SessionRecordingEventTriggers *map[string]GCIRSessionReplayTrigger `json:"session_recording_event_triggers,omitempty"`
SessionRecordingExposureTriggers *map[string]GCIRSessionReplayTrigger `json:"session_recording_exposure_triggers,omitempty"`
}
type SDKInfo struct {
SDKType string `json:"sdkType"`
SDKVersion string `json:"sdkVersion"`
}
type BaseSpecInitializeResponse struct {
Name string `json:"name"`
RuleID string `json:"rule_id"`
IDType string `json:"id_type,omitempty"`
SecondaryExposures []SecondaryExposure `json:"secondary_exposures"`
ConfigType ConfigType `json:"config_type,omitempty"`
}
type GateInitializeResponse struct {
BaseSpecInitializeResponse
Value bool `json:"value"`
}
type ConfigInitializeResponse struct {
BaseSpecInitializeResponse
Value map[string]interface{} `json:"value"`
Group string `json:"group"`
IsDeviceBased bool `json:"is_device_based"`
IsExperimentActive *bool `json:"is_experiment_active,omitempty"`
IsUserInExperiment *bool `json:"is_user_in_experiment,omitempty"`
IsInLayer *bool `json:"is_in_layer,omitempty"`
ExplicitParameters *[]string `json:"explicit_parameters,omitempty"`
GroupName string `json:"group_name,omitempty"`
RulePassed bool `json:"passed"`
IsControlGroup *bool `json:"is_control_group,omitempty"`
}
type LayerInitializeResponse struct {
BaseSpecInitializeResponse
Value map[string]interface{} `json:"value"`
Group string `json:"group"`
IsDeviceBased bool `json:"is_device_based"`
IsExperimentActive *bool `json:"is_experiment_active,omitempty"`
IsUserInExperiment *bool `json:"is_user_in_experiment,omitempty"`
ExplicitParameters *[]string `json:"explicit_parameters,omitempty"`
AllocatedExperimentName string `json:"allocated_experiment_name,omitempty"`
UndelegatedSecondaryExposures []SecondaryExposure `json:"undelegated_secondary_exposures"`
GroupName string `json:"group_name,omitempty"`
}
func mergeMaps(a map[string]interface{}, b map[string]interface{}) {
for k, v := range b {
a[k] = v
}
}
func getConfigType(spec configSpec) ConfigType {
switch spec.Entity {
case "feature_gate":
return FeatureGateType
case "holdout":
return HoldoutType
case "segment":
return SegmentType
case "dynamic_config":
return DynamicConfigType
case "experiment":
return ExperimentType
case "autotune":
return AutotuneType
case "layer":
return LayerType
default:
return UnknownType
}
}
func getClientInitializeResponse(
user User,
e *evaluator,
context *evalContext,
options *GCIROptions,
) ClientInitializeResponse {
hashAlgorithm := context.Hash
if hashAlgorithm != "none" && hashAlgorithm != "djb2" {
hashAlgorithm = "sha256"
}
var appId string
if context.TargetAppID != "" {
appId = context.TargetAppID
} else {
appId, _ = e.store.getAppIDForSDKKey(context.ClientKey)
context.TargetAppID = appId
}
evalResultToBaseResponse := func(name string, eval *evalResult) (string, BaseSpecInitializeResponse) {
hashedName := hashName(hashAlgorithm, name)
result := BaseSpecInitializeResponse{
Name: hashedName,
RuleID: eval.RuleID,
IDType: eval.IDType,
SecondaryExposures: eval.SecondaryExposures,
}
return hashedName, result
}
getExperimentControlRule := func(name string, spec configSpec) *configRule {
for _, rule := range spec.Rules {
if rule.IsControlGroup != nil && *rule.IsControlGroup {
return &rule
}
}
return nil
}
gateToResponse := func(gateName string, spec configSpec) (string, GateInitializeResponse) {
evalRes := &evalResult{}
if context.IncludeLocalOverrides || options.Overrides != nil {
if gateOverride, hasOverride := e.getGateOverrideEvalForGCIR(gateName, options); hasOverride {
evalRes = gateOverride
} else {
evalRes = e.eval(user, spec, 0, context)
}
} else {
evalRes = e.eval(user, spec, 0, context)
}
hashedName, base := evalResultToBaseResponse(gateName, evalRes)
result := GateInitializeResponse{
BaseSpecInitializeResponse: base,
Value: evalRes.Value,
}
if context.IncludeConfigType {
result.ConfigType = getConfigType(spec)
}
return hashedName, result
}
cmabToResponse := func(cmabName string, spec configSpec) (string, ConfigInitializeResponse) {
evalRes := &evalResult{}
if context.IncludeLocalOverrides || options.Overrides != nil {
if configOverride, hasOverride := e.getConfigOverrideEvalForGCIR(spec, options); hasOverride {
evalRes = configOverride
} else {
evalRes = e.evalCMABImpl(user, cmabName, 0, context)
}
} else {
evalRes = e.evalCMABImpl(user, cmabName, 0, context)
}
hashedName, base := evalResultToBaseResponse(cmabName, evalRes)
result := ConfigInitializeResponse{
BaseSpecInitializeResponse: base,
Value: evalRes.JsonValue,
}
return hashedName, result
}
configToResponse := func(configName string, spec configSpec) (string, ConfigInitializeResponse) {
evalRes := &evalResult{}
hasExpOverride := false
if context.IncludeLocalOverrides || options.Overrides != nil {
if configOverride, hasOverride := e.getConfigOverrideEvalForGCIR(spec, options); hasOverride {
hasExpOverride = true
evalRes = configOverride
} else {
evalRes = e.eval(user, spec, 0, context)
}
} else {
evalRes = e.eval(user, spec, 0, context)
}
hashedName, base := evalResultToBaseResponse(configName, evalRes)
result := ConfigInitializeResponse{
BaseSpecInitializeResponse: base,
Value: evalRes.JsonValue,
Group: evalRes.RuleID,
IsDeviceBased: strings.EqualFold(spec.IDType, "stableid"),
RulePassed: evalRes.Value,
}
if context.IncludeConfigType {
result.ConfigType = getConfigType(spec)
}
if evalRes.GroupName != "" {
result.GroupName = evalRes.GroupName
}
if strings.EqualFold(spec.Entity, "experiment") {
result.IsUserInExperiment = new(bool)
*result.IsUserInExperiment = evalRes.IsExperimentGroup != nil && *evalRes.IsExperimentGroup
result.IsExperimentActive = new(bool)
*result.IsExperimentActive = spec.IsActive != nil && *spec.IsActive
if context.UseControlForUsersNotInExperiment {
controlRule := getExperimentControlRule(configName, spec)
if controlRule != nil && !*result.IsUserInExperiment && !hasExpOverride {
result.Value = controlRule.ReturnValueJSON
result.GroupName = controlRule.GroupName
}
if controlRule != nil && strings.EqualFold(controlRule.GroupName, result.GroupName) {
result.IsControlGroup = new(bool)
*result.IsControlGroup = true
}
}
if spec.HasSharedParams != nil && *spec.HasSharedParams {
result.IsInLayer = new(bool)
*result.IsInLayer = true
result.ExplicitParameters = new([]string)
*result.ExplicitParameters = spec.ExplicitParameters
layerName, _ := e.store.getExperimentLayer(spec.Name)
layer, exists := e.store.getLayerConfig(layerName)
defaultValue := make(map[string]interface{})
if exists {
mergeMaps(defaultValue, layer.DefaultValueJSON)
mergeMaps(defaultValue, result.Value)
result.Value = defaultValue
}
}
}
return hashedName, result
}
layerToResponse := func(layerName string, spec configSpec) (string, LayerInitializeResponse) {
evalResult := &evalResult{}
if context.IncludeLocalOverrides || options.Overrides != nil {
if layerOverride, hasOverride := e.getLayerOverrideEvalForGCIR(spec, options); hasOverride {
evalResult = layerOverride
} else {
evalResult = e.eval(user, spec, 0, context)
}
} else {
evalResult = e.eval(user, spec, 0, context)
}
hashedName, base := evalResultToBaseResponse(layerName, evalResult)
result := LayerInitializeResponse{
BaseSpecInitializeResponse: base,
Value: evalResult.JsonValue,
Group: evalResult.RuleID,
IsDeviceBased: strings.EqualFold(spec.IDType, "stableid"),
UndelegatedSecondaryExposures: evalResult.UndelegatedSecondaryExposures,
}
if context.IncludeConfigType {
result.ConfigType = getConfigType(spec)
}
delegate := evalResult.ConfigDelegate
result.ExplicitParameters = new([]string)
if len(spec.ExplicitParameters) > 0 {
// spec.ExplicitParameters may be "null" due to how
// JSON Unmarshal works with fields with unallocated memory
*result.ExplicitParameters = spec.ExplicitParameters
} else {
*result.ExplicitParameters = make([]string, 0)
}
if delegate != "" {
delegateSpec, exists := e.store.getDynamicConfig(delegate)
delegateResult := e.eval(user, delegateSpec, 0, context)
if exists {
result.AllocatedExperimentName = hashName(hashAlgorithm, delegate)
result.IsUserInExperiment = new(bool)
*result.IsUserInExperiment = delegateResult.IsExperimentGroup != nil && *delegateResult.IsExperimentGroup
result.IsExperimentActive = new(bool)
*result.IsExperimentActive = delegateSpec.IsActive != nil && *delegateSpec.IsActive
if len(delegateSpec.ExplicitParameters) > 0 {
*result.ExplicitParameters = delegateSpec.ExplicitParameters
}
if delegateResult.GroupName != "" {
result.GroupName = delegateResult.GroupName
}
}
}
return hashedName, result
}
filterByEntities := false
gatesLookup := make(map[string]bool)
configsLookup := make(map[string]bool)
if entities, ok := e.store.getEntitiesForSDKKey(context.ClientKey); ok {
filterByEntities = true
for _, gate := range entities.Gates {
gatesLookup[gate] = true
}
for _, config := range entities.Configs {
configsLookup[config] = true
}
}
configTypesMap := make(map[ConfigType]struct{})
if context.ConfigTypesToInclude != nil {
for _, configType := range context.ConfigTypesToInclude {
configTypesMap[configType] = struct{}{}
}
}
shouldFilterConfigType := func(specType ConfigType) bool {
if context.ConfigTypesToInclude == nil {
return false
}
if _, exists := configTypesMap[specType]; exists {
return false
}
return true
}
featureGates := make(map[string]GateInitializeResponse)
dynamicConfigs := make(map[string]ConfigInitializeResponse)
layerConfigs := make(map[string]LayerInitializeResponse)
for name, spec := range e.store.featureGates {
if !spec.hasTargetAppID(appId) {
continue
}
if shouldFilterConfigType(getConfigType(spec)) {
continue
}
if filterByEntities {
if _, ok := gatesLookup[name]; !ok {
continue
}
}
if !strings.EqualFold(spec.Entity, SegmentType) && !strings.EqualFold(spec.Entity, HoldoutType) {
hashedName, res := gateToResponse(name, spec)
featureGates[hashedName] = res
}
}
for name, spec := range e.store.dynamicConfigs {
if !spec.hasTargetAppID(appId) {
continue
}
if shouldFilterConfigType(getConfigType(spec)) {
continue
}
if filterByEntities {
if _, ok := configsLookup[name]; !ok {
continue
}
}
hashedName, res := configToResponse(name, spec)
dynamicConfigs[hashedName] = res
}
for name, spec := range e.store.layerConfigs {
if !spec.hasTargetAppID(appId) {
continue
}
if shouldFilterConfigType(getConfigType(spec)) {
continue
}
hashedName, res := layerToResponse(name, spec)
layerConfigs[hashedName] = res
}
for name, spec := range e.store.cmabConfigs {
if !spec.hasTargetAppID(appId) {
continue
}
if shouldFilterConfigType(getConfigType(spec)) {
continue
}
if filterByEntities {
if _, ok := configsLookup[name]; !ok {
continue
}
}
hashedName, res := cmabToResponse(name, spec)
dynamicConfigs[hashedName] = res
}
meta := getStatsigMetadata()
response := ClientInitializeResponse{
FeatureGates: featureGates,
DynamicConfigs: dynamicConfigs,
LayerConfigs: layerConfigs,
SdkParams: make(map[string]string),
HasUpdates: true,
Generator: "statsig-go-sdk",
EvaluatedKeys: map[string]interface{}{"userID": user.UserID, "customIDs": user.CustomIDs},
Time: e.store.lastSyncTime,
SDKInfo: SDKInfo{SDKVersion: meta.SDKVersion, SDKType: meta.SDKType},
User: *user.getCopyForLogging(),
HashUsed: hashAlgorithm,
}
sessionReplayInfo := e.store.getSessionReplayInfo()
if sessionReplayInfo == nil {
return response
}
response.RecordingBlocked = sessionReplayInfo.RecordingBlocked
canRecord := !*sessionReplayInfo.RecordingBlocked
response.CanRecordSession = &canRecord
if sessionReplayInfo.SamplingRate != nil {
response.SessionRecordingRate = sessionReplayInfo.SamplingRate
if rand.Float64() > *sessionReplayInfo.SamplingRate {
canRecord = false
response.CanRecordSession = &canRecord
}
}
if sessionReplayInfo.TargetingGate != nil {
res := response.FeatureGates[hashName(hashAlgorithm, *sessionReplayInfo.TargetingGate)]
passesTargeting := res.Value
if !passesTargeting {
canRecord = false
response.CanRecordSession = &canRecord
}
response.PassesSessionRecordingTargeting = &passesTargeting
}
if sessionReplayInfo.SessionRecordingEventTriggers != nil {
resultEventTriggers := make(map[string]GCIRSessionReplayTrigger)
for eventName, trigger := range *sessionReplayInfo.SessionRecordingEventTriggers {
resultEventTrigger := GCIRSessionReplayTrigger{}
if trigger.Values != nil {
resultEventTrigger.Values = trigger.Values
}
if trigger.SamplingRate != nil {
passesSampling := rand.Float64() <= *trigger.SamplingRate
resultEventTrigger.PassesSampling = &passesSampling
}
resultEventTriggers[eventName] = resultEventTrigger
}
response.SessionRecordingEventTriggers = &resultEventTriggers
}
if sessionReplayInfo.SessionRecordingExposureTriggers != nil {
resultExposureTriggers := make(map[string]GCIRSessionReplayTrigger)
for exposureName, trigger := range *sessionReplayInfo.SessionRecordingExposureTriggers {
resultExposureTrigger := GCIRSessionReplayTrigger{}
if trigger.Values != nil {
resultExposureTrigger.Values = trigger.Values
}
if trigger.SamplingRate != nil {
passesSampling := rand.Float64() <= *trigger.SamplingRate
resultExposureTrigger.PassesSampling = &passesSampling
}
resultExposureTriggers[hashName(hashAlgorithm, exposureName)] = resultExposureTrigger
}
response.SessionRecordingExposureTriggers = &resultExposureTriggers
}
return response
}