Class: Statsig::StatsigLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/statsig_logger.rb

Instance Method Summary collapse

Constructor Details

#initialize(network, options, error_boundary, sdk_configs) ⇒ StatsigLogger

Returns a new instance of StatsigLogger.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/statsig_logger.rb', line 21

def initialize(network, options, error_boundary, sdk_configs)
  @network = network
  @events = []
  @options = options

  @logging_pool = Concurrent::ThreadPoolExecutor.new(
    name: 'statsig-logger',
    min_threads: @options.logger_threadpool_size,
    max_threads: @options.logger_threadpool_size,
    # max jobs pending before we start dropping
    max_queue: 100,
    fallback_policy: :discard
  )

  @error_boundary = error_boundary
  @background_flush = periodic_flush
  @deduper = Concurrent::Set.new
  @sampling_key_set = Statsig::TTLSet.new
  @interval = 0
  @flush_mutex = Mutex.new
  @debug_info = nil
  @sdk_configs = sdk_configs
end

Instance Method Details

#flushObject



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/statsig_logger.rb', line 195

def flush
  @flush_mutex.synchronize do
    if @events.length.zero?
      return
    end

    events_clone = @events
    @events = []
    serialized_events = events_clone.map { |e| e.serialize }

    serialized_events.each_slice(@options.logging_max_buffer_size) do |batch|
      @network.post_logs(batch, @error_boundary)
    end
  end
end

#flush_asyncObject



189
190
191
192
193
# File 'lib/statsig_logger.rb', line 189

def flush_async
  @logging_pool.post do
    flush
  end
end

#log_config_exposure(user, result, context = nil) ⇒ Object



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

def log_config_exposure(user, result, context = nil)
  should_log, logged_sampling_rate, shadow_logged = determine_sampling(EntityType::CONFIG, result.name, result, user)
  return unless should_log
  event = StatsigEvent.new($config_exposure_event)
  event.user = user
   = {
    config: result.name,
    ruleID: result.rule_id || Statsig::Const::EMPTY_STR,
    rulePassed: result.gate_value.to_s,
  }
  if result.config_version != nil
    [:configVersion] = result.config_version.to_s
  end
  if @debug_info != nil
    [:debugInfo] = @debug_info
  end
  unless result.override_config_name.nil?
    [:overrideConfigName] = result.override_config_name
  end
  return false if not is_unique_exposure(user, $config_exposure_event, )
  event. = 
  event.secondary_exposures = result.secondary_exposures.is_a?(Array) ? result.secondary_exposures : []
  event. = {}

  safe_add_eval_details(result.evaluation_details, event)
  safe_add_exposure_context(context, event)
  (event, logged_sampling_rate, shadow_logged)
  log_event(event)
end

#log_diagnostics_event(diagnostics, context, user = nil) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/statsig_logger.rb', line 151

def log_diagnostics_event(diagnostics, context, user = nil)
  return if diagnostics.nil?
  if @options.disable_diagnostics_logging
    diagnostics.clear_markers(context)
    return
  end

  event = StatsigEvent.new($diagnostics_event)
  event.user = user
  serialized = diagnostics.serialize_with_sampling(context)
  diagnostics.clear_markers(context)
  return if serialized[:markers].empty?

  event. = serialized
  log_event(event)
end

#log_event(event) ⇒ Object



45
46
47
48
49
50
# File 'lib/statsig_logger.rb', line 45

def log_event(event)
  @events.push(event)
  if @events.length >= @options.logging_max_buffer_size
    flush_async
  end
end

#log_gate_exposure(user, result, context = nil) ⇒ Object



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

def log_gate_exposure(user, result, context = nil)
  should_log, logged_sampling_rate, shadow_logged = determine_sampling(EntityType::GATE, result.name, result, user)
  return unless should_log
  event = StatsigEvent.new($gate_exposure_event)
  event.user = user
   = {
    gate: result.name,
    gateValue: result.gate_value.to_s,
    ruleID: result.rule_id || Statsig::Const::EMPTY_STR,
  }
  if result.config_version != nil
    [:configVersion] = result.config_version.to_s
  end
  if @debug_info != nil
    [:debugInfo] = @debug_info
  end
  unless result.override_config_name.nil?
    [:overrideConfigName] = result.override_config_name
  end
  return false if not is_unique_exposure(user, $gate_exposure_event, )
  event. = 
  event. = {}

  event.secondary_exposures = result.secondary_exposures.is_a?(Array) ? result.secondary_exposures : []

  safe_add_eval_details(result.evaluation_details, event)
  safe_add_exposure_context(context, event)
  (event, logged_sampling_rate, shadow_logged)
  log_event(event)
end

#log_layer_exposure(user, layer, parameter_name, config_evaluation, context = nil) ⇒ Object



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

def log_layer_exposure(user, layer, parameter_name, config_evaluation, context = nil)
  exposures = config_evaluation.undelegated_sec_exps || []
  allocated_experiment = Statsig::Const::EMPTY_STR
  is_explicit = (config_evaluation.explicit_parameters&.include? parameter_name) || false
  if is_explicit
    allocated_experiment = config_evaluation.config_delegate
    exposures = config_evaluation.secondary_exposures
  end

  should_log, logged_sampling_rate, shadow_logged = determine_sampling(EntityType::LAYER, config_evaluation.name, config_evaluation, user, allocated_experiment, parameter_name)
  return unless should_log

  event = StatsigEvent.new($layer_exposure_event)
  event.user = user
   = {
    config: layer.name,
    ruleID: layer.rule_id || Statsig::Const::EMPTY_STR,
    allocatedExperiment: allocated_experiment,
    parameterName: parameter_name,
    isExplicitParameter: String(is_explicit)
  }
  if config_evaluation.config_version != nil
    [:configVersion] = config_evaluation.config_version.to_s
  end
  if @debug_info != nil
    [:debugInfo] = @debug_info
  end
  return false unless is_unique_exposure(user, $layer_exposure_event, )
  event. = 
  event.secondary_exposures = exposures.is_a?(Array) ? exposures : []
  event. = {}

  safe_add_eval_details(config_evaluation.evaluation_details, event)
  safe_add_exposure_context(context, event)
  (event, logged_sampling_rate, shadow_logged)
  log_event(event)
end

#maybe_restart_background_threadsObject



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

def maybe_restart_background_threads
  if @background_flush.nil? || !@background_flush.alive?
    @background_flush = periodic_flush
  end
end

#periodic_flushObject



168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/statsig_logger.rb', line 168

def periodic_flush
  Thread.new do
    @error_boundary.capture() do
      loop do
        sleep @options.logging_interval_seconds
        flush_async
        @interval += 1
        @deduper.clear if @interval % 2 == 0
      end
    end
  end
end

#set_debug_info(debug_info) ⇒ Object



217
218
219
# File 'lib/statsig_logger.rb', line 217

def set_debug_info(debug_info)
  @debug_info = debug_info
end

#shutdownObject



181
182
183
184
185
186
187
# File 'lib/statsig_logger.rb', line 181

def shutdown
  @background_flush&.exit
  @sampling_key_set.shutdown
  @logging_pool.shutdown
  @logging_pool.wait_for_termination(timeout = 3)
  flush
end