From 59204f36e93d1da8d558f27c133856bb62064470 Mon Sep 17 00:00:00 2001 From: David Elner Date: Thu, 10 Jan 2019 16:34:23 -0500 Subject: [PATCH] Changed: Rails to patch Base, API instead of Metal. --- .../contrib/rails/action_controller.rb | 9 ------- .../contrib/rails/action_controller_patch.rb | 2 ++ lib/ddtrace/contrib/rails/action_view.rb | 19 --------------- lib/ddtrace/contrib/rails/active_support.rb | 9 ------- lib/ddtrace/contrib/rails/core_extensions.rb | 24 ++++++------------- lib/ddtrace/contrib/rails/framework.rb | 1 - lib/ddtrace/contrib/rails/patcher.rb | 6 ++--- .../contrib/rails/action_controller_spec.rb | 3 +-- spec/ddtrace/contrib/rails/middleware_spec.rb | 1 - spec/ddtrace/contrib/rails/railtie_spec.rb | 11 --------- 10 files changed, 13 insertions(+), 72 deletions(-) delete mode 100644 lib/ddtrace/contrib/rails/action_view.rb diff --git a/lib/ddtrace/contrib/rails/action_controller.rb b/lib/ddtrace/contrib/rails/action_controller.rb index e03d1f6b54..577dcec4bb 100644 --- a/lib/ddtrace/contrib/rails/action_controller.rb +++ b/lib/ddtrace/contrib/rails/action_controller.rb @@ -8,15 +8,6 @@ module Contrib module Rails # Code used to create and handle 'rails.action_controller' spans. module ActionController - include Datadog::Patcher - - def self.instrument - # patch Rails core components - do_once(:instrument) do - Datadog::RailsActionPatcher.patch_action_controller - end - end - def self.start_processing(payload) # trace the execution tracer = Datadog.configuration[:rails][:tracer] diff --git a/lib/ddtrace/contrib/rails/action_controller_patch.rb b/lib/ddtrace/contrib/rails/action_controller_patch.rb index 22b99d5f55..5e4ab285aa 100644 --- a/lib/ddtrace/contrib/rails/action_controller_patch.rb +++ b/lib/ddtrace/contrib/rails/action_controller_patch.rb @@ -4,6 +4,8 @@ module Rails # Instrument ActiveController processing module ActionControllerPatch def self.included(base) + return if base.ancestors.include?(ProcessActionPatch) + if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.0.0') base.send(:prepend, ProcessActionPatch) else diff --git a/lib/ddtrace/contrib/rails/action_view.rb b/lib/ddtrace/contrib/rails/action_view.rb deleted file mode 100644 index 1690931bcf..0000000000 --- a/lib/ddtrace/contrib/rails/action_view.rb +++ /dev/null @@ -1,19 +0,0 @@ -require 'ddtrace/contrib/rails/utils' - -module Datadog - module Contrib - module Rails - # Code used to create and handle 'rails.render_template' and 'rails.render_partial' spans. - module ActionView - include Datadog::Patcher - - def self.instrument - # patch Rails core components - do_once(:instrument) do - Datadog::RailsRendererPatcher.patch_renderer - end - end - end - end - end -end diff --git a/lib/ddtrace/contrib/rails/active_support.rb b/lib/ddtrace/contrib/rails/active_support.rb index b7ab84a981..81a8be1ed5 100644 --- a/lib/ddtrace/contrib/rails/active_support.rb +++ b/lib/ddtrace/contrib/rails/active_support.rb @@ -6,15 +6,6 @@ module Contrib module Rails # Code used to create and handle 'rails.cache' spans. module ActiveSupport - include Datadog::Patcher - - def self.instrument - do_once(:instrument) do - # patch Rails core components - Datadog::RailsCachePatcher.patch_cache_store - end - end - def self.start_trace_cache(payload) tracer = Datadog.configuration[:rails][:tracer] diff --git a/lib/ddtrace/contrib/rails/core_extensions.rb b/lib/ddtrace/contrib/rails/core_extensions.rb index d0699e0fb7..3d6a12c90e 100644 --- a/lib/ddtrace/contrib/rails/core_extensions.rb +++ b/lib/ddtrace/contrib/rails/core_extensions.rb @@ -169,22 +169,6 @@ def with_datadog_span(span) module RailsActionPatcher include Datadog::Patcher - # Patch for ActionController::Metal - module MetalPatch - # For any Controller class that inherits from ActionController::Metal, - # make sure to include the tracing patch. We do this instead of Metal.include(Patch) - # because Base and API compose modules that would take precedence over the tracing patch. - # By adding it onto the inheriting class, we can make sure it has a higher precedence. - def inherited(base) - super - - # Make sure not to include the patch twice, to avoid double measurements on controllers. - unless base.ancestors.include?(Datadog::Contrib::Rails::ActionControllerPatch) - base.send(:include, Datadog::Contrib::Rails::ActionControllerPatch) - end - end - end - module_function def patch_action_controller @@ -197,7 +181,13 @@ def patch_process_action do_once(:patch_process_action) do require 'ddtrace/contrib/rails/action_controller_patch' - ::ActionController::Metal.extend(MetalPatch) + if defined?(::ActionController::Base) + ::ActionController::Base.send(:include, Datadog::Contrib::Rails::ActionControllerPatch) + end + + if defined?(::ActionController::API) + ::ActionController::API.send(:include, Datadog::Contrib::Rails::ActionControllerPatch) + end end end end diff --git a/lib/ddtrace/contrib/rails/framework.rb b/lib/ddtrace/contrib/rails/framework.rb index 379709edc7..2374ad6d62 100644 --- a/lib/ddtrace/contrib/rails/framework.rb +++ b/lib/ddtrace/contrib/rails/framework.rb @@ -8,7 +8,6 @@ require 'ddtrace/contrib/rails/ext' require 'ddtrace/contrib/rails/core_extensions' require 'ddtrace/contrib/rails/action_controller' -require 'ddtrace/contrib/rails/action_view' require 'ddtrace/contrib/rails/active_support' require 'ddtrace/contrib/rails/utils' diff --git a/lib/ddtrace/contrib/rails/patcher.rb b/lib/ddtrace/contrib/rails/patcher.rb index 9377bea9ef..0b16f5b626 100644 --- a/lib/ddtrace/contrib/rails/patcher.rb +++ b/lib/ddtrace/contrib/rails/patcher.rb @@ -76,9 +76,9 @@ def setup_tracer # Add instrumentation to Rails components def instrument_rails - Datadog::Contrib::Rails::ActionController.instrument - Datadog::Contrib::Rails::ActionView.instrument - Datadog::Contrib::Rails::ActiveSupport.instrument + Datadog::RailsActionPatcher.patch_action_controller + Datadog::RailsRendererPatcher.patch_renderer + Datadog::RailsCachePatcher.patch_cache_store end end end diff --git a/spec/ddtrace/contrib/rails/action_controller_spec.rb b/spec/ddtrace/contrib/rails/action_controller_spec.rb index 03ed46827c..05940b9d98 100644 --- a/spec/ddtrace/contrib/rails/action_controller_spec.rb +++ b/spec/ddtrace/contrib/rails/action_controller_spec.rb @@ -1,11 +1,10 @@ require 'ddtrace/contrib/rails/rails_helper' RSpec.describe 'ActionController tracing' do - let(:tracer) { ::Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } let(:rails_options) { { tracer: tracer } } before(:each) do - Datadog::RailsActionPatcher.patch_action_controller Datadog.configure do |c| c.use :rails, rails_options end diff --git a/spec/ddtrace/contrib/rails/middleware_spec.rb b/spec/ddtrace/contrib/rails/middleware_spec.rb index 827dfa329c..d9baf2dc10 100644 --- a/spec/ddtrace/contrib/rails/middleware_spec.rb +++ b/spec/ddtrace/contrib/rails/middleware_spec.rb @@ -32,7 +32,6 @@ def all_spans end before(:each) do - Datadog::RailsActionPatcher.patch_action_controller Datadog.configure do |c| c.use :rack, rack_options if use_rack c.use :rails, rails_options if use_rails diff --git a/spec/ddtrace/contrib/rails/railtie_spec.rb b/spec/ddtrace/contrib/rails/railtie_spec.rb index 29e0c33f9e..786cf0ae43 100644 --- a/spec/ddtrace/contrib/rails/railtie_spec.rb +++ b/spec/ddtrace/contrib/rails/railtie_spec.rb @@ -9,17 +9,6 @@ let(:tracer) { ::Datadog::Tracer.new(writer: FauxWriter.new) } - let(:routes) { { '/' => 'test#index' } } - let(:controllers) { [controller] } - - let(:controller) do - stub_const('TestController', Class.new(ActionController::Base) do - def index - head :ok - end - end) - end - RSpec::Matchers.define :have_kind_of_middleware do |expected| match do |actual| found = 0