Class: Contrast::Utils::Assess::SamplingUtil
- Includes:
- Components::Interface, 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
Instance Method Summary collapse
-
#initialize ⇒ SamplingUtil
constructor
A new instance of SamplingUtil.
- #sample?(request) ⇒ Boolean
- #update ⇒ Object
Methods included from Components::Interface
Constructor Details
#initialize ⇒ SamplingUtil
Returns a new instance of SamplingUtil.
17 18 19 |
# File 'lib/contrast/utils/assess/sampling_util.rb', line 17 def initialize @requests = {} end |
Instance Method Details
#sample?(request) ⇒ Boolean
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 58 |
# File 'lib/contrast/utils/assess/sampling_util.rb', line 32 def sample? request history = request_history(request) history.hit # if sampling isn't enabled, we record all requests and take a # default amount of responses return [true, true] unless @enabled # 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 |
#update ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/contrast/utils/assess/sampling_util.rb', line 21 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 |