appmap 0.45.0 → 0.48.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/appmap/event.rb CHANGED
@@ -21,10 +21,10 @@ module AppMap
21
21
  LIMIT = 100
22
22
 
23
23
  class << self
24
- def build_from_invocation(me, event_type)
25
- me.id = AppMap::Event.next_id_counter
26
- me.event = event_type
27
- me.thread_id = Thread.current.object_id
24
+ def build_from_invocation(event_type, event:)
25
+ event.id = AppMap::Event.next_id_counter
26
+ event.event = event_type
27
+ event.thread_id = Thread.current.object_id
28
28
  end
29
29
 
30
30
  # Gets a display string for a value. This is not meant to be a machine deserializable value.
@@ -48,8 +48,6 @@ module AppMap
48
48
  nil
49
49
  end
50
50
 
51
- protected
52
-
53
51
  # Heuristic for dynamically defined class whose name can be nil
54
52
  def best_class_name(value)
55
53
  value_cls = value.class
@@ -103,19 +101,20 @@ module AppMap
103
101
  attr_accessor :defined_class, :method_id, :path, :lineno, :parameters, :receiver, :static
104
102
 
105
103
  class << self
106
- def build_from_invocation(mc = MethodCall.new, defined_class, method, receiver, arguments)
104
+ def build_from_invocation(defined_class, method, receiver, arguments, event: MethodCall.new)
105
+ event ||= MethodCall.new
107
106
  defined_class ||= 'Class'
108
- mc.tap do
107
+ event.tap do
109
108
  static = receiver.is_a?(Module)
110
- mc.defined_class = defined_class
111
- mc.method_id = method.name.to_s
109
+ event.defined_class = defined_class
110
+ event.method_id = method.name.to_s
112
111
  if method.source_location
113
112
  path = method.source_location[0]
114
113
  path = path[Dir.pwd.length + 1..-1] if path.index(Dir.pwd) == 0
115
- mc.path = path
116
- mc.lineno = method.source_location[1]
114
+ event.path = path
115
+ event.lineno = method.source_location[1]
117
116
  else
118
- mc.path = [ defined_class, static ? '.' : '#', method.name ].join
117
+ event.path = [ defined_class, static ? '.' : '#', method.name ].join
119
118
  end
120
119
 
121
120
  # Check if the method has key parameters. If there are any they'll always be last.
@@ -123,7 +122,7 @@ module AppMap
123
122
  has_key = [[:dummy], *method.parameters].last.first.to_s.start_with?('key') && arguments[-1].is_a?(Hash)
124
123
  kwargs = has_key && arguments[-1].dup || {}
125
124
 
126
- mc.parameters = method.parameters.map.with_index do |method_param, idx|
125
+ event.parameters = method.parameters.map.with_index do |method_param, idx|
127
126
  param_type, param_name = method_param
128
127
  param_name ||= 'arg'
129
128
  value = case param_type
@@ -144,13 +143,13 @@ module AppMap
144
143
  kind: param_type
145
144
  }
146
145
  end
