Module: OpenTelemetry::SDK::Trace::Samplers

Defined in:
lib/opentelemetry/sdk/trace/samplers.rb,
lib/opentelemetry/sdk/trace/samplers/result.rb,
lib/opentelemetry/sdk/trace/samplers/decision.rb,
lib/opentelemetry/sdk/trace/samplers/parent_or_else.rb,
lib/opentelemetry/sdk/trace/samplers/constant_sampler.rb,
lib/opentelemetry/sdk/trace/samplers/probability_sampler.rb

Overview

The Samplers module contains the sampling logic for OpenTelemetry. The reference implementation provides a ProbabilitySampler, ALWAYS_ON, ALWAYS_OFF, and ParentOrElse.

Custom samplers can be provided by SDK users. The required interface is:

should_sample?(trace_id:, parent_context:, links:, name:, kind:, attributes:) -> Result description -> String

Where:

Returns:

  • (Result)

    The sampling result.

Defined Under Namespace

Modules: Decision Classes: ConstantSampler, ParentOrElse, ProbabilitySampler, Result

Constant Summary collapse

ALWAYS_ON =
ConstantSampler.new(result: RECORD_AND_SAMPLED, description: 'AlwaysOnSampler')
ALWAYS_OFF =

Returns a Result with Decision::NOT_RECORD.

ConstantSampler.new(result: NOT_RECORD, description: 'AlwaysOffSampler')

Class Method Summary collapse

Class Method Details

.parent_or_else(delegate_sampler) ⇒ Object

Returns a new sampler. It either respects the parent span's sampling decision or delegates to delegate_sampler for root spans.

Parameters:

  • delegate_sampler (Sampler)

    The sampler to which the sampling decision is delegated for root spans.



59
60
61
# File 'lib/opentelemetry/sdk/trace/samplers.rb', line 59

def self.parent_or_else(delegate_sampler)
  ParentOrElse.new(delegate_sampler)
end

.probability(probability, ignore_parent: false, apply_probability_to: :root_spans_and_remote_parent) ⇒ Object

Returns a new sampler. The probability of sampling a trace is equal to that of the specified probability.

Parameters:

  • probability (Numeric)

    The desired probability of sampling. Must be within [0.0, 1.0].

  • ignore_parent (optional Boolean) (defaults to: false)

    Whether to ignore parent sampling. Defaults to not ignore parent sampling.

  • apply_probability_to (optional Symbol) (defaults to: :root_spans_and_remote_parent)

    Whether to apply probability sampling to root spans, root spans and remote parents, or all spans. Allowed values include :root_spans, :root_spans_and_remote_parent, and :all_spans. Defaults to :root_spans_and_remote_parent.

Raises:

  • (ArgumentError)

    if probability is out of range

  • (ArgumentError)

    if apply_probability_to is not one of the allowed symbols



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/opentelemetry/sdk/trace/samplers.rb', line 76

def self.probability(probability,
                     ignore_parent: false,
                     apply_probability_to: :root_spans_and_remote_parent)
  raise ArgumentError, 'probability must be in range [0.0, 1.0]' unless (0.0..1.0).include?(probability)
  raise ArgumentError, 'apply_probability_to' unless APPLY_PROBABILITY_TO_SYMBOLS.include?(apply_probability_to)

  ProbabilitySampler.new(probability,
                         ignore_parent: ignore_parent,
                         apply_to_remote_parent: apply_probability_to != :root_spans,
                         apply_to_all_spans: apply_probability_to == :all_spans)
end