class NewRelic::Agent::Transaction::TransactionSampleBuffer

Constants

SINGLE_BUFFER_MAX

Attributes

samples[R]

Public Class Methods

new() click to toggle source
# File lib/new_relic/agent/transaction/transaction_sample_buffer.rb, line 13
def initialize
  @samples = []
end

Public Instance Methods

allow_sample?(sample) click to toggle source
# File lib/new_relic/agent/transaction/transaction_sample_buffer.rb, line 31
def allow_sample?(sample)
  true
end
capacity() click to toggle source

Capacity is the desired number of samples a buffer will hold. This can be user dictated via config if a feature wants.

This value will be forcibly capped by the max_capacity

# File lib/new_relic/agent/transaction/transaction_sample_buffer.rb, line 65
def capacity
  raise NotImplementedError.new('TransactionSampleBuffer subclasses must provide a capacity override')
end
enabled?() click to toggle source
# File lib/new_relic/agent/transaction/transaction_sample_buffer.rb, line 17
def enabled?
  true
end
full?() click to toggle source
# File lib/new_relic/agent/transaction/transaction_sample_buffer.rb, line 57
def full?
  @samples.length >= max_capacity
end
harvest_samples() click to toggle source
# File lib/new_relic/agent/transaction/transaction_sample_buffer.rb, line 25
def harvest_samples
  @samples
ensure
  reset!
end
max_capacity() click to toggle source

Apply hard upper limit to the capacity to prevent users from consuming too much memory buffering TT’s.

A typical buffer should NOT override this method (although we do for odd things like dev-mode)

# File lib/new_relic/agent/transaction/transaction_sample_buffer.rb, line 74
def max_capacity
  [capacity, SINGLE_BUFFER_MAX].min
end
reset!() click to toggle source
# File lib/new_relic/agent/transaction/transaction_sample_buffer.rb, line 21
def reset!
  @samples = []
end
store(sample) click to toggle source
# File lib/new_relic/agent/transaction/transaction_sample_buffer.rb, line 35
def store(sample)
  return unless enabled?

  if allow_sample?(sample)
    add_sample(sample)
    truncate_samples_if_needed
  end
end
store_previous(previous_samples) click to toggle source
# File lib/new_relic/agent/transaction/transaction_sample_buffer.rb, line 44
def store_previous(previous_samples)
  return unless enabled?

  previous_samples.each do |sample|
    add_sample(sample) if allow_sample?(sample)
  end
  truncate_samples_if_needed
end
truncate_samples() click to toggle source

Our default truncation strategy is to keep max_capacity worth of the longest samples. Override this method for alternate behavior.

This doesn’t use the more convenient last and sort_by to avoid additional array allocations (and abundant alliteration)

# File lib/new_relic/agent/transaction/transaction_sample_buffer.rb, line 84
def truncate_samples
  @samples.sort! { |a, b| a.duration <=> b.duration }
  @samples.slice!(0..-(max_capacity + 1))
end
truncate_samples_if_needed() click to toggle source
# File lib/new_relic/agent/transaction/transaction_sample_buffer.rb, line 53
def truncate_samples_if_needed
  truncate_samples if full?
end
visit_node(*) click to toggle source

When pushing a scope different sample buffers potentially want to know about what’s happening to annotate the incoming nodes

# File lib/new_relic/agent/transaction/transaction_sample_buffer.rb, line 91
def visit_node(*)
  # no-op
end

Private Instance Methods

add_sample(sample) click to toggle source

If a buffer needs to modify an added sample, override this method. Bounds checking, allowing samples and truncation belongs elsewhere.

# File lib/new_relic/agent/transaction/transaction_sample_buffer.rb, line 99
def add_sample(sample)
  @samples << sample
end