Skip to content

Commit

Permalink
[rails] prevent patching and instrumenting the Rails framework if use…
Browse files Browse the repository at this point in the history
…rs disable that
  • Loading branch information
Emanuele Palazzetti committed Jan 13, 2017
1 parent 909c5da commit 3e83010
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
28 changes: 15 additions & 13 deletions lib/ddtrace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 |app|
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()
Expand Down
6 changes: 6 additions & 0 deletions lib/ddtrace/contrib/rails/framework.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3e83010

Please sign in to comment.