appmap 0.31.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.dockerignore +5 -0
- data/.gitignore +17 -0
- data/.rubocop.yml +27 -0
- data/.ruby-version +1 -0
- data/.travis.yml +44 -0
- data/CHANGELOG.md +199 -0
- data/Dockerfile.appmap +5 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +300 -0
- data/Rakefile +132 -0
- data/appmap.gemspec +44 -0
- data/appmap.yml +8 -0
- data/examples/install.rb +76 -0
- data/examples/mock_webapp/Gemfile +1 -0
- data/examples/mock_webapp/appmap.yml +2 -0
- data/examples/mock_webapp/exe/mock_webapp_request +12 -0
- data/examples/mock_webapp/lib/mock_webapp/controller.rb +23 -0
- data/examples/mock_webapp/lib/mock_webapp/request.rb +12 -0
- data/examples/mock_webapp/lib/mock_webapp/user.rb +18 -0
- data/exe/appmap +154 -0
- data/lib/appmap.rb +87 -0
- data/lib/appmap/algorithm/prune_class_map.rb +67 -0
- data/lib/appmap/algorithm/stats.rb +91 -0
- data/lib/appmap/class_map.rb +135 -0
- data/lib/appmap/command/record.rb +38 -0
- data/lib/appmap/command/stats.rb +14 -0
- data/lib/appmap/config.rb +91 -0
- data/lib/appmap/cucumber.rb +89 -0
- data/lib/appmap/event.rb +168 -0
- data/lib/appmap/hook.rb +130 -0
- data/lib/appmap/metadata.rb +62 -0
- data/lib/appmap/middleware/remote_recording.rb +114 -0
- data/lib/appmap/minitest.rb +141 -0
- data/lib/appmap/rails/action_handler.rb +91 -0
- data/lib/appmap/rails/sql_handler.rb +145 -0
- data/lib/appmap/railtie.rb +45 -0
- data/lib/appmap/record.rb +27 -0
- data/lib/appmap/rspec.rb +301 -0
- data/lib/appmap/trace.rb +96 -0
- data/lib/appmap/util.rb +40 -0
- data/lib/appmap/version.rb +9 -0
- data/lore/pages/2019-05-21-install-and-record/index.pug +51 -0
- data/lore/pages/2019-05-21-install-and-record/install_example_appmap.png +0 -0
- data/lore/pages/2019-05-21-install-and-record/metadata.yml +5 -0
- data/lore/pages/layout.pug +66 -0
- data/lore/public/lib/bootstrap-4.1.3/css/bootstrap-grid.css +1912 -0
- data/lore/public/lib/bootstrap-4.1.3/css/bootstrap-grid.css.map +1 -0
- data/lore/public/lib/bootstrap-4.1.3/css/bootstrap-grid.min.css +7 -0
- data/lore/public/lib/bootstrap-4.1.3/css/bootstrap-grid.min.css.map +1 -0
- data/lore/public/lib/bootstrap-4.1.3/css/bootstrap-reboot.css +331 -0
- data/lore/public/lib/bootstrap-4.1.3/css/bootstrap-reboot.css.map +1 -0
- data/lore/public/lib/bootstrap-4.1.3/css/bootstrap-reboot.min.css +8 -0
- data/lore/public/lib/bootstrap-4.1.3/css/bootstrap-reboot.min.css.map +1 -0
- data/lore/public/lib/bootstrap-4.1.3/css/bootstrap.css +9030 -0
- data/lore/public/lib/bootstrap-4.1.3/css/bootstrap.css.map +1 -0
- data/lore/public/lib/bootstrap-4.1.3/css/bootstrap.min.css +7 -0
- data/lore/public/lib/bootstrap-4.1.3/css/bootstrap.min.css.map +1 -0
- data/lore/public/stylesheets/style.css +8 -0
- data/package-lock.json +1064 -0
- data/package.json +24 -0
- data/spec/abstract_controller4_base_spec.rb +67 -0
- data/spec/abstract_controller_base_spec.rb +72 -0
- data/spec/config_spec.rb +25 -0
- data/spec/fixtures/hook/attr_accessor.rb +5 -0
- data/spec/fixtures/hook/compare.rb +7 -0
- data/spec/fixtures/hook/constructor.rb +7 -0
- data/spec/fixtures/hook/exception_method.rb +11 -0
- data/spec/fixtures/hook/instance_method.rb +23 -0
- data/spec/fixtures/hook/openssl_sign.rb +87 -0
- data/spec/fixtures/hook/singleton_method.rb +54 -0
- data/spec/fixtures/rack_users_app/.dockerignore +2 -0
- data/spec/fixtures/rack_users_app/.gitignore +2 -0
- data/spec/fixtures/rack_users_app/Dockerfile +32 -0
- data/spec/fixtures/rack_users_app/Gemfile +10 -0
- data/spec/fixtures/rack_users_app/appmap.yml +3 -0
- data/spec/fixtures/rack_users_app/config.ru +2 -0
- data/spec/fixtures/rack_users_app/docker-compose.yml +9 -0
- data/spec/fixtures/rack_users_app/lib/app.rb +36 -0
- data/spec/fixtures/rails4_users_app/.gitignore +13 -0
- data/spec/fixtures/rails4_users_app/.rbenv-gemsets +2 -0
- data/spec/fixtures/rails4_users_app/.ruby-version +1 -0
- data/spec/fixtures/rails4_users_app/Dockerfile +30 -0
- data/spec/fixtures/rails4_users_app/Dockerfile.pg +3 -0
- data/spec/fixtures/rails4_users_app/Gemfile +77 -0
- data/spec/fixtures/rails4_users_app/README.rdoc +28 -0
- data/spec/fixtures/rails4_users_app/Rakefile +6 -0
- data/spec/fixtures/rails4_users_app/app/assets/images/.keep +0 -0
- data/spec/fixtures/rails4_users_app/app/assets/javascripts/application.js +16 -0
- data/spec/fixtures/rails4_users_app/app/assets/stylesheets/application.css +15 -0
- data/spec/fixtures/rails4_users_app/app/controllers/api/users_controller.rb +27 -0
- data/spec/fixtures/rails4_users_app/app/controllers/application_controller.rb +5 -0
- data/spec/fixtures/rails4_users_app/app/controllers/concerns/.keep +0 -0
- data/spec/fixtures/rails4_users_app/app/controllers/health_controller.rb +5 -0
- data/spec/fixtures/rails4_users_app/app/controllers/users_controller.rb +5 -0
- data/spec/fixtures/rails4_users_app/app/helpers/application_helper.rb +2 -0
- data/spec/fixtures/rails4_users_app/app/mailers/.keep +0 -0
- data/spec/fixtures/rails4_users_app/app/models/.keep +0 -0
- data/spec/fixtures/rails4_users_app/app/models/concerns/.keep +0 -0
- data/spec/fixtures/rails4_users_app/app/models/user.rb +18 -0
- data/spec/fixtures/rails4_users_app/app/views/layouts/application.html.haml +7 -0
- data/spec/fixtures/rails4_users_app/app/views/users/index.html.haml +7 -0
- data/spec/fixtures/rails4_users_app/appmap.yml +3 -0
- data/spec/fixtures/rails4_users_app/bin/rails +9 -0
- data/spec/fixtures/rails4_users_app/bin/setup +29 -0
- data/spec/fixtures/rails4_users_app/bin/spring +17 -0
- data/spec/fixtures/rails4_users_app/config.ru +4 -0
- data/spec/fixtures/rails4_users_app/config/application.rb +26 -0
- data/spec/fixtures/rails4_users_app/config/boot.rb +3 -0
- data/spec/fixtures/rails4_users_app/config/database.yml +18 -0
- data/spec/fixtures/rails4_users_app/config/environment.rb +5 -0
- data/spec/fixtures/rails4_users_app/config/environments/development.rb +41 -0
- data/spec/fixtures/rails4_users_app/config/environments/production.rb +79 -0
- data/spec/fixtures/rails4_users_app/config/environments/test.rb +42 -0
- data/spec/fixtures/rails4_users_app/config/initializers/assets.rb +11 -0
- data/spec/fixtures/rails4_users_app/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/fixtures/rails4_users_app/config/initializers/cookies_serializer.rb +3 -0
- data/spec/fixtures/rails4_users_app/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/fixtures/rails4_users_app/config/initializers/inflections.rb +16 -0
- data/spec/fixtures/rails4_users_app/config/initializers/mime_types.rb +4 -0
- data/spec/fixtures/rails4_users_app/config/initializers/session_store.rb +3 -0
- data/spec/fixtures/rails4_users_app/config/initializers/to_time_preserves_timezone.rb +10 -0
- data/spec/fixtures/rails4_users_app/config/initializers/wrap_parameters.rb +14 -0
- data/spec/fixtures/rails4_users_app/config/locales/en.yml +23 -0
- data/spec/fixtures/rails4_users_app/config/routes.rb +12 -0
- data/spec/fixtures/rails4_users_app/config/secrets.yml +22 -0
- data/spec/fixtures/rails4_users_app/create_app +23 -0
- data/spec/fixtures/rails4_users_app/db/migrate/20191127112304_create_users.rb +10 -0
- data/spec/fixtures/rails4_users_app/db/schema.rb +26 -0
- data/spec/fixtures/rails4_users_app/db/seeds.rb +7 -0
- data/spec/fixtures/rails4_users_app/docker-compose.yml +26 -0
- data/spec/fixtures/rails4_users_app/lib/assets/.keep +0 -0
- data/spec/fixtures/rails4_users_app/lib/tasks/.keep +0 -0
- data/spec/fixtures/rails4_users_app/log/.keep +0 -0
- data/spec/fixtures/rails4_users_app/public/404.html +67 -0
- data/spec/fixtures/rails4_users_app/public/422.html +67 -0
- data/spec/fixtures/rails4_users_app/public/500.html +66 -0
- data/spec/fixtures/rails4_users_app/public/favicon.ico +0 -0
- data/spec/fixtures/rails4_users_app/public/robots.txt +5 -0
- data/spec/fixtures/rails4_users_app/spec/controllers/users_controller_api_spec.rb +49 -0
- data/spec/fixtures/rails4_users_app/spec/rails_helper.rb +95 -0
- data/spec/fixtures/rails4_users_app/spec/spec_helper.rb +96 -0
- data/spec/fixtures/rails4_users_app/test/fixtures/users.yml +9 -0
- data/spec/fixtures/rails_users_app/.dockerignore +1 -0
- data/spec/fixtures/rails_users_app/.gitignore +39 -0
- data/spec/fixtures/rails_users_app/.rspec +1 -0
- data/spec/fixtures/rails_users_app/.ruby-version +1 -0
- data/spec/fixtures/rails_users_app/Dockerfile +29 -0
- data/spec/fixtures/rails_users_app/Dockerfile.pg +3 -0
- data/spec/fixtures/rails_users_app/Gemfile +52 -0
- data/spec/fixtures/rails_users_app/Rakefile +6 -0
- data/spec/fixtures/rails_users_app/app/controllers/api/users_controller.rb +27 -0
- data/spec/fixtures/rails_users_app/app/controllers/application_controller.rb +2 -0
- data/spec/fixtures/rails_users_app/app/controllers/concerns/.keep +0 -0
- data/spec/fixtures/rails_users_app/app/controllers/health_controller.rb +5 -0
- data/spec/fixtures/rails_users_app/app/controllers/users_controller.rb +5 -0
- data/spec/fixtures/rails_users_app/app/models/activerecord/user.rb +18 -0
- data/spec/fixtures/rails_users_app/app/models/concerns/.keep +0 -0
- data/spec/fixtures/rails_users_app/app/models/sequel/user.rb +25 -0
- data/spec/fixtures/rails_users_app/app/views/layouts/application.html.haml +7 -0
- data/spec/fixtures/rails_users_app/app/views/users/index.html.haml +7 -0
- data/spec/fixtures/rails_users_app/appmap.yml +3 -0
- data/spec/fixtures/rails_users_app/bin/appmap +29 -0
- data/spec/fixtures/rails_users_app/bin/byebug +29 -0
- data/spec/fixtures/rails_users_app/bin/gli +29 -0
- data/spec/fixtures/rails_users_app/bin/htmldiff +29 -0
- data/spec/fixtures/rails_users_app/bin/ldiff +29 -0
- data/spec/fixtures/rails_users_app/bin/nokogiri +29 -0
- data/spec/fixtures/rails_users_app/bin/rackup +29 -0
- data/spec/fixtures/rails_users_app/bin/rails +4 -0
- data/spec/fixtures/rails_users_app/bin/rake +29 -0
- data/spec/fixtures/rails_users_app/bin/rspec +29 -0
- data/spec/fixtures/rails_users_app/bin/ruby-parse +29 -0
- data/spec/fixtures/rails_users_app/bin/ruby-rewrite +29 -0
- data/spec/fixtures/rails_users_app/bin/sequel +29 -0
- data/spec/fixtures/rails_users_app/bin/setup +25 -0
- data/spec/fixtures/rails_users_app/bin/sprockets +29 -0
- data/spec/fixtures/rails_users_app/bin/thor +29 -0
- data/spec/fixtures/rails_users_app/bin/update +25 -0
- data/spec/fixtures/rails_users_app/config.ru +5 -0
- data/spec/fixtures/rails_users_app/config/application.rb +51 -0
- data/spec/fixtures/rails_users_app/config/boot.rb +3 -0
- data/spec/fixtures/rails_users_app/config/credentials.yml.enc +1 -0
- data/spec/fixtures/rails_users_app/config/database.yml +18 -0
- data/spec/fixtures/rails_users_app/config/environment.rb +5 -0
- data/spec/fixtures/rails_users_app/config/environments/development.rb +40 -0
- data/spec/fixtures/rails_users_app/config/environments/production.rb +68 -0
- data/spec/fixtures/rails_users_app/config/environments/test.rb +36 -0
- data/spec/fixtures/rails_users_app/config/initializers/application_controller_renderer.rb +8 -0
- data/spec/fixtures/rails_users_app/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/fixtures/rails_users_app/config/initializers/cors.rb +16 -0
- data/spec/fixtures/rails_users_app/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/fixtures/rails_users_app/config/initializers/inflections.rb +16 -0
- data/spec/fixtures/rails_users_app/config/initializers/mime_types.rb +4 -0
- data/spec/fixtures/rails_users_app/config/initializers/record_button.rb +3 -0
- data/spec/fixtures/rails_users_app/config/initializers/wrap_parameters.rb +9 -0
- data/spec/fixtures/rails_users_app/config/locales/en.yml +33 -0
- data/spec/fixtures/rails_users_app/config/routes.rb +11 -0
- data/spec/fixtures/rails_users_app/create_app +27 -0
- data/spec/fixtures/rails_users_app/db/migrate/20190728211408_create_users.rb +9 -0
- data/spec/fixtures/rails_users_app/db/schema.rb +23 -0
- data/spec/fixtures/rails_users_app/docker-compose.yml +28 -0
- data/spec/fixtures/rails_users_app/features/api_users.feature +13 -0
- data/spec/fixtures/rails_users_app/features/support/env.rb +4 -0
- data/spec/fixtures/rails_users_app/features/support/hooks.rb +11 -0
- data/spec/fixtures/rails_users_app/features/support/steps.rb +18 -0
- data/spec/fixtures/rails_users_app/lib/tasks/.keep +0 -0
- data/spec/fixtures/rails_users_app/log/.keep +0 -0
- data/spec/fixtures/rails_users_app/public/robots.txt +1 -0
- data/spec/fixtures/rails_users_app/spec/controllers/users_controller_api_spec.rb +29 -0
- data/spec/fixtures/rails_users_app/spec/models/user_spec.rb +39 -0
- data/spec/fixtures/rails_users_app/spec/rails_helper.rb +66 -0
- data/spec/fixtures/rails_users_app/spec/spec_helper.rb +96 -0
- data/spec/fixtures/rails_users_app/users_app/.gitignore +20 -0
- data/spec/hook_spec.rb +576 -0
- data/spec/rails_spec_helper.rb +60 -0
- data/spec/railtie_spec.rb +44 -0
- data/spec/record_sql_rails4_pg_spec.rb +76 -0
- data/spec/record_sql_rails_pg_spec.rb +68 -0
- data/spec/remote_recording_spec.rb +117 -0
- data/spec/rspec_feature_metadata_spec.rb +32 -0
- data/spec/spec_helper.rb +15 -0
- data/spec/util_spec.rb +21 -0
- data/test/cli_test.rb +116 -0
- data/test/cucumber_test.rb +72 -0
- data/test/fixtures/cli_record_test/appmap.yml +3 -0
- data/test/fixtures/cli_record_test/lib/cli_record_test/main.rb +9 -0
- data/test/fixtures/cucumber4_recorder/Gemfile +5 -0
- data/test/fixtures/cucumber4_recorder/appmap.yml +3 -0
- data/test/fixtures/cucumber4_recorder/features/say_hello.feature +5 -0
- data/test/fixtures/cucumber4_recorder/features/support/env.rb +5 -0
- data/test/fixtures/cucumber4_recorder/features/support/hooks.rb +11 -0
- data/test/fixtures/cucumber4_recorder/features/support/steps.rb +9 -0
- data/test/fixtures/cucumber4_recorder/lib/hello.rb +7 -0
- data/test/fixtures/cucumber_recorder/Gemfile +5 -0
- data/test/fixtures/cucumber_recorder/appmap.yml +3 -0
- data/test/fixtures/cucumber_recorder/features/say_hello.feature +5 -0
- data/test/fixtures/cucumber_recorder/features/support/env.rb +5 -0
- data/test/fixtures/cucumber_recorder/features/support/hooks.rb +11 -0
- data/test/fixtures/cucumber_recorder/features/support/steps.rb +9 -0
- data/test/fixtures/cucumber_recorder/lib/hello.rb +7 -0
- data/test/fixtures/minitest_recorder/Gemfile +5 -0
- data/test/fixtures/minitest_recorder/appmap.yml +3 -0
- data/test/fixtures/minitest_recorder/lib/hello.rb +5 -0
- data/test/fixtures/minitest_recorder/test/hello_test.rb +12 -0
- data/test/fixtures/process_recorder/appmap.yml +3 -0
- data/test/fixtures/process_recorder/hello.rb +9 -0
- data/test/fixtures/rspec_recorder/Gemfile +5 -0
- data/test/fixtures/rspec_recorder/appmap.yml +3 -0
- data/test/fixtures/rspec_recorder/lib/hello.rb +5 -0
- data/test/fixtures/rspec_recorder/spec/decorated_hello_spec.rb +21 -0
- data/test/fixtures/rspec_recorder/spec/labeled_hello_spec.rb +9 -0
- data/test/fixtures/rspec_recorder/spec/plain_hello_spec.rb +9 -0
- data/test/minitest_test.rb +38 -0
- data/test/record_process_test.rb +35 -0
- data/test/rspec_test.rb +82 -0
- data/test/test_helper.rb +4 -0
- metadata +525 -0
@@ -0,0 +1,114 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module AppMap
|
4
|
+
module Middleware
|
5
|
+
# RemoteRecording adds `/_appmap/record` routes to control recordings via HTTP requests
|
6
|
+
class RemoteRecording
|
7
|
+
def initialize(app)
|
8
|
+
require 'json'
|
9
|
+
|
10
|
+
@app = app
|
11
|
+
end
|
12
|
+
|
13
|
+
def event_loop
|
14
|
+
loop do
|
15
|
+
event = @tracer.next_event if @tracer
|
16
|
+
if event
|
17
|
+
@events << event.to_h
|
18
|
+
else
|
19
|
+
sleep 0.0001
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def start_recording
|
25
|
+
return [ 409, 'Recording is already in progress' ] if @tracer
|
26
|
+
|
27
|
+
@events = []
|
28
|
+
@tracer = AppMap.tracing.trace
|
29
|
+
@event_thread = Thread.new { event_loop }
|
30
|
+
@event_thread.abort_on_exception = true
|
31
|
+
|
32
|
+
[ 200 ]
|
33
|
+
end
|
34
|
+
|
35
|
+
def stop_recording(req)
|
36
|
+
return [ 404, 'No recording is in progress' ] unless @tracer
|
37
|
+
|
38
|
+
tracer = @tracer
|
39
|
+
@tracer = nil
|
40
|
+
|
41
|
+
AppMap.tracing.delete(tracer)
|
42
|
+
|
43
|
+
@event_thread.exit
|
44
|
+
@event_thread.join
|
45
|
+
@event_thread = nil
|
46
|
+
|
47
|
+
# Delete the events which are calls to or returns from the URL path _appmap/record
|
48
|
+
# because these are not of interest to the user.
|
49
|
+
is_control_command_event = lambda do |event|
|
50
|
+
event[:event] == :call &&
|
51
|
+
event[:http_server_request] &&
|
52
|
+
event[:http_server_request][:path_info] == '/_appmap/record'
|
53
|
+
end
|
54
|
+
control_command_events = @events.select(&is_control_command_event)
|
55
|
+
|
56
|
+
is_return_from_control_command_event = lambda do |event|
|
57
|
+
event[:parent_id] && control_command_events.find { |e| e[:id] == event[:parent_id] }
|
58
|
+
end
|
59
|
+
|
60
|
+
@events.delete_if(&is_control_command_event)
|
61
|
+
@events.delete_if(&is_return_from_control_command_event)
|
62
|
+
|
63
|
+
metadata = AppMap.detect_metadata
|
64
|
+
metadata[:recorder] = {
|
65
|
+
name: 'remote_recording'
|
66
|
+
}
|
67
|
+
|
68
|
+
response = JSON.generate \
|
69
|
+
version: AppMap::APPMAP_FORMAT_VERSION,
|
70
|
+
classMap: AppMap.class_map(tracer.event_methods),
|
71
|
+
metadata: metadata,
|
72
|
+
events: @events
|
73
|
+
|
74
|
+
[ 200, response ]
|
75
|
+
end
|
76
|
+
|
77
|
+
def call(env)
|
78
|
+
req = Rack::Request.new(env)
|
79
|
+
return handle_record_request(req) if req.path == '/_appmap/record'
|
80
|
+
|
81
|
+
@app.call(env)
|
82
|
+
end
|
83
|
+
|
84
|
+
def recording_state
|
85
|
+
[ 200, JSON.generate({ enabled: recording? }) ]
|
86
|
+
end
|
87
|
+
|
88
|
+
def handle_record_request(req)
|
89
|
+
method = req.env['REQUEST_METHOD']
|
90
|
+
|
91
|
+
status, body = \
|
92
|
+
if method.eql?('GET')
|
93
|
+
recording_state
|
94
|
+
elsif method.eql?('POST')
|
95
|
+
start_recording
|
96
|
+
elsif method.eql?('DELETE')
|
97
|
+
stop_recording(req)
|
98
|
+
else
|
99
|
+
[ 404, '' ]
|
100
|
+
end
|
101
|
+
|
102
|
+
[status, { 'Content-Type' => 'application/json' }, [body || '']]
|
103
|
+
end
|
104
|
+
|
105
|
+
def html_response?(headers)
|
106
|
+
headers['Content-Type'] && headers['Content-Type'] =~ /html/
|
107
|
+
end
|
108
|
+
|
109
|
+
def recording?
|
110
|
+
!@event_thread.nil?
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,141 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'appmap/util'
|
4
|
+
|
5
|
+
module AppMap
|
6
|
+
# Integration of AppMap with Minitest. When enabled with APPMAP=true, the AppMap tracer will
|
7
|
+
# be activated around each test.
|
8
|
+
module Minitest
|
9
|
+
APPMAP_OUTPUT_DIR = 'tmp/appmap/minitest'
|
10
|
+
LOG = false
|
11
|
+
|
12
|
+
def self.metadata
|
13
|
+
AppMap.detect_metadata
|
14
|
+
end
|
15
|
+
|
16
|
+
Recording = Struct.new(:test) do
|
17
|
+
def initialize(test)
|
18
|
+
super
|
19
|
+
|
20
|
+
warn "Starting recording of test #{test.class}.#{test.name}" if AppMap::Minitest::LOG
|
21
|
+
@trace = AppMap.tracing.trace
|
22
|
+
end
|
23
|
+
|
24
|
+
def finish
|
25
|
+
warn "Finishing recording of test #{test.class}.#{test.name}" if AppMap::Minitest::LOG
|
26
|
+
|
27
|
+
events = []
|
28
|
+
AppMap.tracing.delete @trace
|
29
|
+
|
30
|
+
events << @trace.next_event.to_h while @trace.event?
|
31
|
+
|
32
|
+
AppMap::Minitest.add_event_methods @trace.event_methods
|
33
|
+
|
34
|
+
class_map = AppMap.class_map(@trace.event_methods)
|
35
|
+
|
36
|
+
feature_group = test.class.name.underscore.split('_')[0...-1].join('_').capitalize
|
37
|
+
feature_name = test.name.split('_')[1..-1].join(' ')
|
38
|
+
scenario_name = [ feature_group, feature_name ].join(' ')
|
39
|
+
|
40
|
+
AppMap::Minitest.save scenario_name,
|
41
|
+
class_map,
|
42
|
+
events: events,
|
43
|
+
feature_name: feature_name,
|
44
|
+
feature_group_name: feature_group
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
@recordings_by_test = {}
|
49
|
+
@event_methods = Set.new
|
50
|
+
|
51
|
+
class << self
|
52
|
+
def init
|
53
|
+
warn 'Configuring AppMap recorder for Minitest'
|
54
|
+
|
55
|
+
FileUtils.mkdir_p APPMAP_OUTPUT_DIR
|
56
|
+
end
|
57
|
+
|
58
|
+
def begin_test(test)
|
59
|
+
@recordings_by_test[test.object_id] = Recording.new(test)
|
60
|
+
end
|
61
|
+
|
62
|
+
def end_test(test)
|
63
|
+
recording = @recordings_by_test.delete(test.object_id)
|
64
|
+
return warn "No recording found for #{test}" unless recording
|
65
|
+
|
66
|
+
recording.finish
|
67
|
+
end
|
68
|
+
|
69
|
+
def config
|
70
|
+
@config or raise "AppMap is not configured"
|
71
|
+
end
|
72
|
+
|
73
|
+
def add_event_methods(event_methods)
|
74
|
+
@event_methods += event_methods
|
75
|
+
end
|
76
|
+
|
77
|
+
def save(example_name, class_map, events: nil, feature_name: nil, feature_group_name: nil, labels: nil)
|
78
|
+
metadata = AppMap::Minitest.metadata.tap do |m|
|
79
|
+
m[:name] = example_name
|
80
|
+
m[:app] = AppMap.configuration.name
|
81
|
+
m[:feature] = feature_name if feature_name
|
82
|
+
m[:feature_group] = feature_group_name if feature_group_name
|
83
|
+
m[:frameworks] ||= []
|
84
|
+
m[:frameworks] << {
|
85
|
+
name: 'minitest',
|
86
|
+
version: Gem.loaded_specs['minitest']&.version&.to_s
|
87
|
+
}
|
88
|
+
m[:recorder] = {
|
89
|
+
name: 'minitest'
|
90
|
+
}
|
91
|
+
end
|
92
|
+
|
93
|
+
appmap = {
|
94
|
+
version: AppMap::APPMAP_FORMAT_VERSION,
|
95
|
+
metadata: metadata,
|
96
|
+
classMap: class_map,
|
97
|
+
events: events
|
98
|
+
}.compact
|
99
|
+
fname = AppMap::Util.scenario_filename(example_name)
|
100
|
+
|
101
|
+
File.write(File.join(APPMAP_OUTPUT_DIR, fname), JSON.generate(appmap))
|
102
|
+
end
|
103
|
+
|
104
|
+
def print_inventory
|
105
|
+
class_map = AppMap.class_map(@event_methods)
|
106
|
+
save 'Inventory', class_map, labels: %w[inventory]
|
107
|
+
end
|
108
|
+
|
109
|
+
def enabled?
|
110
|
+
ENV['APPMAP'] == 'true'
|
111
|
+
end
|
112
|
+
|
113
|
+
def run
|
114
|
+
init
|
115
|
+
at_exit do
|
116
|
+
print_inventory
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
if AppMap::Minitest.enabled?
|
124
|
+
require 'appmap'
|
125
|
+
require 'minitest/test'
|
126
|
+
|
127
|
+
class ::Minitest::Test
|
128
|
+
alias run_without_hook run
|
129
|
+
|
130
|
+
def run
|
131
|
+
AppMap::Minitest.begin_test self
|
132
|
+
begin
|
133
|
+
run_without_hook
|
134
|
+
ensure
|
135
|
+
AppMap::Minitest.end_test self
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
AppMap::Minitest.run
|
141
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'appmap/event'
|
4
|
+
|
5
|
+
module AppMap
|
6
|
+
module Rails
|
7
|
+
module ActionHandler
|
8
|
+
Context = Struct.new(:id, :start_time)
|
9
|
+
|
10
|
+
module ContextKey
|
11
|
+
def context_key
|
12
|
+
"#{HTTPServerRequest.name}#call"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class HTTPServerRequest
|
17
|
+
include ContextKey
|
18
|
+
|
19
|
+
class Call < AppMap::Event::MethodEvent
|
20
|
+
attr_accessor :payload
|
21
|
+
|
22
|
+
def initialize(path, lineno, payload)
|
23
|
+
super AppMap::Event.next_id_counter, :call, HTTPServerRequest, :call, path, lineno, Thread.current.object_id
|
24
|
+
|
25
|
+
self.payload = payload
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_h
|
29
|
+
super.tap do |h|
|
30
|
+
h[:http_server_request] = {
|
31
|
+
request_method: payload[:method],
|
32
|
+
path_info: payload[:path]
|
33
|
+
}
|
34
|
+
|
35
|
+
params = payload[:params]
|
36
|
+
h[:message] = params.keys.map do |key|
|
37
|
+
val = params[key]
|
38
|
+
{
|
39
|
+
name: key,
|
40
|
+
class: val.class.name,
|
41
|
+
value: self.class.display_string(val),
|
42
|
+
object_id: val.__id__
|
43
|
+
}
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def call(_, started, finished, _, payload) # (name, started, finished, unique_id, payload)
|
50
|
+
event = Call.new(__FILE__, __LINE__, payload)
|
51
|
+
Thread.current[context_key] = Context.new(event.id, Time.now)
|
52
|
+
AppMap.tracing.record_event(event)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
class HTTPServerResponse
|
57
|
+
include ContextKey
|
58
|
+
|
59
|
+
class Call < AppMap::Event::MethodReturnIgnoreValue
|
60
|
+
attr_accessor :payload
|
61
|
+
|
62
|
+
def initialize(path, lineno, payload, parent_id, elapsed)
|
63
|
+
super AppMap::Event.next_id_counter, :return, HTTPServerResponse, :call, path, lineno, Thread.current.object_id
|
64
|
+
|
65
|
+
self.payload = payload
|
66
|
+
self.parent_id = parent_id
|
67
|
+
self.elapsed = elapsed
|
68
|
+
end
|
69
|
+
|
70
|
+
def to_h
|
71
|
+
super.tap do |h|
|
72
|
+
h[:http_server_response] = {
|
73
|
+
status: payload[:status]
|
74
|
+
}
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def call(_, started, finished, _, payload) # (name, started, finished, unique_id, payload)
|
80
|
+
return unless Thread.current[context_key]
|
81
|
+
|
82
|
+
context = Thread.current[context_key]
|
83
|
+
Thread.current[context_key] = nil
|
84
|
+
|
85
|
+
event = Call.new(__FILE__, __LINE__, payload, context.id, Time.now - context.start_time)
|
86
|
+
AppMap.tracing.record_event(event)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,145 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'appmap/event'
|
4
|
+
|
5
|
+
module AppMap
|
6
|
+
module Rails
|
7
|
+
class SQLHandler
|
8
|
+
class SQLCall < AppMap::Event::MethodEvent
|
9
|
+
attr_accessor :payload
|
10
|
+
|
11
|
+
def initialize(path, lineno, payload)
|
12
|
+
super AppMap::Event.next_id_counter, :call, SQLHandler, :call, path, lineno, Thread.current.object_id
|
13
|
+
|
14
|
+
self.payload = payload
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_h
|
18
|
+
super.tap do |h|
|
19
|
+
h[:sql_query] = {
|
20
|
+
sql: payload[:sql],
|
21
|
+
database_type: payload[:database_type]
|
22
|
+
}.tap do |sql_query|
|
23
|
+
%i[server_version explain_sql].each do |attribute|
|
24
|
+
sql_query[attribute] = payload[attribute] if payload[attribute]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class SQLReturn < AppMap::Event::MethodReturnIgnoreValue
|
32
|
+
def initialize(path, lineno, parent_id, elapsed)
|
33
|
+
super AppMap::Event.next_id_counter, :return, SQLHandler, :call, path, lineno, Thread.current.object_id
|
34
|
+
|
35
|
+
self.parent_id = parent_id
|
36
|
+
self.elapsed = elapsed
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
module SQLExaminer
|
41
|
+
class << self
|
42
|
+
def examine(payload, sql:)
|
43
|
+
return unless (examiner = build_examiner)
|
44
|
+
|
45
|
+
payload[:server_version] = examiner.server_version
|
46
|
+
payload[:database_type] = examiner.database_type.to_s
|
47
|
+
end
|
48
|
+
|
49
|
+
protected
|
50
|
+
|
51
|
+
def build_examiner
|
52
|
+
if defined?(Sequel)
|
53
|
+
SequelExaminer.new
|
54
|
+
elsif defined?(ActiveRecord)
|
55
|
+
ActiveRecordExaminer.new
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
class SequelExaminer
|
61
|
+
def server_version
|
62
|
+
Sequel::Model.db.server_version
|
63
|
+
end
|
64
|
+
|
65
|
+
def database_type
|
66
|
+
Sequel::Model.db.database_type.to_sym
|
67
|
+
end
|
68
|
+
|
69
|
+
def execute_query(sql)
|
70
|
+
Sequel::Model.db[sql].all
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
class ActiveRecordExaminer
|
75
|
+
def server_version
|
76
|
+
case database_type
|
77
|
+
when :postgres
|
78
|
+
ActiveRecord::Base.connection.postgresql_version
|
79
|
+
else
|
80
|
+
warn "Unable to determine database version for #{database_type.inspect}"
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def database_type
|
85
|
+
return :postgres if ActiveRecord::Base.connection.respond_to?(:postgresql_version)
|
86
|
+
|
87
|
+
ActiveRecord::Base.connection.adapter_name.downcase.to_sym
|
88
|
+
end
|
89
|
+
|
90
|
+
def execute_query(sql)
|
91
|
+
ActiveRecord::Base.connection.execute(sql).inject([]) { |memo, r| memo << r; memo }
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def call(_, started, finished, _, payload) # (name, started, finished, unique_id, payload)
|
97
|
+
return if AppMap.tracing.empty?
|
98
|
+
|
99
|
+
reentry_key = "#{self.class.name}#call"
|
100
|
+
return if Thread.current[reentry_key] == true
|
101
|
+
|
102
|
+
Thread.current[reentry_key] = true
|
103
|
+
begin
|
104
|
+
sql = payload[:sql].strip
|
105
|
+
|
106
|
+
# Detect whether a function call within a specified filename is present in the call stack.
|
107
|
+
find_in_backtrace = lambda do |file_name, function_name = nil|
|
108
|
+
Thread.current.backtrace.find do |line|
|
109
|
+
tokens = line.split(':')
|
110
|
+
matches_file = tokens.find { |t| t.rindex(file_name) == (t.length - file_name.length) }
|
111
|
+
matches_function = function_name.nil? || tokens.find { |t| t == "in `#{function_name}'" }
|
112
|
+
matches_file && matches_function
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
# Ignore SQL calls which are made while establishing a new connection.
|
117
|
+
#
|
118
|
+
# Example:
|
119
|
+
# /path/to/ruby/2.6.0/gems/sequel-5.20.0/lib/sequel/connection_pool.rb:122:in `make_new'
|
120
|
+
return if find_in_backtrace.call('lib/sequel/connection_pool.rb', 'make_new')
|
121
|
+
# lib/active_record/connection_adapters/abstract/connection_pool.rb:811:in `new_connection'
|
122
|
+
return if find_in_backtrace.call('lib/active_record/connection_adapters/abstract/connection_pool.rb', 'new_connection')
|
123
|
+
|
124
|
+
# Ignore SQL calls which are made while inspecting the DB schema.
|
125
|
+
#
|
126
|
+
# Example:
|
127
|
+
# /path/to/ruby/2.6.0/gems/sequel-5.20.0/lib/sequel/model/base.rb:812:in `get_db_schema'
|
128
|
+
return if find_in_backtrace.call('lib/sequel/model/base.rb', 'get_db_schema')
|
129
|
+
# /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/model_schema.rb:466:in `load_schema!'
|
130
|
+
return if find_in_backtrace.call('lib/active_record/model_schema.rb', 'load_schema!')
|
131
|
+
return if find_in_backtrace.call('lib/active_model/attribute_methods.rb', 'define_attribute_methods')
|
132
|
+
return if find_in_backtrace.call('lib/active_record/connection_adapters/schema_cache.rb')
|
133
|
+
|
134
|
+
SQLExaminer.examine payload, sql: sql
|
135
|
+
|
136
|
+
call = SQLCall.new(__FILE__, __LINE__, payload)
|
137
|
+
AppMap.tracing.record_event(call)
|
138
|
+
AppMap.tracing.record_event(SQLReturn.new(__FILE__, __LINE__, call.id, finished - started))
|
139
|
+
ensure
|
140
|
+
Thread.current[reentry_key] = nil
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|