Class: EasyServerTiming::Middleware

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

Constant Summary collapse

SERVER_TIMING_HEADER =
"Server-Timing"

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



10
11
12
# File 'lib/easy_server_timing/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
# File 'lib/easy_server_timing/middleware.rb', line 14

def call(env)
  events = []
  subscriber = ActiveSupport::Notifications.subscribe(EasyServerTiming.notification_pattern) 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 { |e| e.duration.to_f }}"
  end
  headers[SERVER_TIMING_HEADER] = header_info.join(",")

  [ status, headers, body ]
end