class XRay::LeadPoller

The poller to report the current statistics of all sampling rules and retrieve the new allocated sampling quota and TTL from X-Ray service. It also controls the rule poller.

Attributes

connector[R]

Public Class Methods

new(cache) click to toggle source
# File lib/aws-xray-sdk/sampling/lead_poller.rb, line 16
def initialize(cache)
  @cache = cache
  @connector = ServiceConnector.new
  @rule_poller = RulePoller.new cache: @cache, connector: @connector
  @rule_poller_elapsed = 0
end

Public Instance Methods

start() click to toggle source
# File lib/aws-xray-sdk/sampling/lead_poller.rb, line 23
def start
  @rule_poller.run
  Thread.new { worker }
end
worker() click to toggle source
# File lib/aws-xray-sdk/sampling/lead_poller.rb, line 28
def worker
  loop do
    sleep_time = @@interval + rand
    sleep sleep_time
    @rule_poller_elapsed += sleep_time
    refresh_cache
    if @rule_poller_elapsed >= @@rule_interval
      @rule_poller.run
      @rule_poller_elapsed = 0
    end
  end
end

Private Instance Methods

get_candidates(rules) click to toggle source

Don't report a rule statistics if any of the conditions is met:

  1. The report time hasn't come(some rules might have larger report intervals).

  2. The rule is never matched.

# File lib/aws-xray-sdk/sampling/lead_poller.rb, line 66
def get_candidates(rules)
  candidates = []
  rules.each { |rule| candidates << rule if rule.ever_matched? && rule.time_to_report? }
  candidates
end
refresh_cache() click to toggle source
# File lib/aws-xray-sdk/sampling/lead_poller.rb, line 43
def refresh_cache
  candidates = get_candidates(@cache.rules)
  if candidates.empty?
    logger.debug %(No X-Ray sampling rules to report statistics. Skipping.)
    return
  end

  result = @connector.fetch_sampling_targets(candidates)
  targets = {}
  result[:documents].each { |doc| targets[doc.rule_name] = doc }
  @cache.load_targets(targets)

  return unless @cache.last_updated && result[:last_modified].to_i > @cache.last_updated
  logger.info 'Performing out-of-band sampling rule polling to fetch updated rules.'
  @rule_poller.run
  @rule_poller_elapsed = 0
rescue StandardError => e
  logger.warn %(failed to fetch X-Ray sampling targets due to #{e.message})
end