statsig 2.9.6 → 2.9.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '0652683b8a04c0258b6d37d076ac8ae2d44523fdb52d042d992dcc948693e1e7'
4
- data.tar.gz: c7fd0cf211bcfdb60164f988109ee155f18dd534f6b8e97c767b9a1d68cfd3f9
3
+ metadata.gz: 88105ccc6be1be7d86382983b72dcc4e13293bd065023adfc95ce42bdf1e6359
4
+ data.tar.gz: 3d9926e559b76805e25ed37249c9998b14e90a1a5c23855b17e93daf9389f878
5
5
  SHA512:
6
- metadata.gz: 295de37f1befbfe8e627a2d70e34597dae27791b4850433ab61295120fb15aefd4cfee1f311872ee183254364cbcf8c10746eddd878db24459470ed9654f4be6
7
- data.tar.gz: d84fa2839e8a55e1b029d932b30c381172dd75190380d37b6750d7716593bb5dc9ea849ebbb94b3c58c237daffe4718f7987bbfdb2a2e9054d6f64d62eb2743c
6
+ metadata.gz: cd63e35e62beb964da2bab585e987d1178d5bfb27d2d7c58fa5d5bf6a76659c81c3a8a969446207a3a3415ee657dd39a2dc877d14e8b85447e57d340afd726c6
7
+ data.tar.gz: 7dca5dfa17127bd11fe0170f790ddec7c55f490db4b880d8b741f104823943dc61910b46cbbe11983d1c5bfe81e1bde08676c039f67e62ec45f5f5290a527d5e
data/lib/config_result.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require_relative 'constants'
2
+
1
3
  module Statsig
2
4
  class ConfigResult
3
5
 
@@ -78,18 +80,24 @@ module Statsig
78
80
  from_hash(config_name, sticky_values)
79
81
  end
80
82
 
83
+ # Persisted (sticky) value for this result. String keys match how values are read
84
+ # back from storage. `value` and `time` duplicate `gate_value` and `config_sync_time`
85
+ # under the names Server Core SDKs read, so a store can be shared with them.
81
86
  def to_hash
87
+ config_sync_time = @evaluation_details&.config_sync_time
82
88
  {
83
- json_value: @json_value,
84
- gate_value: @gate_value,
85
- rule_id: @rule_id,
86
- secondary_exposures: @secondary_exposures,
87
- config_sync_time: @evaluation_details.config_sync_time,
88
- init_time: @init_time,
89
- group_name: @group_name,
90
- id_type: @id_type,
91
- target_app_ids: @target_app_ids,
92
- override_config_name: @override_config_name
89
+ Const::JSON_VALUE => @json_value,
90
+ Const::GATE_VALUE => @gate_value,
91
+ Const::VALUE => @gate_value,
92
+ Const::RULE_ID => @rule_id,
93
+ Const::SECONDARY_EXPOSURES => @secondary_exposures,
94
+ Const::CONFIG_SYNC_TIME => config_sync_time,
95
+ Const::TIME => config_sync_time,
96
+ Const::INIT_TIME => @evaluation_details&.init_time,
97
+ Const::GROUP_NAME => @group_name,
98
+ Const::ID_TYPE => @id_type,
99
+ Const::TARGET_APP_IDS => @target_app_ids,
100
+ Const::OVERRIDE_CONFIG_NAME => @override_config_name
93
101
  }
94
102
  end
95
103
  end
data/lib/constants.rb CHANGED
@@ -68,6 +68,10 @@ module Statsig
68
68
  TARGET_APP_IDS = 'target_app_ids'.freeze
69
69
  CONFIG_SYNC_TIME = 'config_sync_time'.freeze
70
70
  INIT_TIME = 'init_time'.freeze
71
+ OVERRIDE_CONFIG_NAME = 'override_config_name'.freeze
72
+ # Field names used by Statsig Server Core SDKs for the same values
73
+ VALUE = 'value'.freeze
74
+ TIME = 'time'.freeze
71
75
 
72
76
  # Spec Types
73
77
  TYPE_FEATURE_GATE = 'feature_gate'.freeze
@@ -1,3 +1,4 @@
1
+ require 'json'
1
2
  require_relative 'statsig_errors'
2
3
 
3
4
  $endpoint = 'https://statsigapi.net/v1/sdk_exception'
@@ -27,6 +28,45 @@ module Statsig
27
28
  return res
