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(result: RECORD_AND_SAMPLE, description: 'AlwaysOnSampler')
ALWAYS_OFF =

Returns a Result with Decision::DROP.

ConstantSampler.new(result: 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.



74
75
76
77
78
79
80
81
82
# File 'lib/opentelemetry/sdk/trace/samplers.rb', line 74

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



90
91
92
93
94
# File 'lib/opentelemetry/sdk/trace/samplers.rb', line 90

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