Sha256: 55bd2dc15976ecd4bbb0fa94bc7aa179f3f55325dee6fce1f92f3b1a75bb5d29

Contents?: true

Size: 1.87 KB

Versions: 16

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

require "active_support/notifications"

module ActionDispatch
  class ServerTiming
    class Subscriber # :nodoc:
      include Singleton
      KEY = :action_dispatch_server_timing_events

      def initialize
        @mutex = Mutex.new
      end

      def call(event)
        if events = ActiveSupport::IsolatedExecutionState[KEY]
          events << event
        end
      end

      def collect_events
        events = []
        ActiveSupport::IsolatedExecutionState[KEY] = events
        yield
        events
      ensure
        ActiveSupport::IsolatedExecutionState.delete(KEY)
      end

      def ensure_subscribed
        @mutex.synchronize do
          # Subscribe to all events, except those beginning with "!"
          # Ideally we would be more selective of what is being measured
          @subscriber ||= ActiveSupport::Notifications.subscribe(/\A[^!]/, self)
        end
      end

      def unsubscribe
        @mutex.synchronize do
          ActiveSupport::Notifications.unsubscribe @subscriber
          @subscriber = nil
        end
      end
    end

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

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

    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

      if headers[ActionDispatch::Constants::SERVER_TIMING].present?
        header_info.prepend(headers[ActionDispatch::Constants::SERVER_TIMING])
      end
      headers[ActionDispatch::Constants::SERVER_TIMING] = header_info.join(", ")

      response
    end
  end
end

Version data entries

16 entries across 16 versions & 3 rubygems

Version Path
actionpack-7.1.5 lib/action_dispatch/middleware/server_timing.rb
actionpack-7.1.4.2 lib/action_dispatch/middleware/server_timing.rb
actionpack-7.1.4.1 lib/action_dispatch/middleware/server_timing.rb
actionpack-7.1.4 lib/action_dispatch/middleware/server_timing.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/actionpack-7.1.3.4/lib/action_dispatch/middleware/server_timing.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/actionpack-7.1.3.4/lib/action_dispatch/middleware/server_timing.rb
actionpack-7.1.3.4 lib/action_dispatch/middleware/server_timing.rb
actionpack-7.1.3.2 lib/action_dispatch/middleware/server_timing.rb
actionpack-7.1.3.1 lib/action_dispatch/middleware/server_timing.rb
actionpack-7.1.3 lib/action_dispatch/middleware/server_timing.rb
actionpack-7.1.2 lib/action_dispatch/middleware/server_timing.rb
actionpack-7.1.1 lib/action_dispatch/middleware/server_timing.rb
actionpack-7.1.0 lib/action_dispatch/middleware/server_timing.rb
actionpack-7.1.0.rc2 lib/action_dispatch/middleware/server_timing.rb
actionpack-7.1.0.rc1 lib/action_dispatch/middleware/server_timing.rb
actionpack-7.1.0.beta1 lib/action_dispatch/middleware/server_timing.rb