aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_dispatch/middleware
diff options
context:
space:
mode:
authorJohn Hawthorn <john@hawthorn.email>2019-05-07 13:16:57 -0700
committerJohn Hawthorn <john@hawthorn.email>2019-05-08 13:30:41 -0700
commit6f549ce53f3398403d4c2a47f9ecba910be256dd (patch)
tree866b93d0aa56cf3fcca33ecf92b215f1ddc1393b /actionpack/lib/action_dispatch/middleware
parent52125dc0f8669d8dd497427c7b177d5d04106e0c (diff)
downloadrails-6f549ce53f3398403d4c2a47f9ecba910be256dd.tar.gz
rails-6f549ce53f3398403d4c2a47f9ecba910be256dd.tar.bz2
rails-6f549ce53f3398403d4c2a47f9ecba910be256dd.zip
Only build middleware proxy when instrumentating
The instrumentation proxy adds three stack frames per-middleware, even when nothing is listening. This commit, when the middleware stack is built, only adds instrumentation when the `process_middleware.action_dispatch` event has already been subscribed to. The advantage to this is that we don't have any extra stack frames in apps which don't need middleware instrumentation. The disadvantage is that the subscriptions need to be in place when the middleware stack is built (during app boot). I think this is likely okay because temporary AS::Notifications subscriptions are strongly discouraged.
Diffstat (limited to 'actionpack/lib/action_dispatch/middleware')
-rw-r--r--actionpack/lib/action_dispatch/middleware/stack.rb15
1 files changed, 13 insertions, 2 deletions
diff --git a/actionpack/lib/action_dispatch/middleware/stack.rb b/actionpack/lib/action_dispatch/middleware/stack.rb
index f0c869fba0..57e4adb457 100644
--- a/actionpack/lib/action_dispatch/middleware/stack.rb
+++ b/actionpack/lib/action_dispatch/middleware/stack.rb
@@ -34,7 +34,11 @@ module ActionDispatch
end
def build(app)
- InstrumentationProxy.new(klass.new(app, *args, &block), inspect)
+ klass.new(app, *args, &block)
+ end
+
+ def build_instrumented(app)
+ InstrumentationProxy.new(build(app), inspect)
end
end
@@ -119,7 +123,14 @@ module ActionDispatch
end
def build(app = nil, &block)
- middlewares.freeze.reverse.inject(app || block) { |a, e| e.build(a) }
+ instrumenting = ActiveSupport::Notifications.notifier.listening?(InstrumentationProxy::EVENT_NAME)
+ middlewares.freeze.reverse.inject(app || block) do |a, e|
+ if instrumenting
+ e.build_instrumented(a)
+ else
+ e.build(a)
+ end
+ end
end
private