Class: InternalEventsSamplingUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/wingify/utils/internal_events_sampling_util.rb

Overview

Helpers for reading and applying internal-events sampling settings.

Class Method Summary collapse

Class Method Details

.get_debug_event_sampling_percent(settings) ⇒ Float

Reads the debug-event sampling percentage for the server runtime from settings.

Parameters:

  • settings (SettingsModel, nil)

    Parsed settings from the server

Returns:

  • (Float)

    Configured debug sampling percentage (0-100)



66
67
68
69
70
# File 'lib/wingify/utils/internal_events_sampling_util.rb', line 66

def get_debug_event_sampling_percent(settings)
  sampling = settings ? settings.get_sampling : nil
  debug_sampling = sampling ? sampling.get_debug : nil
  get_runtime_sampling_percent(debug_sampling)
end

.get_default_sampling_percentFloat

Returns the default server sampling percentage when settings omit the value.

Returns:

  • (Float)

    Default sampling percentage (10.0)



24
25
26
# File 'lib/wingify/utils/internal_events_sampling_util.rb', line 24

def get_default_sampling_percent
  Constants::INTERNAL_EVENTS_DEFAULT_SAMPLING_PERCENT_SERVER
end

.get_runtime_sampling_percent(runtime_sampling_config) ⇒ Float

Reads the server sampling percentage from a runtime sampling config object.

Parameters:

Returns:

  • (Float)

    Sampling percentage for the server runtime



46
47
48
49
50
# File 'lib/wingify/utils/internal_events_sampling_util.rb', line 46

def get_runtime_sampling_percent(runtime_sampling_config)
  return get_default_sampling_percent if runtime_sampling_config.nil?

  normalize_sampling_percent(runtime_sampling_config.get_server)
end

.get_usage_stats_sampling_percent(settings) ⇒ Float

Reads the usage-stats sampling percentage for the server runtime from settings.

Parameters:

  • settings (SettingsModel, nil)

    Parsed settings from the server

Returns:

  • (Float)

    Configured usage-stats sampling percentage (0-100)



56
57
58
59
60
# File 'lib/wingify/utils/internal_events_sampling_util.rb', line 56

def get_usage_stats_sampling_percent(settings)
  sampling = settings ? settings.get_sampling : nil
  usage_sampling = sampling ? sampling.get_usage : nil
  get_runtime_sampling_percent(usage_sampling)
end

.is_sampled_debug_error_template_key(message_template_key) ⇒ Boolean

Checks whether a debug error template key is in the sampled set.

Parameters:

  • message_template_key (String)

    The msg_t value from the debug event

Returns:

  • (Boolean)

    true when the key requires sampling before send



104
105
106
# File 'lib/wingify/utils/internal_events_sampling_util.rb', line 104

def is_sampled_debug_error_template_key(message_template_key)
  SampledDebugErrorTemplateKeys.contains?(message_template_key)
end

.normalize_sampling_percent(sampling_value) ⇒ Float

Normalizes a sampling value to a valid percentage in the range [0, 100]. Falls back to the server default when the value is missing or invalid.

Parameters:

  • sampling_value (Numeric, nil)

    Raw value from settings

Returns:

  • (Float)

    Valid sampling percentage between 0 and 100



33
34
35
36
37
38
39
40
# File 'lib/wingify/utils/internal_events_sampling_util.rb', line 33

def normalize_sampling_percent(sampling_value)
  # Accept only numeric values that fall within 0–100 inclusive
  if !sampling_value.nil? && sampling_value.is_a?(Numeric) && sampling_value >= 0 && sampling_value <= 100
    return sampling_value.to_f
  end

  get_default_sampling_percent
end

.passes_sampling_percent(sampling_percent, random_value) ⇒ Boolean

Evaluates whether an event qualifies under the configured sampling percentage.

Parameters:

  • sampling_percent (Float)

    Configured sampling percentage (0-100)

  • random_value (Float)

    Random value in the range [0, 1)

Returns:

  • (Boolean)

    true when the random draw falls within the sampling threshold



94
95
96
97
98
# File 'lib/wingify/utils/internal_events_sampling_util.rb', line 94

def passes_sampling_percent(sampling_percent, random_value)
  # Map random [0, 1) to an integer percent bucket [0, 100]
  normalized_random_percent = (random_value * 101).floor
  normalized_random_percent <= sampling_percent
end

.should_apply_sampling(settings) ⇒ Boolean

Determines whether sampling should be evaluated before send. Controlled by alwaysApplySampling.server; defaults to false.

Parameters:

  • settings (SettingsModel, nil)

    Parsed settings from the server

Returns:

  • (Boolean)

    true when a sampling check is required before sending



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/wingify/utils/internal_events_sampling_util.rb', line 77

def should_apply_sampling(settings)
  return Constants::INTERNAL_EVENTS_DEFAULT_ALWAYS_APPLY_SAMPLING if settings.nil?

  always_apply_sampling = settings.get_always_apply_sampling
  # Missing config or missing server flag → use default (do not sample)
  if always_apply_sampling.nil? || always_apply_sampling.get_server.nil?
    return Constants::INTERNAL_EVENTS_DEFAULT_ALWAYS_APPLY_SAMPLING
  end

  always_apply_sampling.get_server
end