10
11
12
13
14
15
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/mini_profiler_rails/railtie.rb', line 10
def self.initialize!(app)
raise "MiniProfilerRails initialized twice. Set `require: false' for rack-mini-profiler in your Gemfile" if defined?(@already_initialized) && @already_initialized
c = Rack::MiniProfiler.config
c.pre_authorize_cb = lambda { |env|
!Rails.env.test?
}
c.skip_paths ||= []
if serves_static_assets?(app)
c.skip_paths << app.config.assets.prefix
wp_assets_path = get_webpacker_assets_path()
c.skip_paths << wp_assets_path if wp_assets_path
end
unless Rails.env.development? || Rails.env.test?
c.authorization_mode = :allow_authorized
end
if Rails.logger
c.logger = Rails.logger
end
if c.storage == Rack::MiniProfiler::MemoryStore && c.storage_options.nil?
base_path = Rails.application.config.paths['tmp'].first rescue "#{Rails.root}/tmp"
tmp = base_path + '/miniprofiler'
c.storage_options = { path: tmp }
c.storage = Rack::MiniProfiler::FileStore
end
c.backtrace_remove = Rails.root.to_s + "/"
c.backtrace_includes = [/^\/?(app|config|lib|test)/]
c.skip_schema_queries = (Rails.env.development? || Rails.env.test?)
app.middleware.insert(0, Rack::MiniProfiler)
c.enable_advanced_debugging_tools = Rails.env.development?
if ::Rack::MiniProfiler.patch_rails?
ActiveSupport.on_load(:action_controller) do
::Rack::MiniProfiler.profile_method(ActionController::Base, :process) { |action| "Executing action: #{action}" }
end
ActiveSupport.on_load(:action_view) do
::Rack::MiniProfiler.profile_method(ActionView::Template, :render) { |x, y| "Rendering: #{@virtual_path}" }
end
else
subscribe("start_processing.action_controller") do |name, start, finish, id, payload|
next if !should_measure?
current = Rack::MiniProfiler.current
controller_name = payload[:controller].sub(/Controller\z/, '').downcase
description = "Executing: #{controller_name}##{payload[:action]}"
Thread.current[get_key(payload)] = current.current_timer
Rack::MiniProfiler.current.current_timer = current.current_timer.add_child(description)
end
subscribe("process_action.action_controller") do |name, start, finish, id, payload|
next if !should_measure?
key = get_key(payload)
parent_timer = Thread.current[key]
next if !parent_timer
Thread.current[key] = nil
Rack::MiniProfiler.current.current_timer.record_time
Rack::MiniProfiler.current.current_timer = parent_timer
end
subscribe("render_partial.action_view") do |name, start, finish, id, payload|
render_notification_handler(shorten_identifier(payload[:identifier]), finish, start)
end
subscribe("render_template.action_view") do |name, start, finish, id, payload|
render_notification_handler(shorten_identifier(payload[:identifier]), finish, start)
end
if Rack::MiniProfiler.subscribe_sql_active_record
subscribe("sql.active_record") do |name, start, finish, id, payload|
next if !should_measure?
next if payload[:name] =~ /SCHEMA/ && Rack::MiniProfiler.config.skip_schema_queries
Rack::MiniProfiler.record_sql(
payload[:sql],
(finish - start) * 1000,
Rack::MiniProfiler.binds_to_params(payload[:binds])
)
end
subscribe("instantiation.active_record") do |name, start, finish, id, payload|
next if !should_measure?
Rack::MiniProfiler.report_reader_duration(
(finish - start) * 1000,
payload[:record_count],
payload[:class_name]
)
end
end
end
@already_initialized = true
end
|