Class: Dial::Middleware

Inherits:
Object
  • Object
show all
Includes:
RailsStat, RubyStat
Defined in:
lib/dial/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



16
17
18
# File 'lib/dial/middleware.rb', line 16

def initialize app
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



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
# File 'lib/dial/middleware.rb', line 20

def call env
  unless env[HTTP_ACCEPT]&.include? CONTENT_TYPE_HTML
    return @app.call env
  end

  request = ::Rack::Request.new env
  unless should_profile? request
    return @app.call env
  end

  start_time = Process.clock_gettime Process::CLOCK_MONOTONIC

  status, headers, rack_body, ruby_vm_stat, gc_stat, gc_stat_heap, vernier_result = nil
  ::Prosopite.scan do
    vernier_result = ::Vernier.profile interval: Dial._configuration.vernier_interval, \
                                       allocation_interval: Dial._configuration.vernier_allocation_interval, \
                                       hooks: [:memory_usage, :rails] do
      ruby_vm_stat, gc_stat, gc_stat_heap = with_diffed_ruby_stats do
        status, headers, rack_body = @app.call env
      end
    end
  end

  unless headers[CONTENT_TYPE]&.include? CONTENT_TYPE_HTML
    return [status, headers, rack_body]
  end

  finish_time = Process.clock_gettime Process::CLOCK_MONOTONIC
  env[REQUEST_TIMING] = ((finish_time - start_time) * 1_000).round 2

  profile_key = Storage.generate_profile_key
  store_profile_data! vernier_result, (Storage.profile_storage_key profile_key)
  query_logs = clear_query_logs!
  server_timing = server_timing headers
  panel_html = Panel.html env, headers, profile_key, query_logs, ruby_vm_stat, gc_stat, gc_stat_heap, server_timing
  body = PanelInjector.new rack_body, panel_html

  headers.delete CONTENT_LENGTH

  [status, headers, body]
end