147
- mc.receiver = {
146
+ event.receiver = {
148
147
  class: best_class_name(receiver),
149
148
  object_id: receiver.__id__,
150
149
  value: display_string(receiver)
151
150
  }
152
- mc.static = static
153
- MethodEvent.build_from_invocation(mc, :call)
151
+ event.static = static
152
+ MethodEvent.build_from_invocation(:call, event: event)
154
153
  end
155
154
  end
156
155
  end
@@ -175,11 +174,12 @@ module AppMap
175
174
  attr_accessor :parent_id, :elapsed
176
175
 
177
176
  class << self
178
- def build_from_invocation(mr = MethodReturnIgnoreValue.new, parent_id, elapsed)
179
- mr.tap do |_|
180
- mr.parent_id = parent_id
181
- mr.elapsed = elapsed
182
- MethodEvent.build_from_invocation(mr, :return)
177
+ def build_from_invocation(parent_id, elapsed: nil, event: MethodReturnIgnoreValue.new)
178
+ event ||= MethodReturnIgnoreValue.new
179
+ event.tap do |_|
180
+ event.parent_id = parent_id
181
+ event.elapsed = elapsed
182
+ MethodEvent.build_from_invocation(:return, event: event)
183
183
  end
184
184
  end
185
185
  end
@@ -187,7 +187,7 @@ module AppMap
187
187
  def to_h
188
188
  super.tap do |h|
189
189
  h[:parent_id] = parent_id
190
- h[:elapsed] = elapsed
190
+ h[:elapsed] = elapsed if elapsed
191
191
  end
192
192
  end
193
193
  end
@@ -196,10 +196,11 @@ module AppMap
196
196
  attr_accessor :return_value, :exceptions
197
197
 
198
198
  class << self
199
- def build_from_invocation(mr = MethodReturn.new, parent_id, elapsed, return_value, exception)
200
- mr.tap do |_|
199
+ def build_from_invocation(parent_id, return_value, exception, elapsed: nil, event: MethodReturn.new)
200
+ event ||= MethodReturn.new
201
+ event.tap do |_|
201
202
  if return_value
202
- mr.return_value = {
203
+ event.return_value = {
203
204
  class: best_class_name(return_value),
204
205
  value: display_string(return_value),
205
206
  object_id: return_value.__id__
@@ -220,9 +221,9 @@ module AppMap
220
221
  next_exception = next_exception.cause
221
222
  end
222
223
 
223
- mr.exceptions = exceptions
224
+ event.exceptions = exceptions
224
225
  end
225
- MethodReturnIgnoreValue.build_from_invocation(mr, parent_id, elapsed)
226
+ MethodReturnIgnoreValue.build_from_invocation(parent_id, elapsed: elapsed, event: event)
226
227
  end
227
228
  end
228
229
  end
@@ -11,7 +11,7 @@ module AppMap
11
11
  end
12
12
 
13
13
  def handle_return(call_event_id, elapsed, return_value, exception)
14
- AppMap::Event::MethodReturn.build_from_invocation(call_event_id, elapsed, return_value, exception)
14
+ AppMap::Event::MethodReturn.build_from_invocation(call_event_id, return_value, exception, elapsed: elapsed)
15
15
  end
16
16
  end
17
17
  end
@@ -0,0 +1,124 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'appmap/event'
4
+ require 'appmap/hook'
5
+
6
+ module AppMap
7
+ module Handler
8
+ module Rails
9
+ module RequestHandler
10
+ class HTTPServerRequest < AppMap::Event::MethodEvent
11
+ attr_accessor :normalized_path_info, :request_method, :path_info, :params, :mime_type, :headers, :authorization
12
+
13
+ def initialize(request)
14
+ super AppMap::Event.next_id_counter, :call, Thread.current.object_id
15
+
16
+ self.request_method = request.request_method
17
+ self.normalized_path_info = normalized_path(request)
18
+ self.mime_type = request.headers['Content-Type']
19
+ self.headers = AppMap::Util.select_headers(request.env)
20
+ self.authorization = request.headers['Authorization']
21
+ self.path_info = request.path_info.split('?')[0]
22
+ # ActionDispatch::Http::ParameterFilter is deprecated
23
+ parameter_filter_cls = \
24
+ if defined?(ActiveSupport::ParameterFilter)
25
+ ActiveSupport::ParameterFilter
26
+ else
27
+ ActionDispatch::Http::ParameterFilter
28
+ end
29
+ self.params = parameter_filter_cls.new(::Rails.application.config.filter_parameters).filter(request.params)
30
+ end
31
+
32
+ def to_h
33
+ super.tap do |h|
34
+ h[:http_server_request] = {
35
+ request_method: request_method,
36
+ path_info: path_info,
37
+ mime_type: mime_type,
38
+ normalized_path_info: normalized_path_info,
39
+ authorization: authorization,
40
+ headers: headers,
41
+ }.compact
42
+
43
+ unless params.blank?
44
+ h[:message] = params.keys.map do |key|
45
+ val = params[key]
46
+ {
47
+ name: key,
48
+ class: val.class.name,
49
+ value: self.class.display_string(val),
50
+ object_id: val.__id__,
51
+ }.tap do |message|
52
+ properties = object_properties(val)
53
+ message[:properties] = properties if properties
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ private
61
+
62
+ def normalized_path(request, router = ::Rails.application.routes.router)
63
+ router.recognize request do |route, _|
64
+ app = route.app
65
+ next unless app.matches? request
66
+ return normalized_path request, app.rack_app.routes.router if app.engine?
67
+
68
+ return AppMap::Util.swaggerize_path(route.path.spec.to_s)
69
+ end
70
+ end
71
+ end
72
+
73
+ class HTTPServerResponse < AppMap::Event::MethodReturnIgnoreValue
74
+ attr_accessor :status, :mime_type, :headers
75
+
76
+ def initialize(response, parent_id, elapsed)
77
+ super AppMap::Event.next_id_counter, :return, Thread.current.object_id
78
+
79
+ self.status = response.status
80
+ self.mime_type = response.headers['Content-Type']
81
+ self.parent_id = parent_id
82
+ self.elapsed = elapsed
83
+ self.headers = AppMap::Util.select_headers(response.headers)
84
+ end
85
+
86
+ def to_h
87
+ super.tap do |h|
88
+ h[:http_server_response] = {
89
+ status_code: status,
90
+ mime_type: mime_type,
91
+ headers: headers
92
+ }.compact
93
+ end
94
+ end
95
+ end
96
+
97
+ class HookMethod < AppMap::Hook::Method
98
+ def initialize
99
+ # ActionController::Instrumentation has issued start_processing.action_controller and
100
+ # process_action.action_controller since Rails 3. Therefore it's a stable place to hook
101
+ # the request. Rails controller notifications can't be used directly because they don't
102
+ # provide response headers, and we want the Content-Type.
103
+ super(nil, ActionController::Instrumentation, ActionController::Instrumentation.instance_method(:process_action))
104
+ end
105
+
106
+ protected
107
+
108
+ def before_hook(receiver, defined_class, _) # args
109
+ call_event = HTTPServerRequest.new(receiver.request)
110
+ # http_server_request events are i/o and do not require a package name.
111
+ AppMap.tracing.record_event call_event, defined_class: defined_class, method: hook_method
112
+ [ call_event, TIME_NOW.call ]
113
+ end
114
+
115
+ def after_hook(receiver, call_event, start_time, _, _) # return_value, exception
116
+ elapsed = TIME_NOW.call - start_time
117
+ return_event = HTTPServerResponse.new receiver.response, call_event.id, elapsed
118
+ AppMap.tracing.record_event return_event
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,152 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'appmap/event'
4
+
5
+ module AppMap
6
+ module Handler
7
+ module Rails
8
+ class SQLHandler
9
+ class SQLCall < AppMap::Event::MethodCall
10
+ attr_accessor :payload
11
+
12
+ def initialize(payload)
13
+ super AppMap::Event.next_id_counter, :call, Thread.current.object_id
14
+
15
+ self.payload = payload
16
+ end
17
+
18
+ def to_h
19
+ super.tap do |h|
20
+ h[:sql_query] = {
21
+ sql: payload[:sql],
22
+ database_type: payload[:database_type]
23
+ }.tap do |sql_query|
24
+ %i[server_version].each do |attribute|
25
+ sql_query[attribute] = payload[attribute] if payload[attribute]
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ class SQLReturn < AppMap::Event::MethodReturnIgnoreValue
33
+ def initialize(parent_id, elapsed)
34
+ super AppMap::Event.next_id_counter, :return, Thread.current.object_id
35
+
36
+ self.parent_id = parent_id
37
+ self.elapsed = elapsed
38
+ end
39
+ end
40
+
41
+ module SQLExaminer
42
+ class << self
43
+ def examine(payload, sql:)
44
+ return unless (examiner = build_examiner)
45
+
46
+ payload[:server_version] = examiner.server_version
47
+ payload[:database_type] = examiner.database_type.to_s
48
+ end
49
+
50
+ protected
51
+
52
+ def build_examiner
53
+ if defined?(Sequel)
54
+ SequelExaminer.new
55
+ elsif defined?(ActiveRecord)
56
+ ActiveRecordExaminer.new
57
+ end
58
+ end
59
+ end
60
+
61
+ class SequelExaminer
62
+ def server_version
63
+ Sequel::Model.db.server_version
64
+ end
65
+
66
+ def database_type
67
+ Sequel::Model.db.database_type.to_sym
68
+ end
69
+
70
+ def execute_query(sql)
71
+ Sequel::Model.db[sql].all
72
+ end
73
+ end
74
+
75
+ class ActiveRecordExaminer
76
+ @@db_version_warning_issued = {}
77
+
78
+ def issue_warning
79
+ db_type = database_type
80
+ return if @@db_version_warning_issued[db_type]
81
+ warn("AppMap: Unable to determine database version for #{db_type.inspect}")
82
+ @@db_version_warning_issued[db_type] = true
83
+ end
84
+
85
+ def server_version
86
+ ActiveRecord::Base.connection.try(:database_version) || issue_warning
87
+ end
88
+
89
+ def database_type
90
+ type = ActiveRecord::Base.connection.adapter_name.downcase.to_sym
91
+ type = :postgres if type == :postgresql
92
+
93
+ type
94
+ end
95
+
96
+ def execute_query(sql)
97
+ ActiveRecord::Base.connection.execute(sql).inject([]) { |memo, r| memo << r; memo }
98
+ end
99
+ end
100
+ end
101
+
102
+ def call(_, started, finished, _, payload) # (name, started, finished, unique_id, payload)
103
+ return if AppMap.tracing.empty?
104
+
105
+ reentry_key = "#{self.class.name}#call"
106
+ return if Thread.current[reentry_key] == true
107
+
108
+ Thread.current[reentry_key] = true
109
+ begin
110
+ sql = payload[:sql].strip
111
+
112
+ # Detect whether a function call within a specified filename is present in the call stack.
113
+ find_in_backtrace = lambda do |file_name, function_name = nil|
114
+ Thread.current.backtrace.find do |line|
115
+ tokens = line.split(':')
116
+ matches_file = tokens.find { |t| t.rindex(file_name) == (t.length - file_name.length) }
117
+ matches_function = function_name.nil? || tokens.find { |t| t == "in `#{function_name}'" }
118
+ matches_file && matches_function
119
+ end
120
+ end
121
+
122
+ # Ignore SQL calls which are made while establishing a new connection.
123
+ #
124
+ # Example:
125
+ # /path/to/ruby/2.6.0/gems/sequel-5.20.0/lib/sequel/connection_pool.rb:122:in `make_new'
126
+ return if find_in_backtrace.call('lib/sequel/connection_pool.rb', 'make_new')
127
+ # lib/active_record/connection_adapters/abstract/connection_pool.rb:811:in `new_connection'
128
+ return if find_in_backtrace.call('lib/active_record/connection_adapters/abstract/connection_pool.rb', 'new_connection')
129
+
130
+ # Ignore SQL calls which are made while inspecting the DB schema.
131
+ #
132
+ # Example:
133
+ # /path/to/ruby/2.6.0/gems/sequel-5.20.0/lib/sequel/model/base.rb:812:in `get_db_schema'
134
+ return if find_in_backtrace.call('lib/sequel/model/base.rb', 'get_db_schema')
135
+ # /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/model_schema.rb:466:in `load_schema!'
136
+ return if find_in_backtrace.call('lib/active_record/model_schema.rb', 'load_schema!')
137
+ return if find_in_backtrace.call('lib/active_model/attribute_methods.rb', 'define_attribute_methods')
138
+ return if find_in_backtrace.call('lib/active_record/connection_adapters/schema_cache.rb')
139
+
140
+ SQLExaminer.examine payload, sql: sql
141
+
142
+ call = SQLCall.new(payload)
143
+ AppMap.tracing.record_event(call)
144
+ AppMap.tracing.record_event(SQLReturn.new(call.id, finished - started))
145
+ ensure
146
+ Thread.current[reentry_key] = nil
147
+ end
148
+ end
149
+ end
150
+ end
151
+ end
152
+ end
@@ -0,0 +1,155 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'appmap/event'
4
+
5
+ module AppMap
6
+ module Handler
7
+ module Rails
8
+ class Template
9
+ LOG = (ENV['APPMAP_TEMPLATE_DEBUG'] == 'true' || ENV['DEBUG'] == 'true')
10
+
11
+ # All the code which is touched by the AppMap is recorded in the classMap.
12
+ # This duck-typed 'method' is used to represent a view template as a package,
13
+ # class, and method in the classMap.
14
+ # The class name is generated from the template path. The package name is
15
+ # 'app/views', and the method name is 'render'. The source location of the method
16
+ # is, of course, the path to the view template.
17
+ TemplateMethod = Struct.new(:path) do
18
+ private_instance_methods :path
19
+ attr_reader :class_name
20
+
21
+ def initialize(path)
22
+ super
23
+
24
+ @class_name = path.parameterize.underscore
25
+ end
26
+
27
+ def package
28
+ 'app/views'
29
+ end
30
+
31
+ def name
32
+ 'render'
33
+ end
34
+
35
+ def source_location
36
+ path
37
+ end
38
+
39
+ def static
40
+ true
41
+ end
42
+
43
+ def comment
44
+ nil
45
+ end
46
+
47
+ def labels
48
+ [ 'mvc.template' ]
49
+ end
50
+ end
51
+
52
+ # TemplateCall is a type of function call which is specialized to view template rendering. Since
53
+ # there isn't really a perfect method in Rails to hook, this one is synthesized from the available
54
+ # information.
55
+ class TemplateCall < AppMap::Event::MethodEvent
56
+ # This is basically the +self+ parameter.
57
+ attr_reader :render_instance
58
+ # Path to the view template.
59
+ attr_accessor :path
60
+
61
+ def initialize(render_instance)
62
+ super :call
63
+
64
+ AppMap::Event::MethodEvent.build_from_invocation(:call, event: self)
65
+ @render_instance = render_instance
66
+ end
67
+
68
+ def static?
69
+ true
70
+ end
71
+
72
+ def to_h
73
+ super.tap do |h|
74
+ h[:defined_class] = path ? path.parameterize.underscore : 'inline_template'
75
+ h[:method_id] = 'render'
76
+ h[:path] = path
77
+ h[:static] = static?
78
+ h[:parameters] = []
79
+ h[:receiver] = {
80
+ class: AppMap::Event::MethodEvent.best_class_name(render_instance),
81
+ object_id: render_instance.__id__,
82
+ value: AppMap::Event::MethodEvent.display_string(render_instance)
83
+ }
84
+ h.compact
85
+ end
86
+ end
87
+ end
88
+
89
+ TEMPLATE_RENDERER = 'appmap.handler.rails.template.renderer'
90
+
91
+ # Hooks the ActionView::Resolver methods +find_all+, +find_all_anywhere+. The resolver is used
92
+ # during template rendering to lookup the template file path from parameters such as the
93
+ # template name, prefix, and partial (boolean).
94
+ class ResolverHandler
95
+ class << self
96
+ # Handled as a normal function call.
97
+ def handle_call(defined_class, hook_method, receiver, args)
98
+ name, prefix, partial = args
99
+ warn "Resolver: #{{ name: name, prefix: prefix, partial: partial }}" if LOG
100
+
101
+ AppMap::Handler::Function.handle_call(defined_class, hook_method, receiver, args)
102
+ end
103
+
104
+ # When the resolver returns, look to see if there is template rendering underway.
105
+ # If so, populate the template path. In all cases, add a TemplateMethod so that the
106
+ # template will be recorded in the classMap.
107
+ def handle_return(call_event_id, elapsed, return_value, exception)
108
+ renderer = Array(Thread.current[TEMPLATE_RENDERER]).last
109
+ path_obj = Array(return_value).first
110
+
111
+ warn "Resolver return: #{path_obj}" if LOG
112
+
113
+ if path_obj
114
+ path = if path_obj.respond_to?(:identifier) && path_obj.inspect.index('#<')
115
+ path_obj.identifier
116
+ else
117
+ path_obj.inspect
118
+ end
119
+ path = path[Dir.pwd.length + 1..-1] if path.index(Dir.pwd) == 0
120
+ AppMap.tracing.record_method(TemplateMethod.new(path))
121
+ renderer.path ||= path if renderer
122
+ end
123
+
124
+ AppMap::Handler::Function.handle_return(call_event_id, elapsed, return_value, exception)
125
+ end
126
+ end
127
+ end
128
+
129
+ # Hooks the ActionView::Renderer method +render+. This method is used by Rails to perform
130
+ # template rendering. The TemplateCall event which is emitted by this handler has a
131
+ # +path+ parameter, which is nil until it's filled in by a ResolverHandler.
132
+ class RenderHandler
133
+ class << self
134
+ def handle_call(defined_class, hook_method, receiver, args)
135
+ context, options = args
136
+
137
+ warn "Renderer: #{options}" if LOG
138
+
139
+ TemplateCall.new(receiver).tap do |call|
140
+ Thread.current[TEMPLATE_RENDERER] ||= []
141
+ Thread.current[TEMPLATE_RENDERER] << call
142
+ end
143
+ end
144
+
145
+ def handle_return(call_event_id, elapsed, return_value, exception)
146
+ Array(Thread.current[TEMPLATE_RENDERER]).pop
147
+
148
+ AppMap::Event::MethodReturnIgnoreValue.build_from_invocation(call_event_id, elapsed: elapsed)
149
+ end
150
+ end
151
+ end
152
+ end
153
+ end
154
+ end
155
+ end