-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathstatsig.go
265 lines (230 loc) · 8.92 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
// Package statsig implements feature gating and a/b testing
package statsig
import (
"fmt"
"net/http"
"time"
)
type InitializeDetails struct {
Duration time.Duration
Success bool
Error error
Source EvaluationSource
}
var instance *Client
// 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
func Initialize(sdkKey string) InitializeDetails {
return InitializeWithOptions(sdkKey, &Options{})
}
// Initializes the global Statsig instance with the given sdkKey and options
func InitializeWithOptions(sdkKey string, options *Options) InitializeDetails {
InitializeGlobalOutputLogger(options.OutputLoggerOptions)
InitializeGlobalSessionID()
if IsInitialized() {
Logger().Log("Statsig is already initialized.", nil)
return InitializeDetails{Success: true, Source: instance.evaluator.store.source}
}
var context *initContext
instance, context = newClientImpl(sdkKey, options)
return InitializeDetails{
Duration: time.Since(context.Start),
Success: context.Success,
Error: context.Error,
Source: context.Source,
}
}
// 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.GetLayerWithOptions(user, layer, options)
}
// 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
}