Class: Datadog::RateByKeySampler

Inherits:
Sampler
  • Object
show all
Defined in:
lib/ddtrace/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, &block) ⇒ RateByKeySampler

Returns a new instance of RateByKeySampler.

Raises:

  • (ArgumentError)


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

def initialize(default_key, default_rate = 1.0, &block)
  raise ArgumentError, 'No resolver given!' unless block_given?

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

  set_rate(default_key, default_rate)
end

Instance Attribute Details

#default_keyObject (readonly)

Returns the value of attribute default_key.



80
81
82
# File 'lib/ddtrace/sampler.rb', line 80

def default_key
  @default_key
end

Instance Method Details

#default_samplerObject



98
99
100
# File 'lib/ddtrace/sampler.rb', line 98

def default_sampler
  @samplers[default_key]
end

#delete(key) ⇒ Object



138
139
140
141
142
# File 'lib/ddtrace/sampler.rb', line 138

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

#delete_if(&block) ⇒ Object



144
145
146
147
148
# File 'lib/ddtrace/sampler.rb', line 144

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

#lengthObject



150
151
152
# File 'lib/ddtrace/sampler.rb', line 150

def length
  @samplers.length
end

#resolve(span) ⇒ Object



94
95
96
# File 'lib/ddtrace/sampler.rb', line 94

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

#sample!(span) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/ddtrace/sampler.rb', line 110

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

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

#sample?(span) ⇒ Boolean

Returns:

  • (Boolean)


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

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

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

#sample_rate(span) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/ddtrace/sampler.rb', line 118

def sample_rate(span)
  key = resolve(span)

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

#update(key, rate) ⇒ Object



126
127
128
129
130
# File 'lib/ddtrace/sampler.rb', line 126

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

#update_all(rate_by_key) ⇒ Object



132
133
134
135
136
# File 'lib/ddtrace/sampler.rb', line 132

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