Class: StatsigDriver

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/statsig_driver.rb

Instance Method Summary collapse

Constructor Details

#initialize(secret_key, options = nil, error_callback = nil) ⇒ StatsigDriver

Returns a new instance of StatsigDriver.



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
# File 'lib/statsig_driver.rb', line 23

def initialize(secret_key, options = nil, error_callback = nil)
  unless secret_key.start_with?('secret-')
    raise Statsig::ValueError.new('Invalid secret key provided. Provide your project secret key from the Statsig console')
  end

  if !options.nil? && !options.instance_of?(StatsigOptions)
    raise Statsig::ValueError.new('Invalid options provided. Either provide a valid StatsigOptions object or nil')
  end

  @err_boundary = Statsig::ErrorBoundary.new(secret_key)
  @err_boundary.capture(task: lambda {
    @diagnostics = Statsig::Diagnostics.new()
    tracker = @diagnostics.track('initialize', 'overall')
    @options = options || StatsigOptions.new
    @shutdown = false
    @secret_key = secret_key
    @net = Statsig::Network.new(secret_key, @options)
    @logger = Statsig::StatsigLogger.new(@net, @options, @err_boundary)
    @persistent_storage_utils = Statsig::UserPersistentStorageUtils.new(@options)
    @store = Statsig::SpecStore.new(@net, @options, error_callback, @diagnostics, @err_boundary, @logger, secret_key)
    @evaluator = Statsig::Evaluator.new(@store, @options, @persistent_storage_utils)
    tracker.end(success: true)

    @logger.log_diagnostics_event(@diagnostics, 'initialize')
  }, caller: __method__.to_s)
end

Instance Method Details

#check_gate(user, gate_name, options = Statsig::CheckGateOptions.new) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/statsig_driver.rb', line 89

def check_gate(user, gate_name, options = Statsig::CheckGateOptions.new)
  @err_boundary.capture(task: lambda {
    run_with_diagnostics(task: lambda {
      get_gate_impl(user, gate_name, disable_log_exposure: options.disable_log_exposure).value
    }, caller: __method__.to_s)
  }, recover: -> { false }, caller: __method__.to_s)
end

#get_client_initialize_response(user, hash, client_sdk_key) ⇒ Hash

Parameters:

Returns:

  • (Hash)


261
262
263
264
265
266
267
# File 'lib/statsig_driver.rb', line 261

def get_client_initialize_response(user, hash, client_sdk_key)
  @err_boundary.capture(task: lambda {
    validate_user(user)
    normalize_user(user)
    @evaluator.get_client_initialize_response(user, hash, client_sdk_key)
  }, recover: -> { nil }, caller: __method__.to_s)
end

#get_config(user, dynamic_config_name, options = Statsig::GetConfigOptions.new) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/statsig_driver.rb', line 107

def get_config(user, dynamic_config_name, options = Statsig::GetConfigOptions.new)
  @err_boundary.capture(task: lambda {
    run_with_diagnostics(task: lambda {
      user = verify_inputs(user, dynamic_config_name, "dynamic_config_name")
      get_config_impl(user, dynamic_config_name, options.disable_log_exposure)
    }, caller: __method__.to_s)
  }, recover: -> { DynamicConfig.new(dynamic_config_name) }, caller: __method__.to_s)
end

#get_experiment(user, experiment_name, options = Statsig::GetExperimentOptions.new) ⇒ Object



117
118
119
120
121
122
123
124
# File 'lib/statsig_driver.rb', line 117

def get_experiment(user, experiment_name, options = Statsig::GetExperimentOptions.new)
  @err_boundary.capture(task: lambda {
    run_with_diagnostics(task: lambda {
      user = verify_inputs(user, experiment_name, "experiment_name")
      get_config_impl(user, experiment_name, options.disable_log_exposure, user_persisted_values: options.user_persisted_values)
    }, caller: __method__.to_s)
  }, recover: -> { DynamicConfig.new(experiment_name) }, caller: __method__.to_s)
