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



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/ddtrace/contrib/rails/core_extensions.rb', line 173

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



166
167
168
169
170
171
# File 'lib/ddtrace/contrib/rails/core_extensions.rb', line 166

def patch_cache_store
  patch_cache_store_read
  patch_cache_store_fetch
  patch_cache_store_write
  patch_cache_store_delete
end

.patch_cache_store_deleteObject



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/ddtrace/contrib/rails/core_extensions.rb', line 264

def patch_cache_store_delete
  cache_store_class(:delete).class_eval do
    alias_method :delete_without_datadog, :delete
    def delete(*args, &block)
      payload = {
        action: 'DELETE',
        key: args[0],
        tracing_context: {}
      }

      begin
        # process and catch cache exceptions
        Datadog::Contrib::Rails::ActiveSupport.start_trace_cache(payload)
        delete_without_datadog(*args, &block)
      rescue Exception => e
        payload[:exception] = [e.class.name, e.message]
        payload[:exception_object] = e
        raise e
      end
    ensure
      Datadog::Contrib::Rails::ActiveSupport.finish_trace_cache(payload)
    end
  end
end

.patch_cache_store_fetchObject



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/ddtrace/contrib/rails/core_extensions.rb', line 214

def patch_cache_store_fetch
  cache_store_class(:fetch).class_eval do
    alias_method :fetch_without_datadog, :fetch
    def fetch(*args, &block)
      payload = {
        action: 'GET',
        key: args[0],
        tracing_context: {}
      }

      begin
        # process and catch cache exceptions
        Datadog::Contrib::Rails::ActiveSupport.start_trace_cache(payload)
        fetch_without_datadog(*args, &block)
      rescue Exception => e
        payload[:exception] = [e.class.name, e.message]
        payload[:exception_object] = e
        raise e
      end
    ensure
      Datadog::Contrib::Rails::ActiveSupport.finish_trace_cache(payload)
    end
  end
end

.patch_cache_store_readObject



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/ddtrace/contrib/rails/core_extensions.rb', line 189

def patch_cache_store_read
  cache_store_class(:read).class_eval do
    alias_method :read_without_datadog, :read
    def read(*args, &block)
      payload = {
        action: 'GET',
        key: args[0],
        tracing_context: {}
      }

      begin
        # process and catch cache exceptions
        Datadog::Contrib::Rails::ActiveSupport.start_trace_cache(payload)
        read_without_datadog(*args, &block)
      rescue Exception => e
        payload[:exception] = [e.class.name, e.message]
        payload[:exception_object] = e
        raise e
      end
    ensure
      Datadog::Contrib::Rails::ActiveSupport.finish_trace_cache(payload)
    end
  end
end

.patch_cache_store_writeObject



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/ddtrace/contrib/rails/core_extensions.rb', line 239

def patch_cache_store_write
  cache_store_class(:write).class_eval do
    alias_method :write_without_datadog, :write
    def write(*args, &block)
      payload = {
        action: 'SET',
        key: args[0],
        tracing_context: {}
      }

      begin
        # process and catch cache exceptions
        Datadog::Contrib::Rails::ActiveSupport.start_trace_cache(payload)
        write_without_datadog(*args, &block)
      rescue Exception => e
        payload[:exception] = [e.class.name, e.message]
        payload[:exception_object] = e
        raise e
      end
    ensure
      Datadog::Contrib::Rails::ActiveSupport.finish_trace_cache(payload)
    end
  end
end