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)


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

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.



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

def default_key
  @default_key
end

Instance Method Details

#default_samplerObject



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

def default_sampler
  @samplers[default_key]
end

#delete(key) ⇒ Object



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

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

#delete_if(&block) ⇒ Object



76
77
78
79
80
# File 'lib/datadog/tracing/sampling/rate_by_key_sampler.rb', line 76

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

#lengthObject



82
83
84
# File 'lib/datadog/tracing/sampling/rate_by_key_sampler.rb', line 82

def length
  @samplers.length
end

#resolve(trace) ⇒ Object



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

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

#sample!(trace) ⇒ Object



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

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

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

#sample?(trace) ⇒ Boolean

Returns:

  • (Boolean)


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

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

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

#sample_rate(trace) ⇒ Object



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

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



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

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

#update_all(rate_by_key, decision: nil) ⇒ Object



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

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