class EasyServerTiming::Middleware

Constants

SERVER_TIMING_HEADER

Public Class Methods

new(app) click to toggle source
# File lib/easy_server_timing/middleware.rb, line 10
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# 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