28
29
  end
29
30
 
31
+ MAX_MESSAGE_LENGTH = 200
32
+ REPORT_THREAD_NAME = 'statsig-error-report'.freeze
33
+
34
+ # Some exception messages embed the input: json < 2.10 ends ParserError#message with
35
+ # "at '<rest of the document>'". Returns a copy without the document and at most
36
+ # MAX_MESSAGE_LENGTH long, keeping the class and backtrace.
37
+ def self.with_short_message(exception)
38
+ original = exception.message.to_s
39
+ message = original
40
+ message = message.split(" at '", 2).first if exception.is_a?(JSON::ParserError)
41
+ message = message[0, MAX_MESSAGE_LENGTH]
42
+ return exception if message == original
43
+
44
+ short = exception.exception(message)
45
+ short.set_backtrace(exception.backtrace) if exception.backtrace
46
+ short
47
+ end
48
+
49
+ # Reports on a background thread so callers (initialize, config sync, flush) never wait
50
+ # on, or fail because of, the sdk_exception request.
51
+ def log_exception_async(exception, tag: nil, extra: {}, force: false)
52
+ return if @local_mode
53
+ return if @seen.include?(exception.class.name) && !force
54
+
55
+ thread = Thread.new do
56
+ begin
57
+ log_exception(exception, tag: tag, extra: extra, force: force)
58
+ rescue Exception # rubocop:disable Lint/RescueException
59
+ # Best effort only. This also covers non-StandardError exceptions such as WebMock's
60
+ # NetConnectNotAllowedError, which Thread.abort_on_exception would re-raise in the app.
61
+ end
62
+ end
63
+ thread.name = REPORT_THREAD_NAME
64
+ thread
65
+ rescue StandardError
66
+ # e.g. ThreadError when the process cannot create more threads. Reporting is optional.
67
+ nil
68
+ end
69
+
30
70
  def log_exception(exception, tag: nil, extra: {}, force: false)
31
71
  if @local_mode
32
72
  return
data/lib/evaluator.rb CHANGED
@@ -224,16 +224,17 @@ module Statsig
224
224
  if !user_persisted_values.nil? && config[:isActive] == true
225
225
  sticky_values = user_persisted_values[config_name]
226
226
  unless sticky_values.nil?
227
- end_result.gate_value = sticky_values[Statsig::Const::GATE_VALUE]
227
+ # Entries written by Server Core SDKs use `value`/`time` and omit `id_type`.
228
+ end_result.gate_value = value_or_fallback(sticky_values, Statsig::Const::GATE_VALUE, Statsig::Const::VALUE)
228
229
  end_result.json_value = sticky_values[Statsig::Const::JSON_VALUE]
229
230
  end_result.rule_id = sticky_values[Statsig::Const::RULE_ID]
230
231
  end_result.secondary_exposures = sticky_values[Statsig::Const::SECONDARY_EXPOSURES]
231
232
  end_result.group_name = sticky_values[Statsig::Const::GROUP_NAME]
232
- end_result.id_type = sticky_values[Statsig::Const::ID_TYPE]
233
+ end_result.id_type = sticky_values[Statsig::Const::ID_TYPE] || config[:idType]
233
234
  end_result.target_app_ids = sticky_values[Statsig::Const::TARGET_APP_IDS]
234
235
  unless end_result.disable_evaluation_details
235
236
  end_result.evaluation_details = EvaluationDetails.persisted(
236
- sticky_values[Statsig::Const::CONFIG_SYNC_TIME],
237
+ value_or_fallback(sticky_values, Statsig::Const::CONFIG_SYNC_TIME, Statsig::Const::TIME),
237
238
  sticky_values[Statsig::Const::INIT_TIME]
238
239
  )
239
240
  end
@@ -254,6 +255,12 @@ module Statsig
254
255
  end
255
256
  end
256
257
 
258
+ # `||` is avoided so a stored `false` is not replaced by the fallback.
259
+ def value_or_fallback(hash, key, fallback_key)
260
+ value = hash[key]
261
+ value.nil? ? hash[fallback_key] : value
262
+ end
263
+
257
264
  def eval_cmab(config_name, user, end_result)
258
265
  return false unless @spec_store.has_cmab_config?(config_name)
259
266
 
data/lib/hash_utils.rb CHANGED
@@ -27,6 +27,22 @@ module Statsig
27
27
  return Digest::MD5.base64digest(input_str)
