Class: OpenTelemetry::Exporters::Datadog::DatadogProbabilitySampler

Inherits:
Object
  • Object
show all
Defined in:
lib/opentelemetry/exporters/datadog/datadog_probability_sampler.rb

Overview

Implements sampling based on a probability but records all spans regardless.

Constant Summary collapse

DEFAULT =
new(1.0)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(probability) ⇒ DatadogProbabilitySampler

Returns a new instance of DatadogProbabilitySampler.



19
20
21
22
23
# File 'lib/opentelemetry/exporters/datadog/datadog_probability_sampler.rb', line 19

def initialize(probability)
  @probability = probability
  @id_upper_bound = (probability * (2**64 - 1)).ceil
  @description = format('TraceIdRatioBased{%.6f}', probability)
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



17
18
19
# File 'lib/opentelemetry/exporters/datadog/datadog_probability_sampler.rb', line 17

def description
  @description
end

Class Method Details

.default_with_probability(probability = 1.0) ⇒ Object

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

Parameters:

  • probability (Numeric) (defaults to: 1.0)

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

Raises:

  • (ArgumentError)


47
48
49
50
51
# File 'lib/opentelemetry/exporters/datadog/datadog_probability_sampler.rb', line 47

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

  new(probability)
end

Instance Method Details

#sample?(trace_id) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/opentelemetry/exporters/datadog/datadog_probability_sampler.rb', line 25

def sample?(trace_id)
  @probability == 1.0 || trace_id[8, 8].unpack1('Q>') < @id_upper_bound
end

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

See Samplers.

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
# File 'lib/opentelemetry/exporters/datadog/datadog_probability_sampler.rb', line 32

def should_sample?(trace_id:, parent_context:, links:, name:, kind:, attributes:)
  # Ignored for sampling decision: links, name, kind, attributes.
  tracestate = OpenTelemetry::Trace.current_span(parent_context).context.tracestate
  if sample?(trace_id)
    OpenTelemetry::SDK::Trace::Samplers::Result.new(decision: OpenTelemetry::SDK::Trace::Samplers::Decision::RECORD_AND_SAMPLE, tracestate: tracestate)
  else
    OpenTelemetry::SDK::Trace::Samplers::Result.new(decision: OpenTelemetry::SDK::Trace::Samplers::Decision::RECORD_ONLY, tracestate: tracestate)
  end
end