forked from statsig-io/go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statsig.go
335 lines (291 loc) · 11.3 KB
/
statsig.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
// Package statsig implements feature gating and a/b testing
package statsig
import (
"fmt"
"net/http"
"time"
)
var instance *Client
// Initializes the global Statsig instance with the given sdkKey
func Initialize(sdkKey string) {
InitializeGlobalOutputLogger(OutputLoggerOptions{})
InitializeGlobalSessionID()
if IsInitialized() {
Logger().Log("Statsig is already initialized.", nil)
return
}
instance = NewClient(sdkKey)
}
// Advanced options for configuring the Statsig SDK
type Options struct {
API string `json:"api"`
APIOverrides APIOverrides `json:"api_overrides"`
Environment Environment `json:"environment"`
LocalMode bool `json:"localMode"`
ConfigSyncInterval time.Duration
IDListSyncInterval time.Duration
LoggingInterval time.Duration
LoggingMaxBufferSize int
BootstrapValues string
RulesUpdatedCallback func(rules string, time int64)
InitTimeout time.Duration
DataAdapter IDataAdapter
OutputLoggerOptions OutputLoggerOptions
StatsigLoggerOptions StatsigLoggerOptions
EvaluationCallbacks EvaluationCallbacks
DisableCDN bool // Disables use of CDN for downloading config specs
UserPersistentStorage IUserPersistentStorage
IPCountryOptions IPCountryOptions
UAParserOptions UAParserOptions
}
type APIOverrides struct {
DownloadConfigSpecs string `json:"download_config_specs"`
GetIDLists string `json:"get_id_lists"`
LogEvent string `json:"log_event"`
}
type EvaluationCallbacks struct {
GateEvaluationCallback func(name string, result bool, exposure *ExposureEvent)
ConfigEvaluationCallback func(name string, result DynamicConfig, exposure *ExposureEvent)
ExperimentEvaluationCallback func(name string, result DynamicConfig, exposure *ExposureEvent)
LayerEvaluationCallback func(name string, param string, result DynamicConfig, exposure *ExposureEvent)
ExposureCallback func(name string, exposure *ExposureEvent)
IncludeDisabledExposures bool
}
type OutputLoggerOptions struct {
LogCallback func(message string, err error)
EnableDebug bool
DisableInitDiagnostics bool
DisableSyncDiagnostics bool
}
type StatsigLoggerOptions struct {
DisableInitDiagnostics bool
DisableSyncDiagnostics bool
DisableApiDiagnostics bool
DisableAllLogging bool
}
type IPCountryOptions struct {
Disabled bool // Fully disable IP to country lookup
LazyLoad bool // Load in background
EnsureLoaded bool // Wait until loaded when needed
}
type UAParserOptions struct {
Disabled bool // Fully disable UA parser
LazyLoad bool // Load in background
EnsureLoaded bool // Wait until loaded when needed
}
// See https://docs.statsig.com/guides/usingEnvironments
type Environment struct {
Tier string `json:"tier"`
Params map[string]string `json:"params"`
}
// options for getClientInitializeResponse
type GCIROptions struct {
IncludeLocalOverrides bool
ClientKey string
HashAlgorithm string
}
// IsInitialized returns whether the global Statsig instance has already been initialized or not
func IsInitialized() bool {
return instance != nil
}
// Initializes the global Statsig instance with the given sdkKey and options
func InitializeWithOptions(sdkKey string, options *Options) {
InitializeGlobalOutputLogger(options.OutputLoggerOptions)
InitializeGlobalSessionID()
if IsInitialized() {
Logger().Log("Statsig is already initialized.", nil)
return
}
instance = NewClientWithOptions(sdkKey, options)
}
// Checks the value of a Feature Gate for the given user
func CheckGate(user User, gate string) bool {
if !IsInitialized() {
panic(fmt.Errorf("must Initialize() statsig before calling CheckGate"))
}
return instance.CheckGate(user, gate)
}
// Checks the value of a Feature Gate for the given user without logging an exposure event
func CheckGateWithExposureLoggingDisabled(user User, gate string) bool {
if !IsInitialized() {
panic(fmt.Errorf("must Initialize() statsig before calling CheckGateWithExposureLoggingDisabled"))
}
return instance.CheckGateWithExposureLoggingDisabled(user, gate)
}
// Get the Feature Gate for the given user
func GetGate(user User, gate string) FeatureGate {
if !IsInitialized() {
panic(fmt.Errorf("must Initialize() statsig before calling GetGate"))
}
return instance.GetGate(user, gate)
}
// Get the Feature Gate for the given user without logging an exposure event
func GetGateWithExposureLoggingDisabled(user User, gate string) FeatureGate {
if !IsInitialized() {
panic(fmt.Errorf("must Initialize() statsig before calling GetGateWithExposureLoggingDisabled"))
}
return instance.GetGateWithExposureLoggingDisabled(user, gate)
}
// Logs an exposure event for the gate
func ManuallyLogGateExposure(user User, config string) {
if !IsInitialized() {
panic(fmt.Errorf("must Initialize() statsig before calling ManuallyLogGateExposure"))
}
instance.ManuallyLogGateExposure(user, config)
}
// Gets the DynamicConfig value for the given user
func GetConfig(user User, config string) DynamicConfig {
if !IsInitialized() {
panic(fmt.Errorf("must Initialize() statsig before calling GetConfig"))
}
return instance.GetConfig(user, config)
}
// Gets the DynamicConfig value for the given user without logging an exposure event
func GetConfigWithExposureLoggingDisabled(user User, config string) DynamicConfig {
if !IsInitialized() {
panic(fmt.Errorf("must Initialize() statsig before calling GetConfigWithExposureLoggingDisabled"))
}
return instance.GetConfigWithExposureLoggingDisabled(user, config)
}
// Logs an exposure event for the dynamic config
func ManuallyLogConfigExposure(user User, config string) {
if !IsInitialized() {
panic(fmt.Errorf("must Initialize() statsig before calling ManuallyLogConfigExposure"))
}
instance.ManuallyLogConfigExposure(user, config)
}
// Override the value of a Feature Gate for the given user
func OverrideGate(gate string, val bool) {
if !IsInitialized() {
panic(fmt.Errorf("must Initialize() statsig before calling OverrideGate"))
}
instance.OverrideGate(gate, val)
}
// Override the DynamicConfig value for the given user
func OverrideConfig(config string, val map[string]interface{}) {
if !IsInitialized() {
panic(fmt.Errorf("must Initialize() statsig before calling OverrideConfig"))
}
instance.OverrideConfig(config, val)
}
// Override the Layer value for the given user
func OverrideLayer(layer string, val map[string]interface{}) {
if !IsInitialized() {
panic(fmt.Errorf("must Initialize() statsig before calling OverrideLayer"))
}
instance.OverrideLayer(layer, val)
}
// Gets the name of layer an Experiment
func GetExperimentLayer(experiment string) (string, bool) {
if !IsInitialized() {
panic(fmt.Errorf("must Initialize() statsig before calling GetExperimentLayer"))
}
return instance.GetExperimentLayer(experiment)
}
// Gets the DynamicConfig value of an Experiment for the given user
func GetExperiment(user User, experiment string) DynamicConfig {
if !IsInitialized() {
panic(fmt.Errorf("must Initialize() statsig before calling GetExperiment"))
}
return instance.GetExperiment(user, experiment)
}
// Gets the DynamicConfig value of an Experiment for the given user without logging an exposure event
func GetExperimentWithExposureLoggingDisabled(user User, experiment string) DynamicConfig {
if !IsInitialized() {
panic(fmt.Errorf("must Initialize() statsig before calling GetExperimentWithExposureLoggingDisabled"))
}
return instance.GetExperimentWithExposureLoggingDisabled(user, experiment)
}
// Gets the DynamicConfig value of an Experiment for the given user with configurable options
func GetExperimentWithOptions(user User, experiment string, options *GetExperimentOptions) DynamicConfig {
if !IsInitialized() {
panic(fmt.Errorf("must Initialize() statsig before calling GetExperimentWithOptions"))
}
return instance.GetExperimentWithOptions(user, experiment, options)
}
// Logs an exposure event for the experiment
func ManuallyLogExperimentExposure(user User, experiment string) {
if !IsInitialized() {
panic(fmt.Errorf("must Initialize() statsig before calling ManuallyLogExperimentExposure"))
}
instance.ManuallyLogExperimentExposure(user, experiment)
}
func GetUserPersistedValues(user User, idType string) UserPersistedValues {
if !IsInitialized() {
panic(fmt.Errorf("must Initialize() statsig before calling GetUserPersistedValues"))
}
return instance.GetUserPersistedValues(user, idType)
}
// Gets the Layer object for the given user
func GetLayer(user User, layer string) Layer {
if !IsInitialized() {
panic(fmt.Errorf("must Initialize() statsig before calling GetLayer"))
}
return instance.GetLayer(user, layer)
}
// Gets the Layer object for the given user without logging an exposure event
func GetLayerWithExposureLoggingDisabled(user User, layer string) Layer {
if !IsInitialized() {
panic(fmt.Errorf("must Initialize() statsig before calling GetLayerWithExposureLoggingDisabled"))
}
return instance.GetLayerWithExposureLoggingDisabled(user, layer)
}
// Gets the Layer object for the given user with configurable options
func GetLayerWithOptions(user User, layer string, options *GetLayerOptions) Layer {
if !IsInitialized() {
panic(fmt.Errorf("must Initialize() statsig before calling GetLayerWithOptions"))
}
return instance.getLayerImpl(user, layer, options, StatsigContext{Caller: "GetLayerWithOptions", ConfigName: layer})
}
// Logs an exposure event for the parameter in the given layer
func ManuallyLogLayerParameterExposure(user User, layer string, parameter string) {
if !IsInitialized() {
panic(fmt.Errorf("must Initialize() statsig before calling ManuallyLogLayerParameterExposure"))
}
instance.ManuallyLogLayerParameterExposure(user, layer, parameter)
}
// Logs an event to the Statsig console
func LogEvent(event Event) {
if !IsInitialized() {
panic(fmt.Errorf("must Initialize() statsig before calling LogEvent"))
}
instance.LogEvent(event)
}
// Logs a slice of events to Statsig server immediately
func LogImmediate(events []Event) (*http.Response, error) {
if !IsInitialized() {
panic(fmt.Errorf("must Initialize() statsig before calling LogImmediate"))
}
return instance.LogImmediate(events)
}
func GetClientInitializeResponse(user User) ClientInitializeResponse {
if !IsInitialized() {
panic(fmt.Errorf("must Initialize() statsig before calling GetClientInitializeResponse"))
}
return instance.GetClientInitializeResponse(user, "", false)
}
func GetClientInitializeResponseWithOptions(user User, options *GCIROptions) ClientInitializeResponse {
if !IsInitialized() {
panic(fmt.Errorf("must Initialize() statsig before calling GetClientInitializeResponseWithOptions"))
}
return instance.GetClientInitializeResponseWithOptions(user, options)
}
func GetClientInitializeResponseForTargetApp(user User, clientKey string) ClientInitializeResponse {
if !IsInitialized() {
panic(fmt.Errorf("must Initialize() statsig before calling GetClientInitializeResponseForTargetApp"))
}
return instance.GetClientInitializeResponse(user, clientKey, false)
}
// Cleans up Statsig, persisting any Event Logs and cleanup processes
// Using any method is undefined after Shutdown() has been called
func Shutdown() {
if !IsInitialized() {
return
}
instance.Shutdown()
}
// For test only so we can clear the shared instance. Not thread safe.
func ShutdownAndDangerouslyClearInstance() {
Shutdown()
instance = nil
}