28
28
  end
29
29
 
30
+ # Returns a copy where ASCII-8BIT (BINARY) strings are read as UTF-8, which is what json 2.6+
31
+ # did when generating. json 3.1 raises for non-ASCII BINARY strings, and Rack servers such as
32
+ # Puma pass header values (e.g. the User-Agent) as BINARY. Only used after generation fails.
33
+ def self.binary_strings_as_utf8(value)
34
+ case value
35
+ when String
36
+ value.encoding == Encoding::BINARY ? value.dup.force_encoding(Encoding::UTF_8) : value
37
+ when Hash
38
+ value.each_with_object({}) { |(k, v), copy| copy[binary_strings_as_utf8(k)] = binary_strings_as_utf8(v) }
39
+ when Array
40
+ value.map { |v| binary_strings_as_utf8(v) }
41
+ else
42
+ value
43
+ end
44
+ end
45
+
30
46
  def self.sortHash(input_hash)
31
47
  dictionary = input_hash.clone.sort_by { |key| key }.to_h;
32
48
  input_hash.each do |key, value|
data/lib/network.rb CHANGED
@@ -64,7 +64,12 @@ module Statsig
64
64
  def post_logs(events, error_boundary)
65
65
  url = @options.log_event_url
66
66
  event_count = events.length
67
- json_body = JSON.generate({ events: events, statsigMetadata: Statsig.get_statsig_metadata })
67
+ payload = { events: events, statsigMetadata: Statsig.get_statsig_metadata }
68
+ json_body = begin
69
+ JSON.generate(payload, allow_duplicate_key: true)
70
+ rescue JSON::GeneratorError, EncodingError
71
+ JSON.generate(Statsig::HashUtils.binary_strings_as_utf8(payload), allow_duplicate_key: true)
72
+ end
68
73
  gzip = Zlib::GzipWriter.new(StringIO.new)
69
74
  gzip << json_body
70
75
 
@@ -76,8 +81,10 @@ module Statsig
76
81
  error_boundary.log_exception(e, tag: 'statsig::log_event_failed', extra: { eventCount: event_count, error: message }, force: true)
77
82
  return
78
83
  end
79
- rescue StandardError
80
-
84
+ rescue StandardError => e
85
+ error = Statsig::ErrorBoundary.with_short_message(e)
86
+ puts "[Statsig]: Failed to log events: #{error.message}"
87
+ error_boundary.log_exception_async(error, tag: 'statsig::log_event_unexpected_error')
81
88
  end
82
89
 
83
90
  def get_id_lists(context)
data/lib/spec_store.rb CHANGED
@@ -61,17 +61,20 @@ module Statsig
61
61
  fallback_policy: :discard
62
62
  )
63
63
 
64
+ bootstrap_failure = nil
64
65
  unless @options.bootstrap_values.nil?
65
66
  if !@options.data_store.nil?
66
67
  puts 'data_store gets priority over bootstrap_values. bootstrap_values will be ignored'
67
68
  else
68
69
  tracker = @diagnostics.track('initialize','bootstrap', 'process')
69
70
  begin
70
- if process_specs(options.bootstrap_values).nil?
71
+ bootstrap_failure = process_specs(options.bootstrap_values)
72
+ if bootstrap_failure.nil?
71
73
  @init_reason = EvaluationReason::BOOTSTRAP
74
+ else
75
+ reason = bootstrap_failure[:exception]&.message || bootstrap_failure[:reason]
76
+ puts "[Statsig]: Failed to process bootstrap_values: #{reason}"
72
77
  end
73
- rescue StandardError
74
- puts 'the provided bootstrapValues is not a valid JSON string'
75
78
  ensure
76
79
  tracker.end(success: @init_reason == EvaluationReason::BOOTSTRAP)
77
80
  end
@@ -92,6 +95,11 @@ module Statsig
92
95
  end
93
96
  end
94
97
 
98
+ # With nothing else to explain it (e.g. local_mode), report why bootstrap_values were not used.
99
+ if @init_reason == EvaluationReason::UNINITIALIZED && failure_details.nil?
100
+ failure_details = bootstrap_failure
101
+ end
102
+
95
103
  @initial_config_sync_time = @last_config_sync_time == 0 ? -1 : @last_config_sync_time
96
104
  if !@options.data_store.nil?
97
105
  get_id_lists_from_adapter('initialize')
