Class: OneApm::Support::TracedMethodStack

Inherits:
Object
  • Object
show all
Defined in:
lib/one_apm/support/traced_method_stack.rb

Overview

TracedMethodStack is responsible for tracking the push and pop of methods that we are tracing, notifying the transaction sampler, and calculating exclusive time when a method is complete. This is allowed whether a transaction is in progress not.

Instance Method Summary collapse

Constructor Details

#initializeTracedMethodStack

Returns a new instance of TracedMethodStack.



25
26
27
# File 'lib/one_apm/support/traced_method_stack.rb', line 25

def initialize
  @stack = []
end

Instance Method Details

#clearObject



93
94
95
# File 'lib/one_apm/support/traced_method_stack.rb', line 93

def clear
  @stack.clear
end

#empty?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/one_apm/support/traced_method_stack.rb', line 97

def empty?
  @stack.empty?
end

#fetch_matching_frame(expected_frame) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/one_apm/support/traced_method_stack.rb', line 60

def fetch_matching_frame(expected_frame)
  while frame = @stack.pop
    if frame == expected_frame
      return frame
    else
      OneApm::Manager.logger.info("Unexpected frame in traced method stack: #{frame.inspect} expected to be #{expected_frame.inspect}")
      OneApm::Manager.logger.debug do
        ["Backtrace for unexpected frame: ", caller.join("\n")]
      end
    end
  end

  raise "Frame not found in blame stack: #{expected_frame.inspect}"
end

#note_children_time(frame, time, deduct_call_time_from_parent) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/one_apm/support/traced_method_stack.rb', line 75

def note_children_time(frame, time, deduct_call_time_from_parent)
  if !@stack.empty?
    if deduct_call_time_from_parent
      @stack.last.children_time += (time - frame.start_time)
    else
      @stack.last.children_time += frame.children_time
    end
  end
end

#pop_frame(state, expected_frame, name, time, deduct_call_time_from_parent = true) ⇒ Object

Pops a frame off the transaction stack - this updates the transaction sampler that we’ve finished execution of a traced method.

expected_frame should be TracedMethodFrame from the corresponding push_frame call.

name will be applied to the generated transaction trace segment.



50
51
52
53
54
55
56
57
58
# File 'lib/one_apm/support/traced_method_stack.rb', line 50

def pop_frame(state, expected_frame, name, time, deduct_call_time_from_parent=true)
  frame = fetch_matching_frame(expected_frame)

  note_children_time(frame, time, deduct_call_time_from_parent)

  transaction_sampler.notice_pop_frame(state, name, time) if sampler_enabled?
  frame.name = name
  frame
end

#push_frame(state, tag, time = Time.now.to_f) ⇒ Object

Pushes a frame onto the transaction stack - this generates a TransactionSample::Segment at the end of transaction execution.

The generated segment won’t be named until pop_frame is called.

tag should be a Symbol, and is only for debugging purposes to identify this frame if the stack gets corrupted.



36
37
38
39
40
41
# File 'lib/one_apm/support/traced_method_stack.rb', line 36

def push_frame(state, tag, time = Time.now.to_f)
  transaction_sampler.notice_push_frame(state, time) if sampler_enabled?
  frame = TracedMethodFrame.new(tag, time)
  @stack.push frame
  frame
end

#sampler_enabled?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/one_apm/support/traced_method_stack.rb', line 85

def sampler_enabled?
  OneApm::Manager.config[:'transaction_tracer.enabled']
end

#transaction_samplerObject



89
90
91
# File 'lib/one_apm/support/traced_method_stack.rb', line 89

def transaction_sampler
  OneApm::Manager.agent.transaction_sampler
end