Class: NewRelic::Agent::PrioritySampledBuffer

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

Direct Known Subclasses

TimestampSampledBuffer

Constant Summary collapse

PRIORITY_KEY =
'priority'.freeze

Instance Attribute Summary collapse

Attributes inherited from EventBuffer

#capacity

Instance Method Summary collapse

Methods inherited from EventBuffer

#<<, #full?, #note_dropped, #num_dropped, #num_seen, #reset!, #sample_rate, #size

Constructor Details

#initialize(capacity) ⇒ PrioritySampledBuffer

Returns a new instance of PrioritySampledBuffer.



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

def initialize(capacity)
  super
  @captured_lifetime = 0
  @seen_lifetime = 0
end

Instance Attribute Details

#captured_lifetimeObject (readonly)

Returns the value of attribute captured_lifetime.



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

def captured_lifetime
  @captured_lifetime
end

#seen_lifetimeObject (readonly)

Returns the value of attribute seen_lifetime.



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

def seen_lifetime
  @seen_lifetime
end

Instance Method Details

#append(priority: nil, event: nil, &blk) ⇒ Object Also known as: append_event

expects priority and a block, or an event as a hash with a ‘priority` key.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/new_relic/agent/priority_sampled_buffer.rb', line 28

def append(priority: nil, event: nil, &blk)
  increment_seen

  return if @capacity == 0

  if full?
    priority ||= priority_for(event)
    if priority_for(@items[0]) < priority
      heapify_items_array
      incoming = event || yield
      @items[0] = incoming
      @items.fix(0)
      incoming
    end
  else
    @items << (event || yield)
    @captured_lifetime += 1
    @items[-1]
  end
end

#capacity=(new_capacity) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/new_relic/agent/priority_sampled_buffer.rb', line 51

def capacity=(new_capacity)
  @capacity = new_capacity
  old_items = @items.to_a
  old_seen = @seen
  reset!
  old_items.each { |i| append(event: i) }
  @seen = old_seen
end

#decrement_lifetime_counts_by(n) ⇒ Object



64
65
66
67
# File 'lib/new_relic/agent/priority_sampled_buffer.rb', line 64

def decrement_lifetime_counts_by(n)
  @captured_lifetime -= n
  @seen_lifetime -= n
end

#heapify_items_arrayObject



21
22
23
24
25
# File 'lib/new_relic/agent/priority_sampled_buffer.rb', line 21

def heapify_items_array
  if @items.is_a?(Array)
    @items = Heap.new(@items) { |x| priority_for(x) }
  end
end

#metadataObject



73
74
75
76
77
78
# File 'lib/new_relic/agent/priority_sampled_buffer.rb', line 73

def 
  super.merge!(
    :captured_lifetime => @captured_lifetime,
    :seen_lifetime => @seen_lifetime
  )
end

#sample_rate_lifetimeObject



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

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

#to_aObject



60
61
62
# File 'lib/new_relic/agent/priority_sampled_buffer.rb', line 60

def to_a
  @items.to_a.dup
end