@@ -290,6 +298,8 @@ module Statsig
290
298
  failure_details = process_specs(body)
291
299
  if failure_details.nil?
292
300
  @init_reason = EvaluationReason::NETWORK
301
+ elsif context == 'initialize'
302
+ error = failure_details[:exception]
293
303
  end
294
304
  tracker.end(success: @init_reason == EvaluationReason::NETWORK)
295
305
 
@@ -380,12 +390,12 @@ module Statsig
380
390
  end
381
391
 
382
392
  begin
383
- specs_json = JSON.parse(specs_string, { symbolize_names: true })
393
+ specs_json = JSON.parse(specs_string, symbolize_names: true)
384
394
  return {reason: "PARSE_RESPONSE_ERROR"} unless specs_json.is_a? Hash
385
395
 
386
396
  hashed_sdk_key_used = specs_json[:hashed_sdk_key_used]
387
397
  unless hashed_sdk_key_used.nil? or hashed_sdk_key_used == Statsig::HashUtils.djb2(@secret_key)
388
- err_boundary.log_exception(Statsig::InvalidSDKKeyResponse.new)
398
+ @error_boundary.log_exception_async(Statsig::InvalidSDKKeyResponse.new)
389
399
  return {reason: "PARSE_RESPONSE_ERROR"}
390
400
  end
391
401
 
@@ -421,7 +431,9 @@ module Statsig
421
431
  save_rulesets_to_storage_adapter(specs_string)
422
432
  end
423
433
  rescue StandardError => e
424
- return {reason: "PARSE_RESPONSE_ERROR"}
434
+ error = Statsig::ErrorBoundary.with_short_message(e)
435
+ @error_boundary.log_exception_async(error, tag: 'process_specs')
436
+ return {reason: "PARSE_RESPONSE_ERROR", exception: error}
425
437
  end
426
438
  nil
427
439
  end
data/lib/statsig.rb CHANGED
@@ -8,7 +8,7 @@ module Statsig
8
8
  #
9
9
  # @param secret_key The server SDK key copied from console.statsig.com
10
10
  # @param options The StatsigOptions object used to configure the SDK
11
- # @param error_callback A callback function, called if the initialize network call fails
11
+ # @param error_callback A callback function, called if the initialize network call fails or its response cannot be parsed
12
12
  def self.initialize(secret_key, options = nil, error_callback = nil)
13
13
  unless @shared_instance.nil?
14
14
  puts 'Statsig already initialized.'
@@ -410,7 +410,7 @@ module Statsig
410
410
  def self.get_statsig_metadata
411
411
  {
412
412
  'sdkType' => 'ruby-server',
413
- 'sdkVersion' => '2.9.6',
413
+ 'sdkVersion' => '2.9.7',
414
414
  'languageVersion' => RUBY_VERSION
415
415
  }
416
416
  end
data/lib/statsig_user.rb CHANGED
@@ -89,7 +89,7 @@ class StatsigUser
89
89
  def initialize(user_hash)
90
90
  the_hash = user_hash
91
91
  begin
92
- the_hash = JSON.parse(user_hash&.to_json || "")
92
+ the_hash = clone_user_hash(user_hash)
93
93
  rescue
94
94
  puts 'Failed to clone user hash'
95
95
  end
@@ -223,6 +223,15 @@ class StatsigUser
223
223
  @dirty = true
224
224
  end
225
225
 
226
+ # Deep copy through JSON, keeping json 2.6+ behavior on json 3: the last duplicate key wins and
227
+ # BINARY strings are read as UTF-8.
228
+ def clone_user_hash(user_hash)
229
+ JSON.parse(user_hash&.to_json(allow_duplicate_key: true) || "", allow_duplicate_key: true)
230
+ rescue JSON::GeneratorError, EncodingError
231
+ utf8_hash = Statsig::HashUtils.binary_strings_as_utf8(user_hash)
232
+ JSON.parse(utf8_hash.to_json(allow_duplicate_key: true), allow_duplicate_key: true)
233
+ end
234
+
226
235
  # Pulls fields from the user hash via Symbols and Strings
227
236
  def from_hash(user_hash, keys, type)
228
237
  if user_hash.nil?
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: statsig
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.9.6
4
+ version: 2.9.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Statsig, Inc
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-11-04 00:00:00.000000000 Z
11
+ date: 2026-12-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler