Class: XRay::DefaultSampler

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

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger, logger, logger=

Methods included from Sampler

#sampling_rules

Constructor Details

#initializeDefaultSampler

Returns a new instance of DefaultSampler.



19
20
21
22
23
24
25
26
27
# 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

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



16
17
18
# File 'lib/aws-xray-sdk/sampling/default_sampler.rb', line 16

def cache
  @cache
end

#local_samplerObject (readonly)

Returns the value of attribute local_sampler.



16
17
18
# File 'lib/aws-xray-sdk/sampling/default_sampler.rb', line 16

def local_sampler
  @local_sampler
end

#originObject

Returns the value of attribute origin.



17
18
19
# File 'lib/aws-xray-sdk/sampling/default_sampler.rb', line 17

def origin
  @origin
end

#pollerObject (readonly)

Returns the value of attribute poller.



16
17
18
# File 'lib/aws-xray-sdk/sampling/default_sampler.rb', line 16

def poller
  @poller
end

Instance Method Details

#daemon_config=(v) ⇒ Object



73
74
75
# File 'lib/aws-xray-sdk/sampling/default_sampler.rb', line 73

def daemon_config=(v)
  @poller.connector.daemon_config = v
end

#sample?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/aws-xray-sdk/sampling/default_sampler.rb', line 61

def sample?
  sample_request? nil
end

#sample_request?(sampling_req) ⇒ Boolean

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.

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 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) ⇒ Object

This configuration has lower priority than service sampling rules and only has effect when those rules are not available or expired.

Parameters:

  • v (Hash)

    Local sampling rules definition.



69
70
71
# File 'lib/aws-xray-sdk/sampling/default_sampler.rb', line 69

def sampling_rules=(v)
  @local_sampler.sampling_rules = v
end

#startObject

Start background threads to poll sampling rules



30
31
32
33
34
35
36
37
# 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