Class: Jaeger::Samplers::RateLimiting

Inherits:
Object
  • Object
show all
Defined in:
lib/jaeger/samplers/rate_limiting.rb

Overview

Samples at most max_traces_per_second. The distribution of sampled traces follows burstiness of the service, i.e. a service with uniformly distributed requests will have those requests sampled uniformly as well, but if requests are bursty, especially sub-second, then a number of sequential requests can be sampled each second.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_traces_per_second: 10) ⇒ RateLimiting

Returns a new instance of RateLimiting.



13
14
15
# File 'lib/jaeger/samplers/rate_limiting.rb', line 13

def initialize(max_traces_per_second: 10)
  update(max_traces_per_second: max_traces_per_second)
end

Instance Attribute Details

#max_traces_per_secondObject (readonly)

Returns the value of attribute max_traces_per_second.



11
12
13
# File 'lib/jaeger/samplers/rate_limiting.rb', line 11

def max_traces_per_second
  @max_traces_per_second
end

#tagsObject (readonly)

Returns the value of attribute tags.



11
12
13
# File 'lib/jaeger/samplers/rate_limiting.rb', line 11

def tags
  @tags
end

Instance Method Details

#sampleObject



46
47
48
# File 'lib/jaeger/samplers/rate_limiting.rb', line 46

def sample(*)
  [@rate_limiter.check_credit(1.0), @tags]
end

#update(max_traces_per_second:) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/jaeger/samplers/rate_limiting.rb', line 17

def update(max_traces_per_second:)
  if max_traces_per_second < 0.0
    raise "max_traces_per_second must not be negative, got #{max_traces_per_second}"
  end

  return false if max_traces_per_second == @max_traces_per_second

  @tags = {
    'sampler.type' => 'ratelimiting',
    'sampler.param' => max_traces_per_second
  }
  @max_traces_per_second = max_traces_per_second
  max_balance = [max_traces_per_second, 1.0].max

  if @rate_limiter
    @rate_limiter.update(
      credits_per_second: max_traces_per_second,
      max_balance: max_balance
    )
  else
    @rate_limiter = RateLimiter.new(
      credits_per_second: max_traces_per_second,
      max_balance: [max_traces_per_second, 1.0].max
    )
  end

  true
end