Class: XRay::LocalSampler

Inherits:
Object
  • Object
show all
Includes:
Sampler
Defined in:
lib/aws-xray-sdk/sampling/local/sampler.rb

Overview

The local sampler that uses locally defined sampling rule and reservoir models to decide sampling decision. It also uses the default sampling rule. An example definition:

{
  version: 2,
  rules: [
    {
      description: 'Player moves.',
      host: '*',
      http_method: '*',
      url_path: '/api/move/*',
      fixed_target: 0,
      rate: 0.05
    }
  ],
  default: {
    fixed_target: 1,
    rate: 0.1
  }
}

This example defines one custom rule and a default rule. The custom rule applies a five-percent sampling rate with no minimum number of requests to trace for paths under /api/move/. The default rule traces the first request each second and 10 percent of additional requests. The SDK applies custom rules in the order in which they are defined. If a request matches multiple custom rules, the SDK applies only the first rule.

Constant Summary collapse

DEFAULT_RULES =
{
  version: 2,
  default: {
    fixed_target: 1,
    rate: 0.05
  },
  rules: []
}.freeze
SUPPORTED_VERSION =
[1, 2].freeze

Instance Method Summary collapse

Constructor Details

#initializeLocalSampler

Returns a new instance of LocalSampler.



46
47
48
# File 'lib/aws-xray-sdk/sampling/local/sampler.rb', line 46

def initialize
  load_sampling_rules(DEFAULT_RULES)
end

Instance Method Details

#sample?Boolean

Decides if should sample based on non-path-based rule. Currently only the default rule is non-path-based.

Returns:

  • (Boolean)


70
71
72
# File 'lib/aws-xray-sdk/sampling/local/sampler.rb', line 70

def sample?
  should_sample?(@default_rule)
end

#sample_request?(sampling_req) ⇒ Boolean

Return True if the sampler decide to sample based on input information and sampling rules. It will first check if any custom rule should be applied, if not it falls back to the default sampling rule. All arugments are extracted from incoming requests by X-Ray middleware to perform path based sampling.

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
64
65
66
# File 'lib/aws-xray-sdk/sampling/local/sampler.rb', line 56

def sample_request?(sampling_req)
  sample = sample?
  return sample if sampling_req.nil? || sampling_req.empty?
  @custom_rules ||= []
  @custom_rules.each do |c|
    return should_sample?(c) if c.applies?(sampling_req)
  end
  # use previously made decision based on default rule
  # if no path-based rule has been matched
  sample
end

#sampling_rulesArray

Returns An array of [SamplingRule].

Returns:

  • (Array)

    An array of [SamplingRule]



80
81
82
83
84
85
# File 'lib/aws-xray-sdk/sampling/local/sampler.rb', line 80

def sampling_rules
  all_rules = []
  all_rules << @default_rule
  all_rules << @custom_rules unless @custom_rules.empty?
  all_rules
end

#sampling_rules=(v) ⇒ Object

Parameters:

  • v (Hash)

    The sampling rules definition.



75
76
77
# File 'lib/aws-xray-sdk/sampling/local/sampler.rb', line 75

def sampling_rules=(v)
  load_sampling_rules(v)
end