Class: Datadog::RateByServiceSampler

Inherits:
Sampler
  • Object
show all
Defined in:
lib/ddtrace/sampler.rb

Overview

RateByServiceSampler samples different services at different rates

Constant Summary collapse

DEFAULT_KEY =
'service:,env:'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(rate = 1.0, opts = {}) ⇒ RateByServiceSampler

Returns a new instance of RateByServiceSampler.



71
72
73
74
75
76
# File 'lib/ddtrace/sampler.rb', line 71

def initialize(rate = 1.0, opts = {})
  @env = opts.fetch(:env, Datadog.tracer.tags[:env])
  @mutex = Mutex.new
  @fallback = RateSampler.new(rate)
  @sampler = { DEFAULT_KEY => @fallback }
end

Instance Method Details

#sample!(span) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/ddtrace/sampler.rb', line 86

def sample!(span)
  key = key_for(span)

  @mutex.synchronize do
    @sampler.fetch(key, @fallback).sample!(span)
  end
end

#sample?(span) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
84
# File 'lib/ddtrace/sampler.rb', line 78

def sample?(span)
  key = key_for(span)

  @mutex.synchronize do
    @sampler.fetch(key, @fallback).sample?(span)
  end
end

#sample_rate(span) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/ddtrace/sampler.rb', line 94

def sample_rate(span)
  key = key_for(span)

  @mutex.synchronize do
    @sampler.fetch(key, @fallback).sample_rate
  end
end

#update(rate_by_service) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/ddtrace/sampler.rb', line 102

def update(rate_by_service)
  @mutex.synchronize do
    @sampler.delete_if { |key, _| key != DEFAULT_KEY && !rate_by_service.key?(key) }

    rate_by_service.each do |key, rate|
      @sampler[key] ||= RateSampler.new(rate)
      @sampler[key].sample_rate = rate
    end
  end
end