Class: OneApm::Transaction::TransactionSampleBuffer
- Inherits:
-
Object
- Object
- OneApm::Transaction::TransactionSampleBuffer
- Defined in:
- lib/one_apm/transaction/sample_buffer/transaction_sample_buffer.rb
Direct Known Subclasses
DeveloperModeSampleBuffer, ForcePersistSampleBuffer, SlowestSampleBuffer, SyntheticsSampleBuffer, XraySampleBuffer
Constant Summary collapse
- OA_SINGLE_BUFFER_MAX =
20
- OA_NO_SAMPLES =
[].freeze
Instance Attribute Summary collapse
-
#samples ⇒ Object
readonly
Returns the value of attribute samples.
Instance Method Summary collapse
- #allow_sample?(sample) ⇒ Boolean
-
#capacity ⇒ Object
Capacity is the desired number of samples a buffer will hold.
- #enabled? ⇒ Boolean
- #full? ⇒ Boolean
- #harvest_samples ⇒ Object
-
#initialize ⇒ TransactionSampleBuffer
constructor
A new instance of TransactionSampleBuffer.
-
#max_capacity ⇒ Object
Apply hard upper limit to the capacity to prevent users from consuming too much memory buffering TT’s.
- #reset! ⇒ Object
- #store(sample) ⇒ Object
- #store_previous(previous_samples) ⇒ Object
-
#truncate_samples ⇒ Object
Our default truncation strategy is to keep max_capacity worth of the longest samples.
- #truncate_samples_if_needed ⇒ Object
-
#visit_segment ⇒ Object
When pushing a scope different sample buffers potentially want to know about what’s happening to annotate the incoming segments.
Constructor Details
#initialize ⇒ TransactionSampleBuffer
Returns a new instance of TransactionSampleBuffer.
11 12 13 |
# File 'lib/one_apm/transaction/sample_buffer/transaction_sample_buffer.rb', line 11 def initialize @samples = [] end |
Instance Attribute Details
#samples ⇒ Object (readonly)
Returns the value of attribute samples.
6 7 8 |
# File 'lib/one_apm/transaction/sample_buffer/transaction_sample_buffer.rb', line 6 def samples @samples end |
Instance Method Details
#allow_sample?(sample) ⇒ Boolean
29 30 31 |
# File 'lib/one_apm/transaction/sample_buffer/transaction_sample_buffer.rb', line 29 def allow_sample?(sample) true end |
#capacity ⇒ Object
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
61 62 63 |
# File 'lib/one_apm/transaction/sample_buffer/transaction_sample_buffer.rb', line 61 def capacity raise NotImplementedError.new("TransactionSampleBuffer subclasses must provide a capacity override") end |
#enabled? ⇒ Boolean
15 16 17 |
# File 'lib/one_apm/transaction/sample_buffer/transaction_sample_buffer.rb', line 15 def enabled? true end |
#full? ⇒ Boolean
53 54 55 |
# File 'lib/one_apm/transaction/sample_buffer/transaction_sample_buffer.rb', line 53 def full? @samples.length >= max_capacity end |
#harvest_samples ⇒ Object
23 24 25 26 27 |
# File 'lib/one_apm/transaction/sample_buffer/transaction_sample_buffer.rb', line 23 def harvest_samples @samples ensure reset! end |
#max_capacity ⇒ Object
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)
70 71 72 |
# File 'lib/one_apm/transaction/sample_buffer/transaction_sample_buffer.rb', line 70 def max_capacity capacity > OA_SINGLE_BUFFER_MAX ? OA_SINGLE_BUFFER_MAX : capacity end |
#reset! ⇒ Object
19 20 21 |
# File 'lib/one_apm/transaction/sample_buffer/transaction_sample_buffer.rb', line 19 def reset! @samples = [] end |
#store(sample) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/one_apm/transaction/sample_buffer/transaction_sample_buffer.rb', line 33 def store(sample) return unless enabled? if allow_sample?(sample) add_sample(sample) truncate_samples_if_needed end end |
#store_previous(previous_samples) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/one_apm/transaction/sample_buffer/transaction_sample_buffer.rb', line 41 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 ⇒ Object
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)
80 81 82 83 |
# File 'lib/one_apm/transaction/sample_buffer/transaction_sample_buffer.rb', line 80 def truncate_samples @samples.sort!{|a,b| a.duration <=> b.duration} @samples.slice!(0..-(max_capacity + 1)) end |
#truncate_samples_if_needed ⇒ Object
49 50 51 |
# File 'lib/one_apm/transaction/sample_buffer/transaction_sample_buffer.rb', line 49 def truncate_samples_if_needed truncate_samples if full? end |
#visit_segment ⇒ Object
When pushing a scope different sample buffers potentially want to know about what’s happening to annotate the incoming segments
87 88 89 |
# File 'lib/one_apm/transaction/sample_buffer/transaction_sample_buffer.rb', line 87 def visit_segment(*) # no-op end |