From a8860c5644ca7a3fe24396990b98eb0e312e39ea Mon Sep 17 00:00:00 2001 From: David Elner Date: Mon, 6 May 2019 11:28:43 -0400 Subject: [PATCH] Changed: reload_cache_store in Rails instead of ActiveSupport. --- lib/ddtrace/contrib/active_support/patcher.rb | 22 ------------------- lib/ddtrace/contrib/rails/framework.rb | 22 +++++++++++++++++++ lib/ddtrace/contrib/redis/patcher.rb | 6 ++++- 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/lib/ddtrace/contrib/active_support/patcher.rb b/lib/ddtrace/contrib/active_support/patcher.rb index 27864b99bdf..10ba6d34fcb 100644 --- a/lib/ddtrace/contrib/active_support/patcher.rb +++ b/lib/ddtrace/contrib/active_support/patcher.rb @@ -33,7 +33,6 @@ def patch_cache_store patch_cache_store_fetch patch_cache_store_write patch_cache_store_delete - reload_cache_store end end @@ -163,27 +162,6 @@ def delete(*args, &block) end end end - - def self.reload_cache_store - redis = Datadog.registry[:redis] - return unless redis && redis.patcher.patched? - - return unless defined?(::ActiveSupport::Cache::RedisStore) && - defined?(::Rails) && - ::Rails.respond_to?(:cache) && - ::Rails.cache.is_a?(::ActiveSupport::Cache::RedisStore) - - Tracer.log.debug('Reloading redis cache store') - - # backward compatibility: Rails 3.x doesn't have `cache=` method - cache_store = ::Rails.configuration.cache_store - cache_instance = ::ActiveSupport::Cache.lookup_store(cache_store) - if ::Rails::VERSION::MAJOR.to_i == 3 - silence_warnings { Object.const_set 'RAILS_CACHE', cache_instance } - elsif ::Rails::VERSION::MAJOR.to_i > 3 - ::Rails.cache = cache_instance - end - end end end end diff --git a/lib/ddtrace/contrib/rails/framework.rb b/lib/ddtrace/contrib/rails/framework.rb index 586ee14d0fa..8eebb241eb6 100644 --- a/lib/ddtrace/contrib/rails/framework.rb +++ b/lib/ddtrace/contrib/rails/framework.rb @@ -63,6 +63,28 @@ def self.activate_active_support!(config) service_name: config[:cache_service], tracer: config[:tracer] ) + + reload_cache_store + end + + def self.reload_cache_store + return unless Datadog.registry[:redis] && + Datadog.registry[:redis].patcher.patched? + + return unless defined?(::ActiveSupport::Cache::RedisStore) && + ::Rails.respond_to?(:cache) && + ::Rails.cache.is_a?(::ActiveSupport::Cache::RedisStore) + + Tracer.log.debug('Reloading redis cache store') + + # backward compatibility: Rails 3.x doesn't have `cache=` method + cache_store = ::Rails.configuration.cache_store + cache_instance = ::ActiveSupport::Cache.lookup_store(cache_store) + if ::Rails::VERSION::MAJOR.to_i == 3 + silence_warnings { Object.const_set 'RAILS_CACHE', cache_instance } + elsif ::Rails::VERSION::MAJOR.to_i > 3 + ::Rails.cache = cache_instance + end end def self.activate_active_record!(config) diff --git a/lib/ddtrace/contrib/redis/patcher.rb b/lib/ddtrace/contrib/redis/patcher.rb index 78248567222..d4035ea2fde 100644 --- a/lib/ddtrace/contrib/redis/patcher.rb +++ b/lib/ddtrace/contrib/redis/patcher.rb @@ -25,7 +25,11 @@ def patch require 'ddtrace/contrib/redis/quantize' patch_redis_client - RailsCachePatcher.reload_cache_store if Datadog.registry[:rails].patcher.patched? + + # Reload the Rails cache, if defined, so instrumentation is available. + if Datadog.registry[:rails] && Datadog.registry[:rails].patcher.patched? + Contrib::Rails::Framework.reload_cache_store + end rescue StandardError => e Datadog::Tracer.log.error("Unable to apply Redis integration: #{e}") end