Class: XRay::RuleCache

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/aws-xray-sdk/sampling/rule_cache.rb

Overview

Cache sampling rules and quota retrieved by ‘TargetPoller` and `RulePoller`. It will not return anything if it expires.

Constant Summary collapse

@@TTL =

1 hour

60 * 60

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger, logger, logger=

Constructor Details

#initializeRuleCache

Returns a new instance of RuleCache.



11
12
13
14
15
# File 'lib/aws-xray-sdk/sampling/rule_cache.rb', line 11

def initialize
  @rules = []
  @last_updated = nil
  @lock = Mutex.new
end

Instance Attribute Details

#last_updatedObject

Returns the value of attribute last_updated.



8
9
10
# File 'lib/aws-xray-sdk/sampling/rule_cache.rb', line 8

def last_updated
  @last_updated
end

Instance Method Details

#get_matched_rule(sampling_req, now: Time.now.to_i) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/aws-xray-sdk/sampling/rule_cache.rb', line 17

def get_matched_rule(sampling_req, now: Time.now.to_i)
  return nil if expired?(now)
  matched = nil
  rules.each do |rule|
    matched = rule if matched.nil? && rule.applies?(sampling_req)
    matched = rule if matched.nil? && rule.default?
  end
  matched
end

#load_rules(new_rules) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/aws-xray-sdk/sampling/rule_cache.rb', line 27

def load_rules(new_rules)
  @lock.synchronize do
    # Simply assign rules and sort if cache is empty
    if @rules.empty?
      @rules = new_rules
      return sort_rules
    end

    # otherwise we need to merge new rules and current rules
    curr_rules = {}
    @rules.each do |rule|
      curr_rules[rule.name] = rule
    end
    # Update the rules in the cache
    @rules = new_rules
    # Transfer state information
    @rules.each do |rule|
      curr_rule = curr_rules[rule.name]
      rule.merge(curr_rule) unless curr_rule.nil?
    end
    sort_rules
  end
end

#load_targets(targets_h) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/aws-xray-sdk/sampling/rule_cache.rb', line 51

def load_targets(targets_h)
  @lock.synchronize do
    @rules.each do |rule|
      target = targets_h[rule.name]
      next if target.nil?
      rule.rate = target.fixed_rate
      rule.reservoir.load_target_info(
        quota: target.reservoir_quota,
        ttl: target.reservoir_quota_ttl,
        interval: target.interval
      )
    end
  end
end

#rulesObject



66
67
68
69
70
# File 'lib/aws-xray-sdk/sampling/rule_cache.rb', line 66

def rules
  @lock.synchronize do
    @rules
  end
end