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/probability_sampler.rb

Overview

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

Custom samplers can be provided by SDK users. The required interface is a callable with the signature:

(trace_id:, span_id:, parent_context:, links:, name:, kind:, attributes:) -> Result

Where:

Defined Under Namespace

Modules: Decision Classes: ProbabilitySampler, Result

Constant Summary collapse

ALWAYS_ON =
->(trace_id:, span_id:, parent_context:, links:, name:, kind:, attributes:) { RECORD_AND_SAMPLED }
ALWAYS_OFF =

Returns a Result with Decision::NOT_RECORD.

->(trace_id:, span_id:, parent_context:, links:, name:, kind:, attributes:) { NOT_RECORD }
ALWAYS_PARENT =

Returns a Result with Decision::RECORD_AND_SAMPLED if the parent context is sampled or Decision::NOT_RECORD otherwise, or if there is no parent context. rubocop:disable Style/Lambda

->(trace_id:, span_id:, parent_context:, links:, name:, kind:, attributes:) do
  if parent_context&.trace_flags&.sampled?
    RECORD_AND_SAMPLED
  else
    NOT_RECORD
  end
end

Class Method Summary collapse

Class Method Details

.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.

Raises:

  • (ArgumentError)

    if probability is out of range

  • (ArgumentError)

    if apply_probability_to is not one of the allowed symbols



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/opentelemetry/sdk/trace/samplers.rb', line 82

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