Class: NewRelic::Agent::Transaction::TransactionSampleBuffer

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

Direct Known Subclasses

SlowestSampleBuffer, SyntheticsSampleBuffer

Constant Summary collapse

SINGLE_BUFFER_MAX =
20

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTransactionSampleBuffer

Returns a new instance of TransactionSampleBuffer.



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

def initialize
  @samples = []
end

Instance Attribute Details

#samplesObject (readonly)



9
10
11
# File 'lib/new_relic/agent/transaction/transaction_sample_buffer.rb', line 9

def samples
  @samples
end

Instance Method Details

#allow_sample?(sample) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/new_relic/agent/transaction/transaction_sample_buffer.rb', line 31

def allow_sample?(sample)
  true
end

#capacityObject

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

Raises:

  • (NotImplementedError)


65
66
67
# 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?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/new_relic/agent/transaction/transaction_sample_buffer.rb', line 17

def enabled?
  true
end

#full?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/new_relic/agent/transaction/transaction_sample_buffer.rb', line 57

def full?
  @samples.length >= max_capacity
end

#harvest_samplesObject



25
26
27
28
29
# File 'lib/new_relic/agent/transaction/transaction_sample_buffer.rb', line 25

def harvest_samples
  @samples
ensure
  reset!
end

#max_capacityObject

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)



74
75
76
# File 'lib/new_relic/agent/transaction/transaction_sample_buffer.rb', line 74

def max_capacity
  [capacity, SINGLE_BUFFER_MAX].min
end

#reset!Object



21
22
23
# File 'lib/new_relic/agent/transaction/transaction_sample_buffer.rb', line 21

def reset!
  @samples = []
end

#store(sample) ⇒ Object



35
36
37
38
39
40
41
42
# 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) ⇒ Object



44
45
46
47
48
49
50
51
# 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_samplesObject

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)



84
85
86
87
# 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_neededObject



53
54
55
# File 'lib/new_relic/agent/transaction/transaction_sample_buffer.rb', line 53

def truncate_samples_if_needed
  truncate_samples if full?
end

#visit_nodeObject

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



91
92
93
# File 'lib/new_relic/agent/transaction/transaction_sample_buffer.rb', line 91

def visit_node(*)
  # no-op
end