Class: Callstacking::Rails::Instrument

Inherits:
Object
  • Object
show all
Defined in:
lib/callstacking/rails/instrument.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInstrument

Returns a new instance of Instrument.



10
11
12
13
14
# File 'lib/callstacking/rails/instrument.rb', line 10

def initialize
  @spans = {}
  @span_modules = Set.new
  @settings = Callstacking::Rails::Settings.new
end

Instance Attribute Details

#settingsObject (readonly)

Returns the value of attribute settings.



8
9
10
# File 'lib/callstacking/rails/instrument.rb', line 8

def settings
  @settings
end

#span_modulesObject (readonly)

Returns the value of attribute span_modules.



8
9
10
# File 'lib/callstacking/rails/instrument.rb', line 8

def span_modules
  @span_modules
end

#spansObject

Returns the value of attribute spans.



7
8
9
# File 'lib/callstacking/rails/instrument.rb', line 7

def spans
  @spans
end

Class Method Details

.arguments_for(m, args) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/callstacking/rails/instrument.rb', line 125

def self.arguments_for(m, args)
  param_names = m.parameters&.map(&:last)
  return {} if param_names.nil?

  h = param_names.map.with_index do |param, index|
    next if [:&, :*, :**].include?(param)
    [param, args[index].inspect]
  end.compact.to_h

  filter = ::Rails.application.config.filter_parameters
  f = ActiveSupport::ParameterFilter.new filter
  f.filter h
end

Instance Method Details

#add_span(span) ⇒ Object



139
140
141
# File 'lib/callstacking/rails/instrument.rb', line 139

def add_span(span)
  spans[Thread.current.object_id] ||= span
end

#disable!(modules = span_modules) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/callstacking/rails/instrument.rb', line 102

def disable!(modules = span_modules)
  modules.each do |mod|
    mod.instance_methods.each do |method_name|
      mod.remove_method(method_name)
    end
  end
  
  reset!
end

#enable!(klasses) ⇒ Object



96
97
98
99
100
# File 'lib/callstacking/rails/instrument.rb', line 96

def enable!(klasses)
  Array.wrap(klasses).each do |klass|
    instrument_klass(klass, application_level: true)
  end
end

#instrument_klass(klass, application_level: true) ⇒ Object



120
121
122
123
# File 'lib/callstacking/rails/instrument.rb', line 120

def instrument_klass(klass, application_level: true)
  relevant_methods = all_methods(klass) - filtered
  relevant_methods.each { |method| instrument_method(klass, method, application_level: application_level) }
end

#instrument_method(klass, method_name, application_level: true) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/callstacking/rails/instrument.rb', line 16

def instrument_method(klass, method_name, application_level: true)
  method_path = (klass.instance_method(method_name).source_location.first rescue nil) ||
    (klass.method(method_name).source_location.first rescue nil)

  # method was not defined in Ruby (i.e. native)
  return if method_path.nil?

  # Application level method definitions
  return if application_level && !(method_path =~ /#{::Rails.root.to_s}/)

  return if method_path =~ /initializer/i

  tmp_module = find_or_initialize_module(klass)

  return if tmp_module.nil? ||
              tmp_module.instance_methods.include?(method_name) ||
              tmp_module.singleton_methods.include?(method_name)

  new_method = nil
  if RUBY_VERSION < "2.7.8"
    new_method = tmp_module.define_method(method_name) do |*args, &block|
      settings = tmp_module.instance_variable_get(:@settings)
      return super(*args, &block) if settings.disabled?
      
      method_name = __method__

      path = method(__method__).super_method.source_location&.first || ''
      line_no = method(__method__).super_method.source_location&.last || ''

      method_source = ''
      method_source = method(__method__).super_method.source if settings.analyze_source?

      p, l = caller.find { |c| c.to_s =~ /#{::Rails.root.to_s}/}&.split(':')

      spans = tmp_module.instance_variable_get(:@spans)
      span  = spans[Thread.current.object_id]
      klass = tmp_module.instance_variable_get(:@klass)

      arguments = Callstacking::Rails::Instrument.arguments_for(method(__method__).super_method, args)

      span.call_entry(klass, method_name, arguments, p || path, l || line_no, method_source)
      return_val = super(*args, &block)
      span.call_return(klass, method_name, p || path, l || line_no, return_val, method_source)

      return_val
    end
    new_method.ruby2_keywords if new_method.respond_to?(:ruby2_keywords)
  else
    new_method = tmp_module.define_method(method_name) do |*args, **kwargs, &block|
      settings = tmp_module.instance_variable_get(:@settings)
      return super(*args, **kwargs, &block) if settings.disabled?

      method_name = __method__

      path = method(__method__).super_method.source_location&.first || ''
      line_no = method(__method__).super_method.source_location&.last || ''

      method_source = ''
      method_source = method(__method__).super_method.source if settings.analyze_source?

      p, l = caller.find { |c| c.to_s =~ /#{::Rails.root.to_s}/}&.split(':')

      spans = tmp_module.instance_variable_get(:@spans)
      span  = spans[Thread.current.object_id]
      klass = tmp_module.instance_variable_get(:@klass)

      arguments = Callstacking::Rails::Instrument.arguments_for(method(__method__).super_method, args)

      span.call_entry(klass, method_name, arguments, p || path, l || line_no, method_source)
      return_val = super(*args, **kwargs, &block)
      span.call_return(klass, method_name, p || path, l || line_no, return_val, method_source)

      return_val
    end

  end

  new_method
end

#instrumentation_required?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/callstacking/rails/instrument.rb', line 112

def instrumentation_required?
  span_modules.empty?
end

#reset!Object



116
117
118
# File 'lib/callstacking/rails/instrument.rb', line 116

def reset!
  span_modules.clear
end