Class: NewRelic::Agent::SampledBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/new_relic/agent/sampled_buffer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(capacity) ⇒ SampledBuffer

Returns a new instance of SampledBuffer.



15
16
17
18
19
20
21
# File 'lib/new_relic/agent/sampled_buffer.rb', line 15

def initialize(capacity)
  @items = []
  @capacity = capacity
  @captured_lifetime = 0
  @seen = 0
  @seen_lifetime = 0
end

Instance Attribute Details

#capacityObject

Returns the value of attribute capacity.



13
14
15
# File 'lib/new_relic/agent/sampled_buffer.rb', line 13

def capacity
  @capacity
end

#captured_lifetimeObject (readonly)

Returns the value of attribute captured_lifetime.



13
14
15
# File 'lib/new_relic/agent/sampled_buffer.rb', line 13

def captured_lifetime
  @captured_lifetime
end

#seenObject (readonly)

Returns the value of attribute seen.



13
14
15
# File 'lib/new_relic/agent/sampled_buffer.rb', line 13

def seen
  @seen
end

#seen_lifetimeObject (readonly)

Returns the value of attribute seen_lifetime.



13
14
15
# File 'lib/new_relic/agent/sampled_buffer.rb', line 13

def seen_lifetime
  @seen_lifetime
end

Instance Method Details

#<<(x) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/new_relic/agent/sampled_buffer.rb', line 39

def <<(x)
  @seen += 1
  if @items.size < @capacity
    @items << x
  else
    m = rand(@seen) # [0, @seen)
    if m < @capacity
      @items[m] = x
    else
      # discard current sample
    end
  end
  return self
end

#append(x) ⇒ Object

Like ‘<<’, but returns the value of full?



35
36
37
# File 'lib/new_relic/agent/sampled_buffer.rb', line 35

def append(x)
  (self << x).full?
end

#full?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/new_relic/agent/sampled_buffer.rb', line 30

def full?
  @items.size >= @capacity
end

#resetObject



23
24
25
26
27
28
# File 'lib/new_relic/agent/sampled_buffer.rb', line 23

def reset
  @captured_lifetime += @items.size
  @seen_lifetime += @seen
  @items = []
  @seen = 0
end

#sample_rateObject



69
70
71
# File 'lib/new_relic/agent/sampled_buffer.rb', line 69

def sample_rate
  @seen > 0 ? (size.to_f / @seen) : 0.0
end

#sample_rate_lifetimeObject



73
74
75
# File 'lib/new_relic/agent/sampled_buffer.rb', line 73

def sample_rate_lifetime
  @captured_lifetime > 0 ? (@captured_lifetime.to_f / @seen_lifetime) : 0.0
end

#sizeObject



54
55
56
# File 'lib/new_relic/agent/sampled_buffer.rb', line 54

def size
  @items.size
end

#to_aObject



58
59
60
# File 'lib/new_relic/agent/sampled_buffer.rb', line 58

def to_a
  @items.dup
end