aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_dispatch/middleware/notifications.rb
blob: c3776d53a8e45087b69dbe5545661278e5ad403b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
module ActionDispatch
  # Provide notifications in the middleware stack. Notice that for the before_dispatch
  # and after_dispatch notifications, we just send the original env, so we don't pile
  # up large env hashes in the queue. However, in exception cases, the whole env hash
  # is actually useful, so we send it all.
  class Notifications
    def initialize(app)
      @app = app
    end

    def call(env)
      payload = retrieve_payload_from_env(env)
      ActiveSupport::Notifications.instrument("action_dispatch.before_dispatch", payload)

      ActiveSupport::Notifications.instrument!("action_dispatch.after_dispatch", payload) do
        @app.call(env)
      end
    rescue Exception => exception
      ActiveSupport::Notifications.instrument('action_dispatch.exception',
        :env => env, :exception => exception)
      raise exception
    end

  protected

    # Remove any rack related constants from the env, like rack.input.
    def retrieve_payload_from_env(env)
      Hash[:env => env.except(*env.keys.select { |k| k.to_s.index("rack.") == 0 })]
    end
  end
end