From 4c88a0d41568f70a8c98bc4edef8324fdde72a57 Mon Sep 17 00:00:00 2001 From: Pedro Lambert Date: Fri, 13 Oct 2017 18:21:04 -0400 Subject: [PATCH 1/2] Migrate `rails` configuration to new API --- lib/ddtrace/contrib/rack/middlewares.rb | 20 ++-- .../contrib/rails/action_controller.rb | 4 +- lib/ddtrace/contrib/rails/action_view.rb | 4 +- lib/ddtrace/contrib/rails/active_record.rb | 4 +- lib/ddtrace/contrib/rails/active_support.rb | 4 +- lib/ddtrace/contrib/rails/framework.rb | 110 +++++++++--------- lib/ddtrace/contrib/rails/middlewares.rb | 2 +- lib/ddtrace/contrib/rails/utils.rb | 2 +- test/contrib/rack/helpers.rb | 1 + test/contrib/rack/middleware_test.rb | 18 ++- test/contrib/rails/apps/application.rb | 7 +- test/contrib/rails/apps/rails3.rb | 7 +- test/contrib/rails/cache_test.rb | 7 +- test/contrib/rails/controller_test.rb | 6 +- test/contrib/rails/database_test.rb | 7 +- test/contrib/rails/default_service_test.rb | 6 +- test/contrib/rails/disable_env_test.rb | 6 +- test/contrib/rails/errors_test.rb | 6 +- test/contrib/rails/rack_middleware_test.rb | 11 +- test/contrib/rails/rails_active_job_test.rb | 8 +- test/contrib/rails/rails_sidekiq_test.rb | 6 +- test/contrib/rails/redis_cache_test.rb | 6 +- test/contrib/rails/tracer_test.rb | 57 ++++----- test/contrib/rails/utils_test.rb | 6 +- test/helper.rb | 12 +- 25 files changed, 167 insertions(+), 160 deletions(-) diff --git a/lib/ddtrace/contrib/rack/middlewares.rb b/lib/ddtrace/contrib/rack/middlewares.rb index 6489de767c6..00c022e5ef8 100644 --- a/lib/ddtrace/contrib/rack/middlewares.rb +++ b/lib/ddtrace/contrib/rack/middlewares.rb @@ -13,20 +13,20 @@ module Rack # application. If request tags are not set by the app, they will be set using # information available at the Rack level. class TraceMiddleware - DEFAULT_CONFIG = { - tracer: Datadog.tracer, - default_service: 'rack', - distributed_tracing_enabled: false - }.freeze + include Base + register_as :rack + + option :tracer, default: Datadog.tracer + option :default_service, default: 'rack' + option :distributed_tracing_enabled, default: false def initialize(app, options = {}) # update options with our configuration, unless it's already available [:tracer, :default_service, :distributed_tracing_enabled].each do |k| - options[k] ||= DEFAULT_CONFIG[k] + Datadog.configuration[:rack][k] = options[k] unless options[k].nil? end @app = app - @options = options end def configure @@ -34,9 +34,9 @@ def configure return clean_context if @tracer && @service # retrieve the current tracer and service - @tracer = @options.fetch(:tracer) - @service = @options.fetch(:default_service) - @distributed_tracing_enabled = @options.fetch(:distributed_tracing_enabled) + @tracer = Datadog.configuration[:rack][:tracer] + @service = Datadog.configuration[:rack][:default_service] + @distributed_tracing_enabled = Datadog.configuration[:rack][:distributed_tracing_enabled] # configure the Rack service @tracer.set_service_info( diff --git a/lib/ddtrace/contrib/rails/action_controller.rb b/lib/ddtrace/contrib/rails/action_controller.rb index e6502c0c9cc..d42bdae5222 100644 --- a/lib/ddtrace/contrib/rails/action_controller.rb +++ b/lib/ddtrace/contrib/rails/action_controller.rb @@ -13,8 +13,8 @@ def self.instrument def self.start_processing(payload) # trace the execution - tracer = ::Rails.configuration.datadog_trace.fetch(:tracer) - service = ::Rails.configuration.datadog_trace.fetch(:default_controller_service) + tracer = Datadog.configuration[:rails][:tracer] + service = Datadog.configuration[:rails][:default_controller_service] type = Datadog::Ext::HTTP::TYPE span = tracer.trace('rails.action_controller', service: service, span_type: type) diff --git a/lib/ddtrace/contrib/rails/action_view.rb b/lib/ddtrace/contrib/rails/action_view.rb index 00f1a4103e7..a8043956b4d 100644 --- a/lib/ddtrace/contrib/rails/action_view.rb +++ b/lib/ddtrace/contrib/rails/action_view.rb @@ -15,7 +15,7 @@ def self.start_render_template(payload) tracing_context = payload.fetch(:tracing_context) # create a new Span and add it to the tracing context - tracer = ::Rails.configuration.datadog_trace.fetch(:tracer) + tracer = Datadog.configuration[:rails][:tracer] span = tracer.trace('rails.render_template', span_type: Datadog::Ext::HTTP::TEMPLATE) tracing_context[:dd_rails_template_span] = span rescue StandardError => e @@ -48,7 +48,7 @@ def self.start_render_partial(payload) # retrieve the tracing context tracing_context = payload.fetch(:tracing_context) - tracer = ::Rails.configuration.datadog_trace.fetch(:tracer) + tracer = Datadog.configuration[:rails][:tracer] span = tracer.trace('rails.render_partial', span_type: Datadog::Ext::HTTP::TEMPLATE) tracing_context[:dd_rails_partial_span] = span rescue StandardError => e diff --git a/lib/ddtrace/contrib/rails/active_record.rb b/lib/ddtrace/contrib/rails/active_record.rb index e29a56e36b4..74fb9958442 100644 --- a/lib/ddtrace/contrib/rails/active_record.rb +++ b/lib/ddtrace/contrib/rails/active_record.rb @@ -18,8 +18,8 @@ def self.instrument end def self.sql(_name, start, finish, _id, payload) - tracer = ::Rails.configuration.datadog_trace.fetch(:tracer) - database_service = ::Rails.configuration.datadog_trace.fetch(:default_database_service) + tracer = Datadog.configuration[:rails][:tracer] + database_service = Datadog.configuration[:rails][:default_database_service] adapter_name = ::ActiveRecord::Base.connection_config[:adapter] adapter_name = Datadog::Contrib::Rails::Utils.normalize_vendor(adapter_name) span_type = Datadog::Ext::SQL::TYPE diff --git a/lib/ddtrace/contrib/rails/active_support.rb b/lib/ddtrace/contrib/rails/active_support.rb index 341ae92e4b7..414205c52c8 100644 --- a/lib/ddtrace/contrib/rails/active_support.rb +++ b/lib/ddtrace/contrib/rails/active_support.rb @@ -12,7 +12,7 @@ def self.instrument end def self.start_trace_cache(payload) - tracer = ::Rails.configuration.datadog_trace.fetch(:tracer) + tracer = Datadog.configuration[:rails][:tracer] tracing_context = payload.fetch(:tracing_context) # In most of the cases Rails ``fetch()`` and ``read()`` calls are nested. @@ -26,7 +26,7 @@ def self.start_trace_cache(payload) payload[:action] == 'GET' # create a new ``Span`` and add it to the tracing context - service = ::Rails.configuration.datadog_trace.fetch(:default_cache_service) + service = Datadog.configuration[:rails][:default_cache_service] type = Datadog::Ext::CACHE::TYPE span = tracer.trace('rails.cache', service: service, span_type: type) span.resource = payload.fetch(:action) diff --git a/lib/ddtrace/contrib/rails/framework.rb b/lib/ddtrace/contrib/rails/framework.rb index 60896da1cbf..a3319713a7a 100644 --- a/lib/ddtrace/contrib/rails/framework.rb +++ b/lib/ddtrace/contrib/rails/framework.rb @@ -30,82 +30,78 @@ module Rails # - handle configuration entries which are specific to Datadog tracing # - instrument parts of the framework when needed module Framework - # default configurations for the Rails integration; by default - # the Datadog.tracer is enabled, while the Rails auto instrumentation - # is kept disabled. - DEFAULT_CONFIG = { - enabled: true, - auto_instrument: false, - auto_instrument_redis: false, - auto_instrument_grape: false, - default_service: 'rails-app', - default_controller_service: 'rails-controller', - default_cache_service: 'rails-cache', - default_grape_service: 'grape', - template_base_path: 'views/', - tracer: Datadog.tracer, - debug: false, - trace_agent_hostname: Datadog::Writer::HOSTNAME, - trace_agent_port: Datadog::Writer::PORT, - env: nil, - tags: {} - }.freeze + include Base + register_as :rails + + option :enabled, default: true + option :auto_instrument, default: false + option :auto_instrument_redis, default: false + option :auto_instrument_grape, default: false + option :default_service, default: 'rails-app' + option :default_controller_service, default: 'rails-controller' + option :default_cache_service, default: 'rails-cache' + option :default_grape_service, default: 'grape' + option :default_database_service + option :distributed_tracing_enabled, default: false + option :template_base_path, default: 'views/' + option :tracer, default: Datadog.tracer + option :debug, default: false + option :trace_agent_hostname, default: Datadog::Writer::HOSTNAME + option :trace_agent_port, default: Datadog::Writer::PORT + option :env, default: nil + option :tags, default: {} # configure Datadog settings # rubocop:disable Metrics/MethodLength - def self.configure(config) - # tracer defaults - # merge default configurations with users settings - user_config = config[:config].datadog_trace rescue {} - datadog_config = DEFAULT_CONFIG.merge(user_config) - datadog_config[:tracer].enabled = datadog_config[:enabled] - - # set debug logging - Datadog::Tracer.debug_logging = datadog_config[:debug] - - # set the address of the trace agent - datadog_config[:tracer].configure( - hostname: datadog_config[:trace_agent_hostname], - port: datadog_config[:trace_agent_port] - ) + def self.configure(rails_config) + user_config = rails_config[:config].datadog_trace rescue {} + Datadog.configuration.use(:rails, user_config) + tracer = Datadog.configuration[:rails][:tracer] + + tracer.enabled = get_option(:enabled) + tracer.class.debug_logging = get_option(:debug) - # set default tracer tags - datadog_config[:tracer].set_tags(datadog_config[:tags]) + tracer.configure( + hostname: get_option(:trace_agent_hostname), + port: get_option(:trace_agent_port) + ) - datadog_config[:tracer].set_tags('env' => datadog_config[:env]) if datadog_config[:env] + tracer.set_tags(get_option(:tags)) + tracer.set_tags('env' => get_option(:env)) if get_option(:env) - # set default service details - datadog_config[:tracer].set_service_info( - datadog_config[:default_service], + tracer.set_service_info( + get_option(:default_service), 'rack', Datadog::Ext::AppTypes::WEB ) - datadog_config[:tracer].set_service_info( - datadog_config[:default_controller_service], + tracer.set_service_info( + get_option(:default_controller_service), 'rails', Datadog::Ext::AppTypes::WEB ) - - datadog_config[:tracer].set_service_info( - datadog_config[:default_cache_service], + tracer.set_service_info( + get_option(:default_cache_service), 'rails', Datadog::Ext::AppTypes::CACHE ) # By default, default service would be guessed from the script # being executed, but here we know better, get it from Rails config. - datadog_config[:tracer].default_service = datadog_config[:default_service] + tracer.default_service = get_option(:default_service) + + Datadog.configuration[:rack][:tracer] = tracer + Datadog.configuration[:rack][:default_service] = get_option(:default_service) + Datadog.configuration[:rack][:distributed_tracing_enabled] = get_option(:distributed_tracing_enabled) if defined?(::ActiveRecord) begin # set default database service details and store it in the configuration conn_cfg = ::ActiveRecord::Base.connection_config() adapter_name = Datadog::Contrib::Rails::Utils.normalize_vendor(conn_cfg[:adapter]) - database_service = datadog_config.fetch(:default_database_service, adapter_name) - datadog_config[:default_database_service] = database_service - datadog_config[:tracer].set_service_info( - database_service, + set_option(:default_database_service, adapter_name) unless get_option(:default_database_service) + tracer.set_service_info( + get_option(:default_database_service), adapter_name, Datadog::Ext::AppTypes::DB ) @@ -115,11 +111,11 @@ def self.configure(config) end # update global configurations - ::Rails.configuration.datadog_trace = datadog_config + ::Rails.configuration.datadog_trace = to_h end def self.auto_instrument_redis - return unless ::Rails.configuration.datadog_trace[:auto_instrument_redis] + return unless get_option(:auto_instrument_redis) Datadog::Tracer.log.debug('Enabling auto-instrumentation for Redis client') # patch the Redis library and reload the CacheStore if it was using Redis @@ -142,7 +138,7 @@ def self.auto_instrument_redis end def self.auto_instrument_grape - return unless ::Rails.configuration.datadog_trace[:auto_instrument_grape] + return unless get_option(:auto_instrument_grape) # patch the Grape library so that endpoints are traced Datadog::Monkey.patch_module(:grape) @@ -150,13 +146,13 @@ def self.auto_instrument_grape # update the Grape pin object pin = Datadog::Pin.get_from(::Grape) return unless pin && pin.enabled? - pin.tracer = ::Rails.configuration.datadog_trace[:tracer] - pin.service = ::Rails.configuration.datadog_trace[:default_grape_service] + pin.tracer = get_option(:tracer) + pin.service = get_option(:default_grape_service) end # automatically instrument all Rails component def self.auto_instrument - return unless ::Rails.configuration.datadog_trace[:auto_instrument] + return unless get_option(:auto_instrument) Datadog::Tracer.log.debug('Enabling auto-instrumentation for core components') # instrumenting Rails framework diff --git a/lib/ddtrace/contrib/rails/middlewares.rb b/lib/ddtrace/contrib/rails/middlewares.rb index 90c09c84d06..d7cda688310 100644 --- a/lib/ddtrace/contrib/rails/middlewares.rb +++ b/lib/ddtrace/contrib/rails/middlewares.rb @@ -21,7 +21,7 @@ def call(env) # It's not a problem since we re-raise it afterwards so for example a # SignalException::Interrupt would still bubble up. rescue Exception => e - tracer = ::Rails.configuration.datadog_trace.fetch(:tracer) + tracer = Datadog.configuration[:rails][:tracer] span = tracer.active_span() span.set_error(e) unless span.nil? raise e diff --git a/lib/ddtrace/contrib/rails/utils.rb b/lib/ddtrace/contrib/rails/utils.rb index 912ba1574e7..d67dac0dd76 100644 --- a/lib/ddtrace/contrib/rails/utils.rb +++ b/lib/ddtrace/contrib/rails/utils.rb @@ -11,7 +11,7 @@ module Utils def self.normalize_template_name(name) return if name.nil? - base_path = ::Rails.configuration.datadog_trace.fetch(:template_base_path, 'views/') + base_path = Datadog.configuration[:rails][:template_base_path] sections_view = name.split(base_path) if sections_view.length == 1 diff --git a/test/contrib/rack/helpers.rb b/test/contrib/rack/helpers.rb index 38657525557..e00ca7aab63 100644 --- a/test/contrib/rack/helpers.rb +++ b/test/contrib/rack/helpers.rb @@ -92,6 +92,7 @@ def app def setup # configure our Middleware with a DummyTracer @tracer = get_test_tracer() + Datadog.configuration[:rack][:default_service] = 'rack' super end end diff --git a/test/contrib/rack/middleware_test.rb b/test/contrib/rack/middleware_test.rb index 528b3fa2fdf..f101c57a761 100644 --- a/test/contrib/rack/middleware_test.rb +++ b/test/contrib/rack/middleware_test.rb @@ -286,19 +286,27 @@ def test_request_middleware_custom_service class RackBaseTest < Minitest::Test def test_middleware_builder_defaults - # by default it should have a Tracer and a service + previous_configuration = Datadog.registry[:rack].to_h + Datadog.registry[:rack].reset_options! + middleware = Datadog::Contrib::Rack::TraceMiddleware.new(proc {}) refute_nil(middleware) - assert_equal(middleware.instance_eval { @options[:tracer] }, Datadog.tracer) - assert_equal(middleware.instance_eval { @options[:default_service] }, 'rack') + assert_equal(Datadog.tracer, Datadog.configuration[:rack][:tracer]) + assert_equal('rack', Datadog.configuration[:rack][:default_service]) + + Datadog.configuration.use(:rack, previous_configuration) end def test_middleware_builder # it should set the tracer and the service + previous_configuration = Datadog.registry[:rack].to_h + tracer = get_test_tracer() middleware = Datadog::Contrib::Rack::TraceMiddleware.new(proc {}, tracer: tracer, default_service: 'custom-rack') refute_nil(middleware) - assert_equal(middleware.instance_eval { @options[:tracer] }, tracer) - assert_equal(middleware.instance_eval { @options[:default_service] }, 'custom-rack') + assert_equal(tracer, Datadog.configuration[:rack][:tracer]) + assert_equal('custom-rack', Datadog.configuration[:rack][:default_service]) + + Datadog.configuration.use(:rack, previous_configuration) end end diff --git a/test/contrib/rails/apps/application.rb b/test/contrib/rails/apps/application.rb index 0b0372230e7..d1155c8008b 100644 --- a/test/contrib/rails/apps/application.rb +++ b/test/contrib/rails/apps/application.rb @@ -41,10 +41,9 @@ def config.database_configuration # the require order is important def test_config # Enables the auto-instrumentation for the testing application - Rails.configuration.datadog_trace = { - auto_instrument: true, - auto_instrument_redis: true - } + Datadog.configure do |c| + c.use :rails, auto_instrument: true, auto_instrument_redis: true + end Rails.application.config.active_job.queue_adapter = :sidekiq # Initialize the Rails application diff --git a/test/contrib/rails/apps/rails3.rb b/test/contrib/rails/apps/rails3.rb index 2f2db07fbfe..1e1b3f1fe45 100644 --- a/test/contrib/rails/apps/rails3.rb +++ b/test/contrib/rails/apps/rails3.rb @@ -16,10 +16,9 @@ class Rails3 < Rails::Application end # Enables the auto-instrumentation for the testing application -Rails.configuration.datadog_trace = { - auto_instrument: true, - auto_instrument_redis: true -} +Datadog.configure do |c| + c.use :rails, auto_instrument: true, auto_instrument_redis: true +end # Initialize the Rails application require 'contrib/rails/apps/controllers' diff --git a/test/contrib/rails/cache_test.rb b/test/contrib/rails/cache_test.rb index 3b2342bac90..98f423c5ed9 100644 --- a/test/contrib/rails/cache_test.rb +++ b/test/contrib/rails/cache_test.rb @@ -3,13 +3,14 @@ class CacheTracingTest < ActionController::TestCase setup do - @original_tracer = Rails.configuration.datadog_trace[:tracer] + @original_tracer = Datadog.configuration[:rails][:tracer] @tracer = get_test_tracer - Rails.configuration.datadog_trace[:tracer] = @tracer + Datadog.configuration[:rails][:default_cache_service] = 'rails-cache' + Datadog.configuration[:rails][:tracer] = @tracer end teardown do - Rails.configuration.datadog_trace[:tracer] = @original_tracer + Datadog.configuration[:rails][:tracer] = @original_tracer end test 'cache.read() is properly traced' do diff --git a/test/contrib/rails/controller_test.rb b/test/contrib/rails/controller_test.rb index 5baaa33e5d7..fd54785607f 100644 --- a/test/contrib/rails/controller_test.rb +++ b/test/contrib/rails/controller_test.rb @@ -4,13 +4,13 @@ class TracingControllerTest < ActionController::TestCase setup do - @original_tracer = Rails.configuration.datadog_trace[:tracer] + @original_tracer = Datadog.configuration[:rails][:tracer] @tracer = get_test_tracer - Rails.configuration.datadog_trace[:tracer] = @tracer + Datadog.configuration[:rails][:tracer] = @tracer end teardown do - Rails.configuration.datadog_trace[:tracer] = @original_tracer + Datadog.configuration[:rails][:tracer] = @original_tracer end test 'request is properly traced' do diff --git a/test/contrib/rails/database_test.rb b/test/contrib/rails/database_test.rb index b83e45b5be4..144dd2f46db 100644 --- a/test/contrib/rails/database_test.rb +++ b/test/contrib/rails/database_test.rb @@ -3,13 +3,14 @@ class DatabaseTracingTest < ActiveSupport::TestCase setup do - @original_tracer = Rails.configuration.datadog_trace[:tracer] + @original_tracer = Datadog.configuration[:rails][:tracer] @tracer = get_test_tracer - Rails.configuration.datadog_trace[:tracer] = @tracer + Datadog.configuration[:rails][:default_database_service] = get_adapter_name + Datadog.configuration[:rails][:tracer] = @tracer end teardown do - Rails.configuration.datadog_trace[:tracer] = @original_tracer + Datadog.configuration[:rails][:tracer] = @original_tracer end test 'active record is properly traced' do diff --git a/test/contrib/rails/default_service_test.rb b/test/contrib/rails/default_service_test.rb index 189b28640e7..0c91fd64328 100644 --- a/test/contrib/rails/default_service_test.rb +++ b/test/contrib/rails/default_service_test.rb @@ -4,14 +4,14 @@ class TracingDefaultServiceTest < ActionController::TestCase setup do - @original_tracer = Rails.configuration.datadog_trace[:tracer] + @original_tracer = Datadog.configuration[:rails][:tracer] @tracer = get_test_tracer - Rails.configuration.datadog_trace[:tracer] = @tracer + Datadog.configuration[:rails][:tracer] = @tracer update_config(:tracer, @tracer) end teardown do - Rails.configuration.datadog_trace[:tracer] = @original_tracer + Datadog.configuration[:rails][:tracer] = @original_tracer end test 'test that a lone span will have rails service picked up' do diff --git a/test/contrib/rails/disable_env_test.rb b/test/contrib/rails/disable_env_test.rb index 0165df0696a..cd767c4c084 100644 --- a/test/contrib/rails/disable_env_test.rb +++ b/test/contrib/rails/disable_env_test.rb @@ -7,13 +7,13 @@ class TracingControllerTest < ActionController::TestCase setup do - @original_tracer = Rails.configuration.datadog_trace[:tracer] + @original_tracer = Datadog.configuration[:rails][:tracer] @tracer = get_test_tracer - Rails.configuration.datadog_trace[:tracer] = @tracer + Datadog.configuration[:rails][:tracer] = @tracer end teardown do - Rails.configuration.datadog_trace[:tracer] = @original_tracer + Datadog.configuration[:rails][:tracer] = @original_tracer end test 'rails must not be instrumented' do diff --git a/test/contrib/rails/errors_test.rb b/test/contrib/rails/errors_test.rb index 04bfc9499ad..0f5045bdb9d 100644 --- a/test/contrib/rails/errors_test.rb +++ b/test/contrib/rails/errors_test.rb @@ -5,13 +5,13 @@ # rubocop:disable Metrics/ClassLength class TracingControllerTest < ActionController::TestCase setup do - @original_tracer = Rails.configuration.datadog_trace[:tracer] + @original_tracer = Datadog.configuration[:rails][:tracer] @tracer = get_test_tracer - Rails.configuration.datadog_trace[:tracer] = @tracer + Datadog.configuration[:rails][:tracer] = @tracer end teardown do - Rails.configuration.datadog_trace[:tracer] = @original_tracer + Datadog.configuration[:rails][:tracer] = @original_tracer end test 'error in the controller must be traced' do diff --git a/test/contrib/rails/rack_middleware_test.rb b/test/contrib/rails/rack_middleware_test.rb index bdcfa71366f..5b18d6c8587 100644 --- a/test/contrib/rails/rack_middleware_test.rb +++ b/test/contrib/rails/rack_middleware_test.rb @@ -5,21 +5,22 @@ class FullStackTest < ActionDispatch::IntegrationTest setup do # store original tracers - Rails.application.app.configure() - @rails_tracer = Rails.configuration.datadog_trace[:tracer] + @rails_tracer = Datadog.configuration[:rails][:tracer] @rack_tracer = Rails.application.app.instance_variable_get :@tracer # replace the Rails and the Rack tracer with a dummy one; # this prevents the overhead to reinitialize the Rails application # and the Rack stack @tracer = get_test_tracer - Rails.configuration.datadog_trace[:tracer] = @tracer - Rails.application.app.instance_variable_set(:@tracer, @tracer) + Datadog.registry[:rails].reset_options! + Datadog.configuration[:rails][:default_database_service] = get_adapter_name + Datadog.configuration[:rails][:tracer] = @tracer + Datadog.configuration[:rack][:tracer] = @tracer end teardown do # restore original tracers - Rails.configuration.datadog_trace[:tracer] = @rails_tracer + Datadog.configuration[:rails][:tracer] = @rails_tracer Rails.application.app.instance_variable_set(:@tracer, @rack_tracer) end diff --git a/test/contrib/rails/rails_active_job_test.rb b/test/contrib/rails/rails_active_job_test.rb index 99f01fc84b6..d4bc1e93cc8 100644 --- a/test/contrib/rails/rails_active_job_test.rb +++ b/test/contrib/rails/rails_active_job_test.rb @@ -13,13 +13,13 @@ class RailsActiveJobTest < ActionController::TestCase setup do # don't pollute the global tracer - @original_tracer = Rails.configuration.datadog_trace[:tracer] + @original_tracer = Datadog.configuration[:rails][:tracer] @original_writer = @original_tracer.writer @tracer = get_test_tracer() Datadog.tracer.writer = @tracer.writer - Rails.configuration.datadog_trace[:tracer] = @tracer + Datadog.configuration[:rails][:tracer] = @tracer # configure Sidekiq Sidekiq.configure_client do |config| @@ -34,8 +34,8 @@ class RailsActiveJobTest < ActionController::TestCase end teardown do - Rails.configuration.datadog_trace[:tracer] = @original_tracer - Rails.configuration.datadog_trace[:tracer].writer = @original_writer + Datadog.configuration[:rails][:tracer] = @original_tracer + Datadog.configuration[:rails][:tracer].writer = @original_writer end # Sidekiq test job diff --git a/test/contrib/rails/rails_sidekiq_test.rb b/test/contrib/rails/rails_sidekiq_test.rb index 6bd0310ca18..05a437d0490 100644 --- a/test/contrib/rails/rails_sidekiq_test.rb +++ b/test/contrib/rails/rails_sidekiq_test.rb @@ -9,9 +9,9 @@ class RailsSidekiqTest < ActionController::TestCase setup do # don't pollute the global tracer - @original_tracer = Rails.configuration.datadog_trace[:tracer] + @original_tracer = Datadog.configuration[:rails][:tracer] @tracer = get_test_tracer() - Rails.configuration.datadog_trace[:tracer] = @tracer + Datadog.configuration[:rails][:tracer] = @tracer # configure Sidekiq Sidekiq.configure_client do |config| @@ -26,7 +26,7 @@ class RailsSidekiqTest < ActionController::TestCase end teardown do - Rails.configuration.datadog_trace[:tracer] = @original_tracer + Datadog.configuration[:rails][:tracer] = @original_tracer end # Sidekiq test job diff --git a/test/contrib/rails/redis_cache_test.rb b/test/contrib/rails/redis_cache_test.rb index 79153fc8825..15ef756d4ca 100644 --- a/test/contrib/rails/redis_cache_test.rb +++ b/test/contrib/rails/redis_cache_test.rb @@ -12,9 +12,9 @@ class RedisCacheTracingTest < ActionController::TestCase setup do # switch Rails with a dummy tracer - @original_tracer = Rails.configuration.datadog_trace[:tracer] + @original_tracer = Datadog.configuration[:rails][:tracer] @tracer = get_test_tracer() - Rails.configuration.datadog_trace[:tracer] = @tracer + Datadog.configuration[:rails][:tracer] = @tracer # get the Redis pin accessing private methods (only Rails 3.x) client = Rails.cache.instance_variable_get(:@data) @@ -24,7 +24,7 @@ class RedisCacheTracingTest < ActionController::TestCase end teardown do - Rails.configuration.datadog_trace[:tracer] = @original_tracer + Datadog.configuration[:rails][:tracer] = @original_tracer end test 'cache.read() and cache.fetch() are properly traced' do diff --git a/test/contrib/rails/tracer_test.rb b/test/contrib/rails/tracer_test.rb index d82a91550ec..bcb1892430a 100644 --- a/test/contrib/rails/tracer_test.rb +++ b/test/contrib/rails/tracer_test.rb @@ -7,7 +7,9 @@ class TracerTest < ActionDispatch::IntegrationTest setup do # don't pollute the global tracer @tracer = get_test_tracer - Rails.configuration.datadog_trace[:tracer] = @tracer + Datadog.registry[:rails].reset_options! + Datadog.configuration[:rails][:default_database_service] = get_adapter_name + Datadog.configuration[:rails][:tracer] = @tracer end teardown do @@ -15,25 +17,26 @@ class TracerTest < ActionDispatch::IntegrationTest end test 'the configuration is correctly called' do - assert Rails.configuration.datadog_trace[:enabled] - assert Rails.configuration.datadog_trace[:auto_instrument] - assert Rails.configuration.datadog_trace[:auto_instrument_redis] - assert_equal(Rails.configuration.datadog_trace[:default_service], 'rails-app') - assert_equal(Rails.configuration.datadog_trace[:default_controller_service], 'rails-controller') - assert_equal(Rails.configuration.datadog_trace[:default_cache_service], 'rails-cache') - refute_nil(Rails.configuration.datadog_trace[:default_database_service]) - assert_equal(Rails.configuration.datadog_trace[:template_base_path], 'views/') - assert Rails.configuration.datadog_trace[:tracer] - assert !Rails.configuration.datadog_trace[:debug] - assert_equal(Rails.configuration.datadog_trace[:trace_agent_hostname], Datadog::Writer::HOSTNAME) - assert_equal(Rails.configuration.datadog_trace[:trace_agent_port], Datadog::Writer::PORT) - assert_nil(Rails.configuration.datadog_trace[:env], 'no env should be set by default') - assert_equal(Rails.configuration.datadog_trace[:tags], {}, 'no tags should be set by default') + assert Datadog.configuration[:rails][:enabled] + refute Datadog.configuration[:rails][:auto_instrument] + refute Datadog.configuration[:rails][:auto_instrument_redis] + assert_equal(Datadog.configuration[:rails][:default_service], 'rails-app') + assert_equal(Datadog.configuration[:rails][:default_controller_service], 'rails-controller') + assert_equal(Datadog.configuration[:rails][:default_cache_service], 'rails-cache') + refute_nil(Datadog.configuration[:rails][:default_database_service]) + assert_equal(Datadog.configuration[:rails][:template_base_path], 'views/') + assert Datadog.configuration[:rails][:tracer] + assert !Datadog.configuration[:rails][:debug] + assert_equal(Datadog.configuration[:rails][:trace_agent_hostname], Datadog::Writer::HOSTNAME) + assert_equal(Datadog.configuration[:rails][:trace_agent_port], Datadog::Writer::PORT) + assert_nil(Datadog.configuration[:rails][:env], 'no env should be set by default') + assert_equal(Datadog.configuration[:rails][:tags], {}, 'no tags should be set by default') end test 'a default service and database should be properly set' do + update_config(:default_cache_service, 'rails-cache') reset_config() - services = Datadog.tracer.services + services = Datadog.configuration[:rails][:tracer].services adapter_name = get_adapter_name() assert_equal( services, @@ -54,7 +57,7 @@ class TracerTest < ActionDispatch::IntegrationTest test 'database service can be changed by user' do update_config(:default_database_service, 'customer-db') - tracer = Rails.configuration.datadog_trace[:tracer] + tracer = Datadog.configuration[:rails][:tracer] adapter_name = get_adapter_name() assert_equal( @@ -76,7 +79,7 @@ class TracerTest < ActionDispatch::IntegrationTest test 'application service can be changed by user' do update_config(:default_controller_service, 'my-custom-app') - tracer = Rails.configuration.datadog_trace[:tracer] + tracer = Datadog.configuration[:rails][:tracer] adapter_name = get_adapter_name() assert_equal( @@ -98,7 +101,7 @@ class TracerTest < ActionDispatch::IntegrationTest test 'cache service can be changed by user' do update_config(:default_cache_service, 'service-cache') - tracer = Rails.configuration.datadog_trace[:tracer] + tracer = Datadog.configuration[:rails][:tracer] adapter_name = get_adapter_name() assert_equal( @@ -128,7 +131,7 @@ class TracerTest < ActionDispatch::IntegrationTest update_config(:trace_agent_hostname, 'example.com') update_config(:trace_agent_port, 42) - tracer = Rails.configuration.datadog_trace[:tracer] + tracer = Datadog.configuration[:rails][:tracer] assert_equal(tracer.writer.transport.hostname, 'example.com') assert_equal(tracer.writer.transport.port, 42) @@ -137,7 +140,7 @@ class TracerTest < ActionDispatch::IntegrationTest test 'tracer environment can be changed by the user' do update_config(:env, 'dev') - tracer = Rails.configuration.datadog_trace[:tracer] + tracer = Datadog.configuration[:rails][:tracer] assert_equal(tracer.tags['env'], 'dev') end @@ -145,7 +148,7 @@ class TracerTest < ActionDispatch::IntegrationTest test 'tracer global tags can be changed by the user' do update_config(:tags, 'component' => 'api', 'section' => 'users') - tracer = Rails.configuration.datadog_trace[:tracer] + tracer = Datadog.configuration[:rails][:tracer] assert_equal(tracer.tags['component'], 'api') assert_equal(tracer.tags['section'], 'users') @@ -153,27 +156,25 @@ class TracerTest < ActionDispatch::IntegrationTest test 'tracer env and env tag setting precedence' do # default case - tracer = Rails.configuration.datadog_trace[:tracer] + tracer = Datadog.configuration[:rails][:tracer] assert_nil(tracer.tags['env']) # use the Rails value update_config(:env, ::Rails.env) update_config(:tags, 'env' => 'foo') - tracer = Rails.configuration.datadog_trace[:tracer] + tracer = Datadog.configuration[:rails][:tracer] assert_equal(tracer.tags['env'], 'test') # explicit set - update_config(:use_rails_env, false) update_config(:env, 'dev') update_config(:tags, 'env' => 'bar') - tracer = Rails.configuration.datadog_trace[:tracer] + tracer = Datadog.configuration[:rails][:tracer] assert_equal(tracer.tags['env'], 'dev') # env is not valid but tags is set - update_config(:use_rails_env, false) update_config(:env, nil) update_config(:tags, 'env' => 'bar') - tracer = Rails.configuration.datadog_trace[:tracer] + tracer = Datadog.configuration[:rails][:tracer] assert_equal(tracer.tags['env'], 'bar') end end diff --git a/test/contrib/rails/utils_test.rb b/test/contrib/rails/utils_test.rb index 45ba3609b3c..4d9eec2c37f 100644 --- a/test/contrib/rails/utils_test.rb +++ b/test/contrib/rails/utils_test.rb @@ -4,11 +4,11 @@ class UtilsTest < ActiveSupport::TestCase setup do - @default_base_template = ::Rails.configuration.datadog_trace.fetch(:template_base_path) + @default_base_template = Datadog.configuration[:rails][:template_base_path] end teardown do - ::Rails.configuration.datadog_trace[:template_base_path] = @default_base_template + Datadog.configuration[:rails][:template_base_path] = @default_base_template end test 'normalize_template_name' do @@ -35,7 +35,7 @@ class UtilsTest < ActiveSupport::TestCase end test 'normalize_template_name_with_custom_prefix' do - ::Rails.configuration.datadog_trace[:template_base_path] = 'custom/' + Datadog.configuration[:rails][:template_base_path] = 'custom/' full_template_name = '/opt/rails/app/custom/welcome/index.html.erb' template_name = Datadog::Contrib::Rails::Utils.normalize_template_name(full_template_name) assert_equal(template_name, 'welcome/index.html.erb') diff --git a/test/helper.rb b/test/helper.rb index 5e86160c36e..ef9806dd886 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -196,7 +196,8 @@ class HTTPTransport # * +key+: the key that should be updated # * +value+: the value of the key def update_config(key, value) - ::Rails.configuration.datadog_trace[key] = value + Datadog.configuration[:rails][key] = value + ::Rails.configuration.datadog_trace.merge!(Datadog.registry[:rails].to_h) config = { config: ::Rails.application.config } Datadog::Contrib::Rails::Framework.configure(config) end @@ -204,13 +205,12 @@ def update_config(key, value) # reset default configuration and replace any dummy tracer # with the global one def reset_config - ::Rails.configuration.datadog_trace = { - auto_instrument: true, - auto_instrument_redis: true - } + Datadog.configure do |c| + c.use :rails, auto_instrument: true, auto_instrument_redis: true + end config = { config: ::Rails.application.config } - Datadog::Contrib::Rails::Framework.configure(config) + Datadog.registry[:rails].configure(config) end def test_repeat From cac9773e43c3e63d42420d2a3ee92081e7191899 Mon Sep 17 00:00:00 2001 From: Pedro Lambert Date: Mon, 16 Oct 2017 11:53:58 -0400 Subject: [PATCH 2/2] Turn `rails` into regular contribution --- lib/ddtrace.rb | 38 ---------------- lib/ddtrace/contrib/rails/framework.rb | 63 +++++++++----------------- lib/ddtrace/contrib/rails/patcher.rb | 55 ++++++++++++++++++++++ lib/ddtrace/contrib/rails/railtie.rb | 18 ++++++++ lib/ddtrace/contrib/sidekiq/tracer.rb | 3 +- lib/ddtrace/monkey.rb | 3 ++ test/helper.rb | 2 +- test/monkey_test.rb | 13 +++--- 8 files changed, 107 insertions(+), 88 deletions(-) create mode 100644 lib/ddtrace/contrib/rails/patcher.rb create mode 100644 lib/ddtrace/contrib/rails/railtie.rb diff --git a/lib/ddtrace.rb b/lib/ddtrace.rb index 20f34d21905..71fc865c37e 100644 --- a/lib/ddtrace.rb +++ b/lib/ddtrace.rb @@ -50,41 +50,3 @@ def configure # the load order, by letting things be lazily loaded while keeping # thread-safety. require 'ddtrace/monkey' - -# Datadog auto instrumentation for frameworks -if defined?(Rails::VERSION) - if !ENV['DISABLE_DATADOG_RAILS'] - if Rails::VERSION::MAJOR.to_i >= 3 - require 'ddtrace/contrib/rails/framework' - require 'ddtrace/contrib/rails/middlewares' - - module Datadog - # Railtie class initializes - class Railtie < Rails::Railtie - # add instrumentation middlewares - options = {} - config.app_middleware.insert_before(0, Datadog::Contrib::Rack::TraceMiddleware, options) - config.app_middleware.use(Datadog::Contrib::Rails::ExceptionMiddleware) - - # auto instrument Rails and third party components after - # the framework initialization - config.after_initialize do |app| - Datadog::Contrib::Rails::Framework.configure(config: app.config) - Datadog::Contrib::Rails::Framework.auto_instrument() - Datadog::Contrib::Rails::Framework.auto_instrument_redis() - Datadog::Contrib::Rails::Framework.auto_instrument_grape() - - # override Rack Middleware configurations with Rails - options.update(::Rails.configuration.datadog_trace) - end - end - end - else - Datadog::Tracer.log.warn 'Detected a Rails version < 3.x.'\ - 'This version is not supported yet and the'\ - 'auto-instrumentation for core components will be disabled.' - end - else - Datadog::Tracer.log.info 'Skipping Rails auto-instrumentation, DISABLE_DATADOG_RAILS is set.' - end -end diff --git a/lib/ddtrace/contrib/rails/framework.rb b/lib/ddtrace/contrib/rails/framework.rb index a3319713a7a..e861c72d796 100644 --- a/lib/ddtrace/contrib/rails/framework.rb +++ b/lib/ddtrace/contrib/rails/framework.rb @@ -30,78 +30,59 @@ module Rails # - handle configuration entries which are specific to Datadog tracing # - instrument parts of the framework when needed module Framework - include Base - register_as :rails - - option :enabled, default: true - option :auto_instrument, default: false - option :auto_instrument_redis, default: false - option :auto_instrument_grape, default: false - option :default_service, default: 'rails-app' - option :default_controller_service, default: 'rails-controller' - option :default_cache_service, default: 'rails-cache' - option :default_grape_service, default: 'grape' - option :default_database_service - option :distributed_tracing_enabled, default: false - option :template_base_path, default: 'views/' - option :tracer, default: Datadog.tracer - option :debug, default: false - option :trace_agent_hostname, default: Datadog::Writer::HOSTNAME - option :trace_agent_port, default: Datadog::Writer::PORT - option :env, default: nil - option :tags, default: {} - # configure Datadog settings # rubocop:disable Metrics/MethodLength + # rubocop:disable Metrics/AbcSize def self.configure(rails_config) user_config = rails_config[:config].datadog_trace rescue {} Datadog.configuration.use(:rails, user_config) tracer = Datadog.configuration[:rails][:tracer] - tracer.enabled = get_option(:enabled) - tracer.class.debug_logging = get_option(:debug) + tracer.enabled = Datadog.configuration[:rails][:enabled] + tracer.class.debug_logging = Datadog.configuration[:rails][:debug] tracer.configure( - hostname: get_option(:trace_agent_hostname), - port: get_option(:trace_agent_port) + hostname: Datadog.configuration[:rails][:trace_agent_hostname], + port: Datadog.configuration[:rails][:trace_agent_port] ) - tracer.set_tags(get_option(:tags)) - tracer.set_tags('env' => get_option(:env)) if get_option(:env) + tracer.set_tags(Datadog.configuration[:rails][:tags]) + tracer.set_tags('env' => Datadog.configuration[:rails][:env]) if Datadog.configuration[:rails][:env] tracer.set_service_info( - get_option(:default_service), + Datadog.configuration[:rails][:default_service], 'rack', Datadog::Ext::AppTypes::WEB ) tracer.set_service_info( - get_option(:default_controller_service), + Datadog.configuration[:rails][:default_controller_service], 'rails', Datadog::Ext::AppTypes::WEB ) tracer.set_service_info( - get_option(:default_cache_service), + Datadog.configuration[:rails][:default_cache_service], 'rails', Datadog::Ext::AppTypes::CACHE ) # By default, default service would be guessed from the script # being executed, but here we know better, get it from Rails config. - tracer.default_service = get_option(:default_service) + tracer.default_service = Datadog.configuration[:rails][:default_service] Datadog.configuration[:rack][:tracer] = tracer - Datadog.configuration[:rack][:default_service] = get_option(:default_service) - Datadog.configuration[:rack][:distributed_tracing_enabled] = get_option(:distributed_tracing_enabled) + Datadog.configuration[:rack][:default_service] = Datadog.configuration[:rails][:default_service] + Datadog.configuration[:rack][:distributed_tracing_enabled] = \ + Datadog.configuration[:rails][:distributed_tracing_enabled] if defined?(::ActiveRecord) begin # set default database service details and store it in the configuration conn_cfg = ::ActiveRecord::Base.connection_config() adapter_name = Datadog::Contrib::Rails::Utils.normalize_vendor(conn_cfg[:adapter]) - set_option(:default_database_service, adapter_name) unless get_option(:default_database_service) + Datadog.configuration[:rails][:default_database_service] ||= adapter_name tracer.set_service_info( - get_option(:default_database_service), + Datadog.configuration[:rails][:default_database_service], adapter_name, Datadog::Ext::AppTypes::DB ) @@ -111,11 +92,11 @@ def self.configure(rails_config) end # update global configurations - ::Rails.configuration.datadog_trace = to_h + ::Rails.configuration.datadog_trace = Datadog.registry[:rails].to_h end def self.auto_instrument_redis - return unless get_option(:auto_instrument_redis) + return unless Datadog.configuration[:rails][:auto_instrument_redis] Datadog::Tracer.log.debug('Enabling auto-instrumentation for Redis client') # patch the Redis library and reload the CacheStore if it was using Redis @@ -138,7 +119,7 @@ def self.auto_instrument_redis end def self.auto_instrument_grape - return unless get_option(:auto_instrument_grape) + return unless Datadog.configuration[:rails][:auto_instrument_grape] # patch the Grape library so that endpoints are traced Datadog::Monkey.patch_module(:grape) @@ -146,13 +127,13 @@ def self.auto_instrument_grape # update the Grape pin object pin = Datadog::Pin.get_from(::Grape) return unless pin && pin.enabled? - pin.tracer = get_option(:tracer) - pin.service = get_option(:default_grape_service) + pin.tracer = Datadog.configuration[:rails][:tracer] + pin.service = Datadog.configuration[:rails][:default_grape_service] end # automatically instrument all Rails component def self.auto_instrument - return unless get_option(:auto_instrument) + return unless Datadog.configuration[:rails][:auto_instrument] Datadog::Tracer.log.debug('Enabling auto-instrumentation for core components') # instrumenting Rails framework diff --git a/lib/ddtrace/contrib/rails/patcher.rb b/lib/ddtrace/contrib/rails/patcher.rb new file mode 100644 index 00000000000..290995ce9a3 --- /dev/null +++ b/lib/ddtrace/contrib/rails/patcher.rb @@ -0,0 +1,55 @@ +module Datadog + module Contrib + module Rails + # Patcher + module Patcher + include Base + register_as :rails, auto_patch: true + + option :enabled, default: true + option :auto_instrument, default: false + option :auto_instrument_redis, default: false + option :auto_instrument_grape, default: false + option :default_service, default: 'rails-app' + option :default_controller_service, default: 'rails-controller' + option :default_cache_service, default: 'rails-cache' + option :default_grape_service, default: 'grape' + option :default_database_service + option :distributed_tracing_enabled, default: false + option :template_base_path, default: 'views/' + option :tracer, default: Datadog.tracer + option :debug, default: false + option :trace_agent_hostname, default: Datadog::Writer::HOSTNAME + option :trace_agent_port, default: Datadog::Writer::PORT + option :env, default: nil + option :tags, default: {} + option :sidekiq_service, default: 'sidekiq' + + @patched = false + + class << self + def patch + return @patched if patched? || !compatible? + require_relative 'framework' + @patched = true + rescue => e + Datadog::Tracer.log.error("Unable to apply Rails integration: #{e}") + @patched + end + + def patched? + @patched + end + + def compatible? + return if ENV['DISABLE_DATADOG_RAILS'] + + defined?(::Rails::VERSION) && ::Rails::VERSION::MAJOR.to_i >= 3 + end + end + end + end + end +end + +require 'ddtrace/contrib/rails/railtie' if Datadog.registry[:rails].compatible? diff --git a/lib/ddtrace/contrib/rails/railtie.rb b/lib/ddtrace/contrib/rails/railtie.rb new file mode 100644 index 00000000000..aa14da3cfc3 --- /dev/null +++ b/lib/ddtrace/contrib/rails/railtie.rb @@ -0,0 +1,18 @@ +require 'ddtrace/contrib/rails/framework' +require 'ddtrace/contrib/rails/middlewares' +require 'ddtrace/contrib/rack/middlewares' + +module Datadog + # Railtie class initializes + class Railtie < Rails::Railtie + config.app_middleware.insert_before(0, Datadog::Contrib::Rack::TraceMiddleware) + config.app_middleware.use(Datadog::Contrib::Rails::ExceptionMiddleware) + + config.after_initialize do |app| + Datadog::Contrib::Rails::Framework.configure(config: app.config) + Datadog::Contrib::Rails::Framework.auto_instrument + Datadog::Contrib::Rails::Framework.auto_instrument_redis + Datadog::Contrib::Rails::Framework.auto_instrument_grape + end + end +end diff --git a/lib/ddtrace/contrib/sidekiq/tracer.rb b/lib/ddtrace/contrib/sidekiq/tracer.rb index 35c2e7d7857..b93c7b585c9 100644 --- a/lib/ddtrace/contrib/sidekiq/tracer.rb +++ b/lib/ddtrace/contrib/sidekiq/tracer.rb @@ -29,8 +29,7 @@ class Tracer def initialize(options = {}) # check if Rails configuration is available and use it to override # Sidekiq defaults - rails_config = ::Rails.configuration.datadog_trace rescue {} - base_config = Datadog.configuration[:sidekiq].merge(rails_config) + base_config = Datadog.configuration[:sidekiq].merge(Datadog.configuration[:rails]) user_config = base_config.merge(options) @tracer = user_config[:tracer] @sidekiq_service = user_config[:sidekiq_service] diff --git a/lib/ddtrace/monkey.rb b/lib/ddtrace/monkey.rb index c2e5daba7f3..9e9aa634208 100644 --- a/lib/ddtrace/monkey.rb +++ b/lib/ddtrace/monkey.rb @@ -4,6 +4,7 @@ # because patchers do not include any 3rd party module nor even our # patching code, which is required on demand, when patching. require 'ddtrace/contrib/base' +require 'ddtrace/contrib/rails/patcher' require 'ddtrace/contrib/active_record/patcher' require 'ddtrace/contrib/elasticsearch/patcher' require 'ddtrace/contrib/faraday/patcher' @@ -80,3 +81,5 @@ class << self end end end + +Datadog::Monkey.patch_module(:rails) diff --git a/test/helper.rb b/test/helper.rb index ef9806dd886..3a66268631e 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -210,7 +210,7 @@ def reset_config end config = { config: ::Rails.application.config } - Datadog.registry[:rails].configure(config) + Datadog::Contrib::Rails::Framework.configure(config) end def test_repeat diff --git a/test/monkey_test.rb b/test/monkey_test.rb index 69773f08893..39130e56418 100644 --- a/test/monkey_test.rb +++ b/test/monkey_test.rb @@ -13,6 +13,7 @@ class MonkeyTest < Minitest::Test def test_autopatch_modules expected = { + rails: true, elasticsearch: true, http: true, redis: true, @@ -41,7 +42,7 @@ def test_patch_module assert_equal(false, Datadog::Contrib::Grape::Patcher.patched?) assert_equal(false, Datadog::Contrib::Aws::Patcher.patched?) assert_equal(false, Datadog::Contrib::ActiveRecord::Patcher.patched?) - assert_equal({ elasticsearch: false, http: false, redis: false, grape: false, faraday: false, aws: false, sucker_punch: false, active_record: false, mongo: false, dalli: false, resque: false }, Datadog::Monkey.get_patched_modules()) + assert_equal({ rails: false, elasticsearch: false, http: false, redis: false, grape: false, faraday: false, aws: false, sucker_punch: false, active_record: false, mongo: false, dalli: false, resque: false }, Datadog::Monkey.get_patched_modules()) Datadog::Monkey.patch_module(:redis) assert_equal(false, Datadog::Contrib::Elasticsearch::Patcher.patched?) @@ -51,7 +52,7 @@ def test_patch_module assert_equal(false, Datadog::Contrib::Aws::Patcher.patched?) assert_equal(false, Datadog::Contrib::ActiveRecord::Patcher.patched?) refute(Datadog::Contrib::Faraday::Patcher.patched?) - assert_equal({ elasticsearch: false, http: false, redis: true, grape: false, faraday: false, aws: false, sucker_punch: false, active_record: false, mongo: false, dalli: false, resque: false }, Datadog::Monkey.get_patched_modules()) + assert_equal({ rails: false, elasticsearch: false, http: false, redis: true, grape: false, faraday: false, aws: false, sucker_punch: false, active_record: false, mongo: false, dalli: false, resque: false }, Datadog::Monkey.get_patched_modules()) # now do it again to check it's idempotent Datadog::Monkey.patch_module(:redis) @@ -62,7 +63,7 @@ def test_patch_module assert_equal(false, Datadog::Contrib::Aws::Patcher.patched?) assert_equal(false, Datadog::Contrib::ActiveRecord::Patcher.patched?) refute(Datadog::Contrib::Faraday::Patcher.patched?) - assert_equal({ elasticsearch: false, http: false, redis: true, grape: false, faraday: false, aws: false, sucker_punch: false, active_record: false, mongo: false, dalli: false, resque: false }, Datadog::Monkey.get_patched_modules()) + assert_equal({ rails: false, elasticsearch: false, http: false, redis: true, grape: false, faraday: false, aws: false, sucker_punch: false, active_record: false, mongo: false, dalli: false, resque: false }, Datadog::Monkey.get_patched_modules()) Datadog::Monkey.patch(elasticsearch: true, redis: true) assert_equal(true, Datadog::Contrib::Elasticsearch::Patcher.patched?) @@ -71,7 +72,7 @@ def test_patch_module assert_equal(false, Datadog::Contrib::Grape::Patcher.patched?) assert_equal(false, Datadog::Contrib::Aws::Patcher.patched?) assert_equal(false, Datadog::Contrib::ActiveRecord::Patcher.patched?) - assert_equal({ elasticsearch: true, http: false, redis: true, grape: false, faraday: false, aws: false, sucker_punch: false, active_record: false, mongo: false, dalli: false, resque: false }, Datadog::Monkey.get_patched_modules()) + assert_equal({ rails: false, elasticsearch: true, http: false, redis: true, grape: false, faraday: false, aws: false, sucker_punch: false, active_record: false, mongo: false, dalli: false, resque: false }, Datadog::Monkey.get_patched_modules()) # verify that active_record is not auto patched by default Datadog::Monkey.patch_all() @@ -81,7 +82,7 @@ def test_patch_module assert_equal(false, Datadog::Contrib::Grape::Patcher.patched?) assert_equal(true, Datadog::Contrib::Aws::Patcher.patched?) assert_equal(false, Datadog::Contrib::ActiveRecord::Patcher.patched?) - assert_equal({ elasticsearch: true, http: true, redis: true, grape: false, faraday: true, aws: true, sucker_punch: true, active_record: false, mongo: false, dalli: true, resque: true }, Datadog::Monkey.get_patched_modules()) + assert_equal({ rails: false, elasticsearch: true, http: true, redis: true, grape: false, faraday: true, aws: true, sucker_punch: true, active_record: false, mongo: false, dalli: true, resque: true }, Datadog::Monkey.get_patched_modules()) Datadog::Monkey.patch_module(:active_record) assert_equal(true, Datadog::Contrib::Elasticsearch::Patcher.patched?) @@ -90,6 +91,6 @@ def test_patch_module assert_equal(false, Datadog::Contrib::Grape::Patcher.patched?) assert_equal(true, Datadog::Contrib::Aws::Patcher.patched?) assert_equal(true, Datadog::Contrib::ActiveRecord::Patcher.patched?) - assert_equal({ elasticsearch: true, http: true, redis: true, grape: false, faraday: true, aws: true, sucker_punch: true, active_record: true, mongo: false, dalli: true, resque: true }, Datadog::Monkey.get_patched_modules()) + assert_equal({ rails: false, elasticsearch: true, http: true, redis: true, grape: false, faraday: true, aws: true, sucker_punch: true, active_record: true, mongo: false, dalli: true, resque: true }, Datadog::Monkey.get_patched_modules()) end end