Sha256: ffd9a8f244af05e42a8a3b681586e1404966194c4892f43977fe7976ddc43a0d

Contents?: true

Size: 1.68 KB

Versions: 7

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

require_relative 'sampler'
require_relative '../utils'

module Datadog
  module Tracing
    module Sampling
      # {Datadog::Tracing::Sampling::RateSampler} is based on a sample rate.
      class RateSampler < Sampler
        KNUTH_FACTOR = 1111111111111111111

        # Initialize a {Datadog::Tracing::Sampling::RateSampler}.
        # This sampler keeps a random subset of the traces. Its main purpose is to
        # reduce the instrumentation footprint.
        #
        # @param sample_rate [Numeric] the sample rate between 0.0 and 1.0, inclusive.
        #   0.0 means that no trace will be sampled; 1.0 means that all traces will be  sampled.
        def initialize(sample_rate = 1.0, decision: nil)
          super()

          unless sample_rate >= 0.0 && sample_rate <= 1.0
            Datadog.logger.warn('sample rate is not between 0 and 1, falling back to 1')
            sample_rate = 1.0
          end

          self.sample_rate = sample_rate

          @decision = decision
        end

        def sample_rate(*_)
          @sample_rate
        end

        def sample_rate=(sample_rate)
          @sample_rate = sample_rate
          @sampling_id_threshold = sample_rate * Tracing::Utils::EXTERNAL_MAX_ID
        end

        def sample?(trace)
          ((trace.id * KNUTH_FACTOR) % Tracing::Utils::EXTERNAL_MAX_ID) <= @sampling_id_threshold
        end

        def sample!(trace)
          sampled = sample?(trace)

          return false unless sampled

          trace.sample_rate = @sample_rate
          trace.set_tag(Tracing::Metadata::Ext::Distributed::TAG_DECISION_MAKER, @decision) if @decision

          true
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
datadog-2.9.0 lib/datadog/tracing/sampling/rate_sampler.rb
datadog-2.8.0 lib/datadog/tracing/sampling/rate_sampler.rb
datadog-2.7.1 lib/datadog/tracing/sampling/rate_sampler.rb
datadog-2.7.0 lib/datadog/tracing/sampling/rate_sampler.rb
datadog-2.6.0 lib/datadog/tracing/sampling/rate_sampler.rb
datadog-2.5.0 lib/datadog/tracing/sampling/rate_sampler.rb
datadog-2.4.0 lib/datadog/tracing/sampling/rate_sampler.rb