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(-> {
    @init_diagnostics = Statsig::Diagnostics.new("initialize")
    @init_diagnostics.mark("overall", "start")
    @options = options || StatsigOptions.new
    @shutdown = false
    @secret_key = secret_key
    @net = Statsig::Network.new(secret_key, @options)
    @logger = Statsig::StatsigLogger.new(@net, @options)
    @evaluator = Statsig::Evaluator.new(@net, @options, error_callback, @init_diagnostics)
    @init_diagnostics.mark("overall", "end")

    log_init_diagnostics
  })
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
# File 'lib/statsig_driver.rb', line 54

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

end

#get_client_initialize_response(user) ⇒ Hash

Parameters:

Returns:

  • (Hash)


199
200
201
202
203
204
205
# File 'lib/statsig_driver.rb', line 199

def get_client_initialize_response(user)
  @err_boundary.capture(-> {
    validate_user(user)
    normalize_user(user)
    @evaluator.get_client_initialize_response(user)
  }, -> { nil })
end

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



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

def get_config(user, dynamic_config_name, options = GetConfigOptions.new)
  @err_boundary.capture(-> {
    user = verify_inputs(user, dynamic_config_name, "dynamic_config_name")
    get_config_impl(user, dynamic_config_name, options)
  }, -> { DynamicConfig.new(dynamic_config_name) })
end

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



104
105
106
107
108
109
# File 'lib/statsig_driver.rb', line 104

def get_experiment(user, experiment_name, options = GetExperimentOptions.new)
  @err_boundary.capture(-> {
    user = verify_inputs(user, experiment_name, "experiment_name")
    get_config_impl(user, experiment_name, options)
  }, -> { DynamicConfig.new(experiment_name) })
end

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



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

def get_layer(user, layer_name, options = GetLayerOptions.new)
  @err_boundary.capture(-> {
    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)
  }, -> {
    Layer.new(layer_name)
  })
end

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



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/statsig_driver.rb', line 160

def log_event(user, event_name, value = nil,  = nil)
  @err_boundary.capture(-> {
    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)
  })
end

#manually_log_config_exposure(user, config_name) ⇒ Object



113
114
115
116
117
# File 'lib/statsig_driver.rb', line 113

def manually_log_config_exposure(user, config_name)
  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)
end

#manually_log_gate_exposure(user, gate_name) ⇒ Object



79
80
81
82
83
# File 'lib/statsig_driver.rb', line 79

def manually_log_gate_exposure(user, gate_name)
  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



153
154
155
156
157
158
# File 'lib/statsig_driver.rb', line 153

def manually_log_layer_parameter_exposure(user, layer_name, parameter_name)
  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)
end

#maybe_restart_background_threadsObject



207
208
209
210
211
212
213
214
215
216
# File 'lib/statsig_driver.rb', line 207

def maybe_restart_background_threads
  if @options.local_mode
    return
  end

  @err_boundary.capture(-> {
    @evaluator.maybe_restart_background_threads
    @logger.maybe_restart_background_threads
  })
end

#override_config(config_name, config_value) ⇒ Object



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

def override_config(config_name, config_value)
  @err_boundary.capture(-> {
    @evaluator.override_config(config_name, config_value)
  })
end

#override_gate(gate_name, gate_value) ⇒ Object



185
186
187
188
189
# File 'lib/statsig_driver.rb', line 185

def override_gate(gate_name, gate_value)
  @err_boundary.capture(-> {
    @evaluator.override_gate(gate_name, gate_value)
  })
end

#shutdownObject



177
178
179
180
181
182
183
# File 'lib/statsig_driver.rb', line 177

def shutdown
  @err_boundary.capture(-> {
    @shutdown = true
    @logger.shutdown
    @evaluator.shutdown
  })
end