Module: NewRelic::Agent::Instrumentation::Rack::Chain

Defined in:
lib/new_relic/agent/instrumentation/rack/chain.rb

Class Method Summary collapse

Class Method Details

.instrument!(builder_class) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/new_relic/agent/instrumentation/rack/chain.rb', line 8

def self.instrument!(builder_class)
  NewRelic::Agent::Instrumentation::RackBuilder.track_deferred_detection(builder_class)

  builder_class.class_eval do
    include ::NewRelic::Agent::Instrumentation::RackBuilder

    def to_app_with_newrelic_deferred_dependency_detection
      with_deferred_dependency_detection { to_app_without_newrelic }
    end

    alias_method(:to_app_without_newrelic, :to_app)
    alias_method(:to_app, :to_app_with_newrelic_deferred_dependency_detection)

    if ::NewRelic::Agent::Instrumentation::RackHelpers.middleware_instrumentation_enabled?
      ::NewRelic::Agent.logger.info("Installing #{builder_class} middleware instrumentation")

      def run_with_newrelic(app = nil, *args, &block)
        app_or_block = app || block
        run_with_tracing(app_or_block) do |wrapped|
          # Rack::Builder#run for Rack v3+ supports a block, and does not
          # support args. Whether a block or an app is provided, that
          # callable object will be wrapped into a MiddlewareProxy
          # instance. That proxy instance must then be passed to
          # run_without_newrelic as the app argument.
          block ? run_without_newrelic(wrapped, &nil) : run_without_newrelic(wrapped, *args)
        end
      end

      alias_method(:run_without_newrelic, :run)
      alias_method(:run, :run_with_newrelic)

      def use_with_newrelic(middleware_class, *args, &block)
        use_with_tracing(middleware_class) { |wrapped_class| use_without_newrelic(wrapped_class, *args, &block) }
      end
      ruby2_keywords(:use_with_newrelic) if respond_to?(:ruby2_keywords, true)

      alias_method(:use_without_newrelic, :use)
      alias_method(:use, :use_with_newrelic)
    end
  end
end