Class: BuilderApm::Methods::Instrumenter

Inherits:
Object
  • Object
show all
Defined in:
lib/builder_apm/methods/instrumenter.rb

Instance Method Summary collapse

Constructor Details

#initialize(root_path: Rails.root.to_s) ⇒ Instrumenter

Returns a new instance of Instrumenter.



4
5
6
7
8
# File 'lib/builder_apm/methods/instrumenter.rb', line 4

def initialize(root_path: Rails.root.to_s)
  @this_gem_path = File.expand_path("../../..", __dir__)
  @root_path = root_path
  @call_times = {}
end

Instance Method Details

#not_controller_endpoint(tp) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/builder_apm/methods/instrumenter.rb', line 36

def not_controller_endpoint(tp)
  start_controller = Thread.current[:stack]&.first || nil

  return false unless start_controller

  "#{tp.defined_class}##{tp.method_id}" != start_controller[:method]
end

#process_trace_point(tp) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/builder_apm/methods/instrumenter.rb', line 52

def process_trace_point(tp)
  if tp.event == :call || tp.event == :b_call || tp.event == :c_call
    process_call_event(tp)
  else
    process_return_event(tp)
  end
end

#setup_traceObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/builder_apm/methods/instrumenter.rb', line 19

def setup_trace
  me = self
  TracePoint.new(:call, :return, :end, :raise) do |tp|
    starttime = Time.now.to_f * 1000
    me.process_trace_point(tp) if me.valid_trace_point?(tp)
    duration = (Time.now.to_f * 1000) - starttime
    
    Thread.current["method_tracing"] = (Thread.current["method_tracing"] ||= 0) + duration
  end
end

#startObject



10
11
12
13
# File 'lib/builder_apm/methods/instrumenter.rb', line 10

def start
  @trace = setup_trace
  @trace.enable
end

#stopObject



15
16
17
# File 'lib/builder_apm/methods/instrumenter.rb', line 15

def stop
  @trace.disable unless @trace.nil?
end

#valid_gem_path(tp) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/builder_apm/methods/instrumenter.rb', line 44

def valid_gem_path(tp)
  not_this_gem = !tp.path.start_with?(@this_gem_path)
  is_a_tracked_gem = tp.path.split(File::SEPARATOR).any? { |folder| folder.start_with?(*gems_to_track) }
  is_a_rails_app_file = tp.path.start_with?(@root_path)

  not_this_gem && (is_a_tracked_gem || is_a_rails_app_file)
end

#valid_trace_point?(tp) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
# File 'lib/builder_apm/methods/instrumenter.rb', line 30

def valid_trace_point?(tp)
  return !Thread.current[:request_id].nil? && tp.path.start_with?(@root_path)

  # !Thread.current[:request_id].nil? && not_controller_endpoint(tp) && valid_gem_path(tp)
end