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_based.rb,
lib/opentelemetry/sdk/trace/samplers/constant_sampler.rb,
lib/opentelemetry/sdk/trace/samplers/trace_id_ratio_based.rb

Overview

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

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, ParentBased, Result, TraceIdRatioBased

Constant Summary collapse

ALWAYS_ON =
ConstantSampler.new(decision: Decision::RECORD_AND_SAMPLE, description: 'AlwaysOnSampler')
ALWAYS_OFF =

Returns a Result with Decision::DROP.

ConstantSampler.new(decision: Decision::DROP, description: 'AlwaysOffSampler')

Class Method Summary collapse

Class Method Details

.parent_based(root:, remote_parent_sampled: ALWAYS_ON, remote_parent_not_sampled: ALWAYS_OFF, local_parent_sampled: ALWAYS_ON, local_parent_not_sampled: ALWAYS_OFF) ⇒ Object

Returns a new sampler. It delegates to samplers according to the following rules:

Parent parent.remote? parent.trace_flags.sampled? Invoke sampler
absent n/a n/a root
present true true remote_parent_sampled
present true false remote_parent_not_sampled
present false true local_parent_sampled
present false false local_parent_not_sampled

Parameters:

  • root (Sampler)

    The sampler to which the sampling decision is delegated for spans with no parent (root spans).

  • remote_parent_sampled (optional Sampler) (defaults to: ALWAYS_ON)

    The sampler to which the sampling decision is delegated for remote parent sampled spans. Defaults to ALWAYS_ON.

  • remote_parent_not_sampled (optional Sampler) (defaults to: ALWAYS_OFF)

    The sampler to which the sampling decision is delegated for remote parent not sampled spans. Defaults to ALWAYS_OFF.

  • local_parent_sampled (optional Sampler) (defaults to: ALWAYS_ON)

    The sampler to which the sampling decision is delegated for local parent sampled spans. Defaults to ALWAYS_ON.

  • local_parent_not_sampled (optional Sampler) (defaults to: ALWAYS_OFF)

    The sampler to which the sampling decision is delegated for local parent not sampld spans. Defaults to ALWAYS_OFF.



67
68
69
70
71
72
73
74
75
# File 'lib/opentelemetry/sdk/trace/samplers.rb', line 67

def self.parent_based(
  root:,
  remote_parent_sampled: ALWAYS_ON,
  remote_parent_not_sampled: ALWAYS_OFF,
  local_parent_sampled: ALWAYS_ON,
  local_parent_not_sampled: ALWAYS_OFF
)
  ParentBased.new(root, remote_parent_sampled, remote_parent_not_sampled, local_parent_sampled, local_parent_not_sampled)
end

.trace_id_ratio_based(ratio) ⇒ Object

Returns a new sampler. The ratio describes the proportion of the trace ID space that is sampled.

Parameters:

  • ratio (Numeric)

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

Raises:

  • (ArgumentError)

    if ratio is out of range



83
84
85
86
87
# File 'lib/opentelemetry/sdk/trace/samplers.rb', line 83

def self.trace_id_ratio_based(ratio)
  raise ArgumentError, 'ratio must be in range [0.0, 1.0]' unless (0.0..1.0).include?(ratio)

  TraceIdRatioBased.new(ratio)
end