Module: RailsServerTimings::ControllerRuntime

Defined in:
lib/rails_server_timings/controller_runtime.rb

Instance Method Summary collapse

Instance Method Details

#process_action(*args) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rails_server_timings/controller_runtime.rb', line 3

def process_action(*args)
  callback = lambda do |*_|
    event = ActiveSupport::Notifications::Event.new(*_)
    payload = event.payload
    timings = []

    factor = 1.0

    # Chrome 57 and lower interpret the header in seconds instead of
    # milliseconds, so we need to divide all the results by a factor of
    # 1000
    if match = request.user_agent.match(/Chrome\/(\d+)/)
      factor = 0.0001 if match[1] and match[1].to_i < 58
    end

    payload.each do |key, value|
      if idx = key.to_s =~ /_runtime/
        timings << ("#{key[0, idx]}=%.3f" % (value.to_f * factor))
      end
    end

    timings << ('total=%.3f' % (event.duration.to_f * factor))

    response.headers['Server-Timing'] = timings.join(', ')
  end


  ActiveSupport::Notifications.subscribed(callback, 'process_action.action_controller') do
    super
  end
end