Module: Datadog::RailsCachePatcher

Defined in:
lib/ddtrace/contrib/rails/core_extensions.rb

Overview

RailsCachePatcher contains function to patch Rails caching libraries.

Class Method Summary collapse

Class Method Details

.cache_store_class(k) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ddtrace/contrib/rails/core_extensions.rb', line 64

def cache_store_class(k)
  # When Redis is used, we can't only patch Cache::Store as it is
  # Cache::RedisStore, a sub-class of it that is used, in practice.
  # We need to do a per-method monkey patching as some of them might
  # be redefined, and some of them not. The latest version of redis-activesupport
  # redefines write but leaves untouched read and delete:
  # https://github.com/redis-store/redis-activesupport/blob/master/lib/active_support/cache/redis_store.rb
  c = if defined?(::ActiveSupport::Cache::RedisStore) &&
         ::ActiveSupport::Cache::RedisStore.instance_methods(false).include?(k)
        ::ActiveSupport::Cache::RedisStore
      else
        ::ActiveSupport::Cache::Store
      end
  c
end

.patch_cache_storeObject



56
57
58
59
60
61
62
# File 'lib/ddtrace/contrib/rails/core_extensions.rb', line 56

def patch_cache_store
  patch_cache_store_read
  patch_cache_store_fetch
  patch_cache_store_write
  patch_cache_store_delete
  patch_cache_store_instrument
end

.patch_cache_store_deleteObject



110
111
112
113
114
115
116
117
118
# File 'lib/ddtrace/contrib/rails/core_extensions.rb', line 110

def patch_cache_store_delete
  cache_store_class(:delete).class_eval do
    alias_method :delete_without_datadog, :delete
    def delete(*args, &block)
      ActiveSupport::Notifications.instrument('start_cache_delete.active_support')
      delete_without_datadog(*args, &block)
    end
  end
end

.patch_cache_store_fetchObject



90
91
92
93
94
95
96
97
98
# File 'lib/ddtrace/contrib/rails/core_extensions.rb', line 90

def patch_cache_store_fetch
  cache_store_class(:fetch).class_eval do
    alias_method :fetch_without_datadog, :fetch
    def fetch(*args, &block)
      ActiveSupport::Notifications.instrument('start_cache_fetch.active_support')
      fetch_without_datadog(*args, &block)
    end
  end
end

.patch_cache_store_instrumentObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/ddtrace/contrib/rails/core_extensions.rb', line 120

def patch_cache_store_instrument
  # by default, Rails 3 doesn't instrument the cache system so we should turn it on
  # using the ActiveSupport::Cache::Store.instrument= function. Unfortunately, early
  # versions of Rails use a Thread.current store that is not compatible with some
  # application servers like Passenger.
  # More details: https://github.com/rails/rails/blob/v3.2.22.5/activesupport/lib/active_support/cache.rb#L175-L177
  return unless ::Rails::VERSION::MAJOR.to_i == 3
  ::ActiveSupport::Cache::Store.singleton_class.class_eval do
    # Add the instrument function that Rails 3.x uses
    # to know if the underlying cache should be instrumented or not. By default,
    # we force that instrumentation if the Rails application is auto instrumented.
    def instrument
      true
    end
  end
end

.patch_cache_store_readObject



80
81
82
83
84
85
86
87
88
# File 'lib/ddtrace/contrib/rails/core_extensions.rb', line 80

def patch_cache_store_read
  cache_store_class(:read).class_eval do
    alias_method :read_without_datadog, :read
    def read(*args, &block)
      ActiveSupport::Notifications.instrument('start_cache_read.active_support')
      read_without_datadog(*args, &block)
    end
  end
end

.patch_cache_store_writeObject



100
101
102
103
104
105
106
107
108
# File 'lib/ddtrace/contrib/rails/core_extensions.rb', line 100

def patch_cache_store_write
  cache_store_class(:write).class_eval do
    alias_method :write_without_datadog, :write
    def write(*args, &block)
      ActiveSupport::Notifications.instrument('start_cache_write.active_support')
      write_without_datadog(*args, &block)
    end
  end
end