Class: XRay::DefaultSampler

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

Overview

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

{
  version: 1,
  rules: [
    {
      description: 'Player moves.',
      service_name: '*',
      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: 1,
  default: {
    fixed_target: 1,
    rate: 0.05
  },
  rules: []
}.freeze

Instance Method Summary collapse

Constructor Details

#initializeDefaultSampler

Returns a new instance of DefaultSampler.



44
45
46
# File 'lib/aws-xray-sdk/sampling/default_sampler.rb', line 44

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 not path-based.

Returns:

  • (Boolean)


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

def sample?
  should_sample?(@default_rule)
end

#sample_request?(service_name:, url_path:, http_method:) ⇒ 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)


54
55
56
57
58
59
60
61
62
# File 'lib/aws-xray-sdk/sampling/default_sampler.rb', line 54

def sample_request?(service_name:, url_path:, http_method:)
  # directly fallback to non-path-based if all arguments are nil
  return sample? unless service_name || url_path || http_method
  @custom_rules ||= []
  @custom_rules.each do |c|
    return should_sample?(c) if c.applies?(target_name: service_name, target_path: url_path, target_method: http_method)
  end
  sample?
end

#sampling_rulesArray

Returns An array of [SamplingRule].

Returns:

  • (Array)

    An array of [SamplingRule]



76
77
78
79
80
81
# File 'lib/aws-xray-sdk/sampling/default_sampler.rb', line 76

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.



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

def sampling_rules=(v)
  load_sampling_rules(v)
end