class XRay::DefaultSampler

Making sampling decisions based on service sampling rules defined by X-Ray control plane APIs. It will fall back to local sampling rules if service sampling rules are not available or expired.

Attributes

cache[R]
local_sampler[R]
origin[RW]
poller[R]

Public Class Methods

new() click to toggle source
# File lib/aws-xray-sdk/sampling/default_sampler.rb, line 19
def initialize
  @local_sampler = LocalSampler.new
  @cache = RuleCache.new
  @poller = LeadPoller.new(@cache)

  @started = false
  @origin = nil
  @lock = Mutex.new
end

Public Instance Methods

daemon_config=(v) click to toggle source
# File lib/aws-xray-sdk/sampling/default_sampler.rb, line 73
def daemon_config=(v)
  @poller.connector.daemon_config = v
end
sample?() click to toggle source
# File lib/aws-xray-sdk/sampling/default_sampler.rb, line 61
def sample?
  sample_request? nil
end
sample_request?(sampling_req) click to toggle source

Return the rule name if it decides to sample based on a service sampling rule matching. If there is no match it will fallback to local defined sampling rules.

# File lib/aws-xray-sdk/sampling/default_sampler.rb, line 42
def sample_request?(sampling_req)
  start unless @started
  now = Time.now.to_i
  if sampling_req.nil?
    sampling_req = { service_type: @origin } if @origin
  elsif !sampling_req.key?(:service_type)
    sampling_req[:service_type] = @origin if @origin
  end

  matched_rule = @cache.get_matched_rule(sampling_req, now: now)
  if !matched_rule.nil?
    logger.debug %(Rule #{matched_rule.name} is selected to make a sampling decision.')
    process_matched_rule(matched_rule, now)
  else
    logger.warn %(No effective centralized sampling rule match. Fallback to local rules.)
    @local_sampler.sample_request?(sampling_req)
  end
end
sampling_rules=(v) click to toggle source

@param [Hash] v Local sampling rules definition. This configuration has lower priority than service sampling rules and only has effect when those rules are not available or expired.

# File lib/aws-xray-sdk/sampling/default_sampler.rb, line 69
def sampling_rules=(v)
  @local_sampler.sampling_rules = v
end
start() click to toggle source

Start background threads to poll sampling rules

# File lib/aws-xray-sdk/sampling/default_sampler.rb, line 30
def start
  @lock.synchronize do
    unless @started
      @poller.start
      @started = true
    end
  end
end

Private Instance Methods

process_matched_rule(rule, now) click to toggle source
# File lib/aws-xray-sdk/sampling/default_sampler.rb, line 79
def process_matched_rule(rule, now)
  # As long as a rule is matched we increment request counter.
  rule.increment_request_count
  reservoir = rule.reservoir
  sample = true
  # We check if we can borrow or take from reservoir first.
  decision = reservoir.borrow_or_take(now, rule.borrowable?)
  if decision == SamplingDecision::BORROW
    rule.increment_borrow_count
  elsif decision == SamplingDecision::TAKE
    rule.increment_sampled_count
    # Otherwise we compute based on fixed rate of this sampling rule.
  elsif rand <= rule.rate
    rule.increment_sampled_count
  else
    sample = false
  end
  sample ? rule.name : false
end