Class: Profiler::Middleware::ProfilerMiddleware
- Inherits:
-
Object
- Object
- Profiler::Middleware::ProfilerMiddleware
- Defined in:
- lib/profiler/middleware/profiler_middleware.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app) ⇒ ProfilerMiddleware
constructor
A new instance of ProfilerMiddleware.
Constructor Details
#initialize(app) ⇒ ProfilerMiddleware
Returns a new instance of ProfilerMiddleware.
10 11 12 |
# File 'lib/profiler/middleware/profiler_middleware.rb', line 10 def initialize(app) @app = app end |
Instance Method Details
#call(env) ⇒ Object
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 |
# File 'lib/profiler/middleware/profiler_middleware.rb', line 14 def call(env) return @app.call(env) unless should_profile?(env) status = nil headers = nil body = nil collectors = nil profile = Models::Profile.new(build_request(env)) Profiler::CurrentContext.token = profile.token # Capture request body before app processes it req_body_raw = read_rack_input(env) # Store profile in env for collectors env["profiler.profile"] = profile collectors = create_collectors(profile) env["profiler.collectors"] = collectors # Measure memory before memory_before = current_memory if Profiler.configuration.track_memory status, headers, body = @app.call(env) # Measure memory after if Profiler.configuration.track_memory memory_after = current_memory profile.memory = memory_after - memory_before end body_content = collect_body(body) body = [body_content] profile.finish(status, headers) profile.set_bodies( request_body: req_body_raw, response_body: body_content, req_content_type: env["CONTENT_TYPE"].to_s, resp_content_type: (headers["content-type"] || headers["Content-Type"]).to_s ) collectors.each do |collector| begin collector.collect if collector.respond_to?(:collect) profile.(collector) rescue => e warn "Collector #{collector.class} failed: #{e.}" end end Profiler.storage.save(profile.token, profile) Profiler::CurrentContext.clear headers["X-Profiler-Token"] = profile.token if html_response?(headers) nonce = env['action_dispatch.content_security_policy_nonce'] body = ToolbarInjector.new(body, profile.token, nonce).inject end [status, headers, body] rescue => e warn "Profiler error: #{e.}\n#{e.backtrace.join("\n")}" collectors&.each { |c| c.unsubscribe if c.respond_to?(:unsubscribe) } Profiler::CurrentContext.clear status ? [status, headers, body || []] : @app.call(env) end |