Class: Contrast::Utils::Assess::SamplingUtil

Inherits:
Object
  • Object
show all
Extended by:
Components::Sampling::InstanceMethods
Includes:
Singleton
Defined in:
lib/contrast/utils/assess/sampling_util.rb

Overview

SamplingUtil has methods for tracking the number of requests per time window

Defined Under Namespace

Classes: RequestHistory

Constant Summary

Constants included from Components::Sampling::Constants

Components::Sampling::Constants::DEFAULT_SAMPLING_BASELINE, Components::Sampling::Constants::DEFAULT_SAMPLING_ENABLED, Components::Sampling::Constants::DEFAULT_SAMPLING_REQUEST_FREQUENCY, Components::Sampling::Constants::DEFAULT_SAMPLING_RESPONSE_FREQUENCY, Components::Sampling::Constants::DEFAULT_SAMPLING_WINDOW_MS

Constants included from Components::ComponentBase

Components::ComponentBase::ENABLE

Constants included from Config::Diagnostics::Tools

Config::Diagnostics::Tools::CHECK

Constants included from Config::Diagnostics::SingletonTools

Config::Diagnostics::SingletonTools::API_CREDENTIALS, Config::Diagnostics::SingletonTools::CONTRAST_MARK

Instance Method Summary collapse

Methods included from Components::Sampling::ClassMethods

#reset_sampling_control, #sampling_control, #sampling_enabled?

Methods included from Components::ComponentBase

#canon_name, #config_values, #false?, #file_exists?, #stringify_array, #to_effective_config, #true?, #valid_cert?

Methods included from Config::Diagnostics::Tools

#add_effective_config_values, #add_single_effective_value

Methods included from Config::Diagnostics::SingletonTools

#flatten_settings, #to_config_values, #update_config, #value_to_s

Constructor Details

#initializeSamplingUtil

Returns a new instance of SamplingUtil.



16
17
18
# File 'lib/contrast/utils/assess/sampling_util.rb', line 16

def initialize
  @requests = {}
end

Instance Method Details

#sample?(request) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/contrast/utils/assess/sampling_util.rb', line 31

def sample? request
  # if sampling isn't enabled, we record all requests and take a
  # default amount of responses
  return [true, true] unless @enabled

  history = request_history(request)
  history.hit

  # if we've exceeded this sample window, reset it
  if history.elapsed >= @window_ms
    history.reset_window
    return [true, true]
  end

  # we have to take a baseline of this request/ response combo. we only
  # baseline the first response, but we'll do full request analysis up
  # to the baseline amount
  return [true, history.window_hit == 1] if history.window_hit < @baseline

  # Once the window_hit exceeds the baseline, we limit the analysis
  # based on @request_frequency and @response_frequency.
  non_baseline = history.window_hit - @baseline
  analyze_request = (non_baseline % @request_frequency).zero?
  analyze_response = (non_baseline % @response_frequency).zero?

  [analyze_request, analyze_response]
end

#updateObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/contrast/utils/assess/sampling_util.rb', line 20

def update
  self.class.reset_sampling_control
  @enabled            = self.class.sampling_enabled?
  @baseline           = self.class.sampling_control[:baseline]
  @request_frequency  = self.class.sampling_control[:request_frequency]
  @response_frequency = self.class.sampling_control[:response_frequency]
  @window_ms          = self.class.sampling_control[:window]

  true
end