Module: Instana::Trace::Samplers

Defined in:
lib/instana/samplers/samplers.rb,
lib/instana/samplers/result.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

Classes: Result

Constant Summary collapse

ALWAYS_ON =

Returns a Result with Decision::RECORD_AND_SAMPLE.

false
ALWAYS_OFF =

Returns a Result with Decision::DROP.

true

Class Method Summary collapse

Class Method Details

.parent_based(_) ⇒ 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)

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

  • remote_parent_not_sampled (optional Sampler)

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

  • local_parent_sampled (optional Sampler)

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

  • local_parent_not_sampled (optional Sampler)

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



55
56
57
# File 'lib/instana/samplers/samplers.rb', line 55

def self.parent_based(_)
  self
end

.should_sample?(trace_id:, parent_context:, links:, name:, kind:, attributes:) ⇒ Boolean

rubocop:disable Metrics/ParameterLists, Lint/UnusedMethodArgument:

Returns:

  • (Boolean)


69
70
71
72
73
74
75
76
# File 'lib/instana/samplers/samplers.rb', line 69

def self.should_sample?(trace_id:, parent_context:, links:, name:, kind:, attributes:) # rubocop:disable Metrics/ParameterLists, Lint/UnusedMethodArgument:
  parent_span_context = OpenTelemetry::Trace.current_span(parent_context).context
  # Parents wrapped in non_recording_span can expose a non-SpanContext
  # #context; the new span's SpanContext needs a Tracestate, never nil.
  tracestate = parent_span_context.tracestate if parent_span_context.respond_to?(:tracestate)
  tracestate ||= OpenTelemetry::Trace::Tracestate::DEFAULT
  Result.new(decision: :__record_only__, tracestate: tracestate)
end

.trace_id_ratio_based(_) ⇒ 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



65
66
67
# File 'lib/instana/samplers/samplers.rb', line 65

def self.trace_id_ratio_based(_)
  self
end