File: probabilistic.rb

package info (click to toggle)
ruby-jaeger-client 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 624 kB
  • sloc: ruby: 3,381; makefile: 6; sh: 4
file content (40 lines) | stat: -rw-r--r-- 896 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# frozen_string_literal: true

module Jaeger
  module Samplers
    # Probabilistic sampler
    #
    # Sample a portion of traces using trace_id as the random decision
    class Probabilistic
      attr_reader :rate

      def initialize(rate: 0.001)
        update(rate: rate)
      end

      def update(rate:)
        if rate < 0.0 || rate > 1.0
          raise "Sampling rate must be between 0.0 and 1.0, got #{rate.inspect}"
        end

        new_boundary = TraceId::TRACE_ID_UPPER_BOUND * rate
        return false if @boundary == new_boundary

        @rate = rate
        @boundary = TraceId::TRACE_ID_UPPER_BOUND * rate
        @tags = {
          'sampler.type' => 'probabilistic',
          'sampler.param' => rate
        }

        true
      end

      def sample(opts)
        trace_id = opts.fetch(:trace_id)

        [@boundary >= trace_id, @tags]
      end
    end
  end
end