Sha256: 170a19b9d3e145195f9fc33583ea76bb6607b01d411b892bbfdffcdea8cd7f62

Contents?: true

Size: 809 Bytes

Versions: 16

Compression:

Stored size: 809 Bytes

Contents

# frozen_string_literal: true

require "active_support/notifications"

module ActionDispatch
  class ServerTiming
    SERVER_TIMING_HEADER = "Server-Timing"

    def initialize(app)
      @app = app
    end

    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
  end
end

Version data entries

16 entries across 14 versions & 2 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/actionpack-7.0.3.1/lib/action_dispatch/middleware/server_timing.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/actionpack-7.0.2.3/lib/action_dispatch/middleware/server_timing.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/actionpack-7.0.3.1/lib/action_dispatch/middleware/server_timing.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/actionpack-7.0.2.3/lib/action_dispatch/middleware/server_timing.rb
actionpack-7.0.3.1 lib/action_dispatch/middleware/server_timing.rb
actionpack-7.0.3 lib/action_dispatch/middleware/server_timing.rb
actionpack-7.0.2.4 lib/action_dispatch/middleware/server_timing.rb
actionpack-7.0.2.3 lib/action_dispatch/middleware/server_timing.rb
actionpack-7.0.2.2 lib/action_dispatch/middleware/server_timing.rb
actionpack-7.0.2.1 lib/action_dispatch/middleware/server_timing.rb
actionpack-7.0.2 lib/action_dispatch/middleware/server_timing.rb
actionpack-7.0.1 lib/action_dispatch/middleware/server_timing.rb
actionpack-7.0.0 lib/action_dispatch/middleware/server_timing.rb
actionpack-7.0.0.rc3 lib/action_dispatch/middleware/server_timing.rb
actionpack-7.0.0.rc2 lib/action_dispatch/middleware/server_timing.rb
actionpack-7.0.0.rc1 lib/action_dispatch/middleware/server_timing.rb