end

#get_gate(user, gate_name, options = Statsig::GetGateOptions.new) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/statsig_driver.rb', line 80

def get_gate(user, gate_name, options = Statsig::GetGateOptions.new)
  @err_boundary.capture(task: lambda {
    run_with_diagnostics(task: lambda {
      get_gate_impl(user, gate_name, disable_log_exposure: options.disable_log_exposure, skip_evaluation: options.skip_evaluation)
    }, caller: __method__.to_s)
  }, recover: -> { false }, caller: __method__.to_s)
end

#get_gate_impl(user, gate_name, disable_log_exposure: false, skip_evaluation: false) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/statsig_driver.rb', line 58

def get_gate_impl(user, gate_name, disable_log_exposure: false, skip_evaluation: false)
  if skip_evaluation
    gate = @store.get_gate(gate_name)
    return FeatureGate.new(gate_name) if gate.nil?
    return FeatureGate.new(gate['name'], target_app_ids: gate['targetAppIDs'])
  end
  user = verify_inputs(user, gate_name, 'gate_name')

  res = @evaluator.check_gate(user, gate_name)
  if res.nil?
    res = Statsig::ConfigResult.new(gate_name)
  end

  unless disable_log_exposure
    @logger.log_gate_exposure(
      user, res.name, res.gate_value, res.rule_id, res.secondary_exposures, res.evaluation_details
    )
  end
  FeatureGate.from_config_result(res)
end

#get_layer(user, layer_name, options = Statsig::GetLayerOptions.new) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/statsig_driver.rb', line 146

def get_layer(user, layer_name, options = Statsig::GetLayerOptions.new)
  @err_boundary.capture(task: lambda {
    run_with_diagnostics(task: lambda {
      user = verify_inputs(user, layer_name, "layer_name")

      res = @evaluator.get_layer(user, layer_name)
      if res.nil?
        res = Statsig::ConfigResult.new(layer_name)
      end

      exposure_log_func = !options.disable_log_exposure ? lambda { |layer, parameter_name|
        @logger.log_layer_exposure(user, layer, parameter_name, res)
      } : nil
      Layer.new(res.name, res.json_value, res.rule_id, res.group_name, res.config_delegate, exposure_log_func)
    }, caller: __method__.to_s)
  }, recover: lambda { Layer.new(layer_name) }, caller: __method__.to_s)
end

#get_user_persisted_values(user, id_type) ⇒ Object



136
137
138
139
140
141
142
143
# File 'lib/statsig_driver.rb', line 136

def get_user_persisted_values(user, id_type)
  @err_boundary.capture(task: lambda {
    persisted_values = @persistent_storage_utils.get_user_persisted_values(user, id_type)
    return {} if persisted_values.nil?

    persisted_values
  }, caller: __method__.to_s)
end

#list_autotunesObject



225
226
227
228
229
# File 'lib/statsig_driver.rb', line 225

def list_autotunes
  @err_boundary.capture(task: lambda {
    @evaluator.list_autotunes
  }, caller: __method__.to_s)
end

#list_configsObject



211
212
213
214
215
# File 'lib/statsig_driver.rb', line 211

def list_configs
  @err_boundary.capture(task: lambda {
    @evaluator.list_configs
  }, caller: __method__.to_s)
end

#list_experimentsObject



218
219
220
221
222
# File 'lib/statsig_driver.rb', line 218

def list_experiments
  @err_boundary.capture(task: lambda {
    @evaluator.list_experiments
  }, caller: __method__.to_s)
end

#list_gatesObject



204
205
206
207
208
# File 'lib/statsig_driver.rb', line 204

def list_gates
  @err_boundary.capture(task: lambda {
    @evaluator.list_gates
  }, caller: __method__.to_s)
end

#list_layersObject



232
233
234
235
236
# File 'lib/statsig_driver.rb', line 232

def list_layers
  @err_boundary.capture(task: lambda {
    @evaluator.list_layers
  }, caller: __method__.to_s)
end

