Class: OpenTelemetry::Sampler::XRay::RuleCache
- Inherits:
-
Object
- Object
- OpenTelemetry::Sampler::XRay::RuleCache
- Defined in:
- lib/opentelemetry/sampler/xray/rule_cache.rb
Overview
RuleCache stores all the Sampling Rule Appliers, each corresponding to the user's Sampling Rules that were retrieved from AWS X-Ray
Constant Summary collapse
- RULE_CACHE_TTL_MILLIS =
The cache expires 1 hour after the last refresh time.
60 * 60 * 1000
Instance Method Summary collapse
- #create_sampling_statistics_documents(client_id) ⇒ Object
- #expired? ⇒ Boolean
- #get_matched_rule(attributes) ⇒ Object
-
#initialize(sampler_resource) ⇒ RuleCache
constructor
A new instance of RuleCache.
- #update_rules(new_rule_appliers) ⇒ Object
- #update_targets(target_documents, last_rule_modification) ⇒ Object
Constructor Details
#initialize(sampler_resource) ⇒ RuleCache
Returns a new instance of RuleCache.
16 17 18 19 20 21 |
# File 'lib/opentelemetry/sampler/xray/rule_cache.rb', line 16 def initialize(sampler_resource) @rule_appliers = [] @sampler_resource = sampler_resource @last_updated_epoch_millis = Time.now.to_i * 1000 @cache_lock = Mutex.new end |
Instance Method Details
#create_sampling_statistics_documents(client_id) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/opentelemetry/sampler/xray/rule_cache.rb', line 56 def create_sampling_statistics_documents(client_id) statistics_documents = [] @cache_lock.synchronize do @rule_appliers.each do |rule| statistics = rule.snapshot_statistics now_in_seconds = Time.now.to_i sampling_statistics_doc = { ClientID: client_id, RuleName: rule.sampling_rule.rule_name, Timestamp: now_in_seconds, RequestCount: statistics.request_count, BorrowCount: statistics.borrow_count, SampledCount: statistics.sample_count } statistics_documents << sampling_statistics_doc end end statistics_documents end |
#expired? ⇒ Boolean
23 24 25 26 |
# File 'lib/opentelemetry/sampler/xray/rule_cache.rb', line 23 def expired? now_in_millis = Time.now.to_i * 1000 now_in_millis > @last_updated_epoch_millis + RULE_CACHE_TTL_MILLIS end |
#get_matched_rule(attributes) ⇒ Object
28 29 30 31 32 |
# File 'lib/opentelemetry/sampler/xray/rule_cache.rb', line 28 def get_matched_rule(attributes) @rule_appliers.find do |rule| rule.matches?(attributes, @sampler_resource) || rule.sampling_rule.rule_name == 'Default' end end |
#update_rules(new_rule_appliers) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/opentelemetry/sampler/xray/rule_cache.rb', line 34 def update_rules(new_rule_appliers) old_rule_appliers_map = {} @cache_lock.synchronize do @rule_appliers.each do |rule| old_rule_appliers_map[rule.sampling_rule.rule_name] = rule end new_rule_appliers.each_with_index do |new_rule, index| rule_name_to_check = new_rule.sampling_rule.rule_name next unless old_rule_appliers_map.key?(rule_name_to_check) old_rule = old_rule_appliers_map[rule_name_to_check] new_rule_appliers[index] = old_rule if new_rule.sampling_rule.equals?(old_rule.sampling_rule) end @rule_appliers = new_rule_appliers sort_rules_by_priority @last_updated_epoch_millis = Time.now.to_i * 1000 end end |
#update_targets(target_documents, last_rule_modification) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/opentelemetry/sampler/xray/rule_cache.rb', line 80 def update_targets(target_documents, last_rule_modification) min_polling_interval = nil next_polling_interval = DEFAULT_TARGET_POLLING_INTERVAL_SECONDS @cache_lock.synchronize do @rule_appliers.each_with_index do |rule, index| target = target_documents[rule.sampling_rule.rule_name] if target @rule_appliers[index] = rule.with_target(target) min_polling_interval = target['Interval'] if target['Interval'] && (min_polling_interval.nil? || min_polling_interval > target['Interval']) else OpenTelemetry.logger.debug('Invalid sampling target: missing rule name') end end next_polling_interval = min_polling_interval if min_polling_interval refresh_sampling_rules = last_rule_modification * 1000 > @last_updated_epoch_millis return [refresh_sampling_rules, next_polling_interval] end end |