Skip to content

Commit

Permalink
Changed: reload_cache_store in Rails instead of ActiveSupport.
Browse files Browse the repository at this point in the history
  • Loading branch information
delner committed Jun 27, 2019
1 parent 1083029 commit a8860c5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
22 changes: 0 additions & 22 deletions lib/ddtrace/contrib/active_support/patcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions lib/ddtrace/contrib/rails/framework.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion lib/ddtrace/contrib/redis/patcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a8860c5

Please sign in to comment.