Class: ActionDispatch::ServerTiming

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

Defined Under Namespace

Classes: Subscriber

Constant Summary collapse

SERVER_TIMING_HEADER =
"Server-Timing"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ServerTiming

Returns a new instance of ServerTiming.



52
53
54
55
56
# File 'lib/action_dispatch/middleware/server_timing.rb', line 52

def initialize(app)
  @app = app
  @subscriber = Subscriber.instance
  @subscriber.ensure_subscribed
end

Class Method Details

.unsubscribeObject

:nodoc:



48
49
50
# File 'lib/action_dispatch/middleware/server_timing.rb', line 48

def self.unsubscribe # :nodoc:
  Subscriber.instance.unsubscribe
end

Instance Method Details

#call(env) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/action_dispatch/middleware/server_timing.rb', line 58

def call(env)
  response = nil
  events = @subscriber.collect_events do
    response = @app.call(env)
  end

  headers = response[1]

  header_info = events.group_by(&:name).map do |event_name, events_collection|
    "%s;dur=%.2f" % [event_name, events_collection.sum(&:duration)]
  end

  header_info.prepend(headers[SERVER_TIMING_HEADER]) if headers[SERVER_TIMING_HEADER].present?
  headers[SERVER_TIMING_HEADER] = header_info.join(", ")

  response
end