Class: Datadog::Tracing::Sampling::RateByKeySampler

Inherits:
Sampler
  • Object
show all
Defined in:
lib/datadog/tracing/sampling/rate_by_key_sampler.rb

Overview

Samples at different rates by key.

Direct Known Subclasses

RateByServiceSampler

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default_key, default_rate = 1.0, decision: nil, &block) ⇒ RateByKeySampler

Returns a new instance of RateByKeySampler.

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/datadog/tracing/sampling/rate_by_key_sampler.rb', line 14

def initialize(default_key, default_rate = 1.0, decision: nil, &block)
  super()

  raise ArgumentError, 'No resolver given!' unless block

  @default_key = default_key
  @resolver = block
  @mutex = Mutex.new
  @samplers = {}

  set_rate(default_key, default_rate, decision)
end

Instance Attribute Details

#default_keyObject (readonly)

Returns the value of attribute default_key.



11
12
13
# File 'lib/datadog/tracing/sampling/rate_by_key_sampler.rb', line 11

def default_key
  @default_key
end

Instance Method Details

#default_samplerObject



31
32
33
# File 'lib/datadog/tracing/sampling/rate_by_key_sampler.rb', line 31

def default_sampler
  @samplers[default_key]
end

#delete(key) ⇒ Object



63
64
65
66
67
# File 'lib/datadog/tracing/sampling/rate_by_key_sampler.rb', line 63

def delete(key)
  @mutex.synchronize do
    @samplers.delete(key)
  end
end

#delete_if(&block) ⇒ Object



69
70
71
72
73
# File 'lib/datadog/tracing/sampling/rate_by_key_sampler.rb', line 69

def delete_if(&block)
  @mutex.synchronize do
    @samplers.delete_if(&block)
  end
end

#lengthObject



75
76
77
# File 'lib/datadog/tracing/sampling/rate_by_key_sampler.rb', line 75

def length
  @samplers.length
end

#resolve(trace) ⇒ Object



27
28
29
# File 'lib/datadog/tracing/sampling/rate_by_key_sampler.rb', line 27

def resolve(trace)
  @resolver.call(trace)
end

#sample!(trace) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/datadog/tracing/sampling/rate_by_key_sampler.rb', line 35

def sample!(trace)
  key = resolve(trace)

  @mutex.synchronize do
    @samplers.fetch(key, default_sampler).sample!(trace)
  end
end

#sample_rate(trace) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/datadog/tracing/sampling/rate_by_key_sampler.rb', line 43

def sample_rate(trace)
  key = resolve(trace)

  @mutex.synchronize do
    @samplers.fetch(key, default_sampler).sample_rate
  end
end

#update(key, rate, decision: nil) ⇒ Object



51
52
53
54
55
# File 'lib/datadog/tracing/sampling/rate_by_key_sampler.rb', line 51

def update(key, rate, decision: nil)
  @mutex.synchronize do
    set_rate(key, rate, decision)
  end
end

#update_all(rate_by_key, decision: nil) ⇒ Object



57
58
59
60
61
# File 'lib/datadog/tracing/sampling/rate_by_key_sampler.rb', line 57

def update_all(rate_by_key, decision: nil)
  @mutex.synchronize do
    rate_by_key.each { |key, rate| set_rate(key, rate, decision) }
  end
end