Module: Datadog::Contrib::Rails::ActiveSupport

Includes:
Patcher
Defined in:
lib/ddtrace/contrib/rails/active_support.rb

Overview

Code used to create and handle ‘rails.cache’ spans.

Class Method Summary collapse

Methods included from Patcher

included

Methods included from Patcher::CommonMethods

#do_once, #without_warnings

Class Method Details

.finish_trace_cache(payload) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ddtrace/contrib/rails/active_support.rb', line 42

def self.finish_trace_cache(payload)
  # retrieve the tracing context and continue the trace
  tracing_context = payload.fetch(:tracing_context)
  span = tracing_context[:dd_cache_span]
  return unless span && !span.finished?

  begin
    # discard parameters from the cache_store configuration
    store, = *Array.wrap(::Rails.configuration.cache_store).flatten
    span.set_tag('rails.cache.backend', store)
    cache_key = Datadog::Utils.truncate(payload.fetch(:key), Ext::CACHE::MAX_KEY_SIZE)
    span.set_tag('rails.cache.key', cache_key)
    span.set_error(payload[:exception]) if payload[:exception]
  ensure
    span.finish()
  end
rescue StandardError => e
  Datadog::Tracer.log.debug(e.message)
end

.instrumentObject



11
12
13
14
15
16
# File 'lib/ddtrace/contrib/rails/active_support.rb', line 11

def self.instrument
  do_once(:instrument) do
    # patch Rails core components
    Datadog::RailsCachePatcher.patch_cache_store
  end
end

.start_trace_cache(payload) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ddtrace/contrib/rails/active_support.rb', line 18

def self.start_trace_cache(payload)
  tracer = Datadog.configuration[:rails][:tracer]
  tracing_context = payload.fetch(:tracing_context)

  # In most of the cases Rails ``fetch()`` and ``read()`` calls are nested.
  # This check ensures that two reads are not nested since they don't provide
  # interesting details.
  # NOTE: the ``finish_trace_cache()`` is fired but it already has a safe-guard
  # to avoid any kind of issue.
  current_span = tracer.active_span
  return if current_span.try('name') == 'rails.cache' &&
            current_span.try('resource') == 'GET' &&
            payload[:action] == 'GET'

  # create a new ``Span`` and add it to the tracing context
  service = Datadog.configuration[:rails][:cache_service]
  type = Datadog::Ext::CACHE::TYPE
  span = tracer.trace('rails.cache', service: service, span_type: type)
  span.resource = payload.fetch(:action)
  tracing_context[:dd_cache_span] = span
rescue StandardError => e
  Datadog::Tracer.log.debug(e.message)
end