Class: StatsigDriver

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

Defined Under Namespace

Classes: CheckGateOptions, GetConfigOptions, GetExperimentOptions, GetLayerOptions

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
# 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('initialize')
    tracker = @diagnostics.track('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)
    @evaluator = Statsig::Evaluator.new(@net, @options, error_callback, @diagnostics, @err_boundary, @logger)
    tracker.end(success: true)

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

Instance Method Details

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



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

def check_gate(user, gate_name, options = CheckGateOptions.new)
  @err_boundary.capture(task: lambda {
    run_with_diagnostics(task: lambda {
      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

      if res == $fetch_from_server
        res = check_gate_fallback(user, gate_name)
        # exposure logged by the server
      else
        if options.log_exposure
          @logger.log_gate_exposure(user, res.name, res.gate_value, res.rule_id, res.secondary_exposures, res.evaluation_details)
        end
      end

      res.gate_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)


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

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 = GetConfigOptions.new) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/statsig_driver.rb', line 94

def get_config(user, dynamic_config_name, options = 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)
    }, caller: __method__.to_s)
  }, recover: -> { DynamicConfig.new(dynamic_config_name) }, caller: __method__.to_s)
end

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



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

def get_experiment(user, experiment_name, options = 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)
    }, caller: __method__.to_s)
  }, recover: -> { DynamicConfig.new(experiment_name) }, caller: __method__.to_s)
end

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



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

def get_layer(user, layer_name, options = 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

      if res == $fetch_from_server
        if res.config_delegate.empty?
          return Layer.new(layer_name)
        end
        res = get_config_fallback(user, res.config_delegate)
        # exposure logged by the server
      end

      exposure_log_func = options.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, exposure_log_func)
    }, caller: __method__.to_s)
  }, recover: lambda { Layer.new(layer_name) }, caller: __method__.to_s)
end

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



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/statsig_driver.rb', line 171

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



120
121
122
123
124
125
126
# File 'lib/statsig_driver.rb', line 120

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



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

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



162
163
164
165
166
167
168
169
# File 'lib/statsig_driver.rb', line 162

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)
    context = { 'is_manual_exposure' => true }
    @logger.log_layer_exposure(user, layer, parameter_name, res, context)
  }, caller: __method__.to_s)
end

#maybe_restart_background_threadsObject



219
220
221
222
223
224
225
226
227
228
# File 'lib/statsig_driver.rb', line 219

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



202
203
204
205
206
# File 'lib/statsig_driver.rb', line 202

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



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

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



188
189
190
191
192
193
194
# File 'lib/statsig_driver.rb', line 188

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