appmap 0.45.1 → 0.48.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
data/lib/appmap/hook.rb CHANGED
@@ -64,7 +64,7 @@ module AppMap
64
64
  end
65
65
  end
66
66
 
67
- config.builtin_methods.each do |class_name, hooks|
67
+ config.builtin_hooks.each do |class_name, hooks|
68
68
  Array(hooks).each do |hook|
69
69
  require hook.package.package_name if hook.package.package_name
70
70
  Array(hook.method_names).each do |method_name|
@@ -139,6 +139,8 @@ module AppMap
139
139
  # a stack overflow in the defined hook method.
140
140
  next if %w[Marshal AppMap ActiveSupport].member?((hook_cls&.name || '').split('::')[0])
141
141
 
142
+ next if method_id == :call
143
+
142
144
  method = begin
143
145
  hook_cls.public_instance_method(method_id)
144
146
  rescue NameError