diff --git a/lib/ddtrace.rb b/lib/ddtrace.rb index b922cb1eb6..37dc08ba1d 100644 --- a/lib/ddtrace.rb +++ b/lib/ddtrace.rb @@ -28,25 +28,27 @@ def self.tracer # Datadog auto instrumentation for frameworks if defined?(Rails::VERSION) if Rails::VERSION::MAJOR.to_i >= 3 - begin - # We include 'redis-rails' here if it's available, doing it later - # (typically in initialize callback) does not work, it does not - # get loaded in the right context. - require 'redis-rails' - Datadog::Tracer.log.info("'redis-rails' module found, datadog redis integration is available") - rescue LoadError - Datadog::Tracer.log.info("no 'redis-rails' module found, datadog redis integration is not available") - end require 'ddtrace/contrib/rails/framework' - Datadog::Monkey.patch_module(:redis) # does nothing if redis is not loaded - Datadog::RailsPatcher.patch_renderer() - Datadog::RailsPatcher.patch_cache_store() - module Datadog # Run the auto instrumentation directly after the initialization of the application and # after the application initializers in config/initializers are run class Railtie < Rails::Railtie + config.before_configuration do + begin + # We include 'redis-rails' here if it's available, doing it later + # (typically in initialize callback) does not work, it does not + # get loaded in the right context. + require 'redis-rails' + Datadog::Tracer.log.info("'redis-rails' module found, datadog redis integration is available") + rescue LoadError + Datadog::Tracer.log.info("no 'redis-rails' module found, datadog redis integration is not available") + end + + Datadog::Monkey.patch_module(:redis) + end + + # we do actions config.after_initialize do |app| Datadog::Contrib::Rails::Framework.configure(config: app.config) Datadog::Contrib::Rails::Framework.auto_instrument() diff --git a/lib/ddtrace/contrib/rails/framework.rb b/lib/ddtrace/contrib/rails/framework.rb index f6b6b35b8a..2afe9178fc 100644 --- a/lib/ddtrace/contrib/rails/framework.rb +++ b/lib/ddtrace/contrib/rails/framework.rb @@ -94,6 +94,12 @@ def self.auto_instrument_redis def self.auto_instrument return unless ::Rails.configuration.datadog_trace[:auto_instrument] Datadog::Tracer.log.info('Detected Rails >= 3.x. Enabling auto-instrumentation for core components.') + + # patch Rails core components + Datadog::RailsPatcher.patch_renderer() + Datadog::RailsPatcher.patch_cache_store() + + # instrumenting Rails framework Datadog::Contrib::Rails::ActionController.instrument() Datadog::Contrib::Rails::ActionView.instrument() Datadog::Contrib::Rails::ActiveRecord.instrument() if defined?(::ActiveRecord)