Class: ActionDispatch::ServerTiming

Inherits:
Object
  • Object
show all
Defined in:
lib/action_dispatch/middleware/server_timing.rb

Constant Summary collapse

SERVER_TIMING_HEADER =
"Server-Timing"

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ServerTiming

Returns a new instance of ServerTiming.



9
10
11
# File 'lib/action_dispatch/middleware/server_timing.rb', line 9

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/action_dispatch/middleware/server_timing.rb', line 13

def call(env)
  events = []
  subscriber = ActiveSupport::Notifications.subscribe(/.*/) do |*args|
    events << ActiveSupport::Notifications::Event.new(*args)
  end

  status, headers, body = begin
    @app.call(env)
  ensure
    ActiveSupport::Notifications.unsubscribe(subscriber)
  end

  header_info = events.group_by(&:name).map do |event_name, events_collection|
    "#{event_name};dur=#{events_collection.sum(&:duration)}"
  end
  headers[SERVER_TIMING_HEADER] = header_info.join(", ")

  [ status, headers, body ]
end