#log_event(user, event_name, value = nil, metadata = nil) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/statsig_driver.rb', line 174

def log_event(user, event_name, value = nil,  = nil)
  @err_boundary.capture(task: lambda {
    if !user.nil? && !user.instance_of?(StatsigUser)
      raise Statsig::ValueError.new('Must provide a valid StatsigUser or nil')
    end
    check_shutdown

    user = normalize_user(user)

    event = StatsigEvent.new(event_name)
    event.user = user
    event.value = value
    event. = 
    @logger.log_event(event)
  }, caller: __method__.to_s)
end

#manually_log_config_exposure(user, config_name) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/statsig_driver.rb', line 127

def manually_log_config_exposure(user, config_name)
  @err_boundary.capture(task: lambda {
    res = @evaluator.get_config(user, config_name)
    context = { 'is_manual_exposure' => true }
    @logger.log_config_exposure(user, res.name, res.rule_id, res.secondary_exposures, res.evaluation_details, context)
  }, caller: __method__.to_s)
end

#manually_log_gate_exposure(user, gate_name) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/statsig_driver.rb', line 98

def manually_log_gate_exposure(user, gate_name)
  @err_boundary.capture(task: lambda {
    res = @evaluator.check_gate(user, gate_name)
    context = { 'is_manual_exposure' => true }
    @logger.log_gate_exposure(user, gate_name, res.gate_value, res.rule_id, res.secondary_exposures, res.evaluation_details, context)
  })
end

#manually_log_layer_parameter_exposure(user, layer_name, parameter_name) ⇒ Object



165
166
167
168
169
170
171
172
# File 'lib/statsig_driver.rb', line 165

def manually_log_layer_parameter_exposure(user, layer_name, parameter_name)
  @err_boundary.capture(task: lambda {
    res = @evaluator.get_layer(user, layer_name)
    layer = Layer.new(layer_name, res.json_value, res.rule_id, res.group_name, res.config_delegate)
    context = { 'is_manual_exposure' => true }
    @logger.log_layer_exposure(user, layer, parameter_name, res, context)
  }, caller: __method__.to_s)
end

#manually_sync_idlistsObject



197
198
199
200
201
# File 'lib/statsig_driver.rb', line 197

def manually_sync_idlists
  @err_boundary.capture(task: lambda {
    @evaluator.spec_store.sync_id_lists
  }, caller: __method__.to_s)
end

#manually_sync_rulesetsObject



191
192
193
194
195
# File 'lib/statsig_driver.rb', line 191

def manually_sync_rulesets
  @err_boundary.capture(task: lambda {
    @evaluator.spec_store.sync_config_specs
  }, caller: __method__.to_s)
end

#maybe_restart_background_threadsObject



269
270
271
272
273
274
275
276
277
278
# File 'lib/statsig_driver.rb', line 269

def maybe_restart_background_threads
  if @options.local_mode
    return
  end

  @err_boundary.capture(task: lambda {
    @evaluator.maybe_restart_background_threads
    @logger.maybe_restart_background_threads
  }, caller: __method__.to_s)
end

#override_config(config_name, config_value) ⇒ Object



252
253
254
255
256
# File 'lib/statsig_driver.rb', line 252

def override_config(config_name, config_value)
  @err_boundary.capture(task: lambda {
    @evaluator.override_config(config_name, config_value)
  }, caller: __method__.to_s)
end

#override_gate(gate_name, gate_value) ⇒ Object



246
247
248
249
250
# File 'lib/statsig_driver.rb', line 246

def override_gate(gate_name, gate_value)
  @err_boundary.capture(task: lambda {
    @evaluator.override_gate(gate_name, gate_value)
  }, caller: __method__.to_s)
end

#shutdownObject



238
239
240
241
242
243
244
# File 'lib/statsig_driver.rb', line 238

def shutdown
  @err_boundary.capture(task: lambda {
    @shutdown = true
    @logger.shutdown
    @evaluator.shutdown
  }, caller: __method__.to_s)
end