Module: Datadog::Tracing::Contrib::ActiveSupport::Cache::Instrumentation

Defined in:
lib/datadog/tracing/contrib/active_support/cache/instrumentation.rb

Overview

Defines instrumentation for ActiveSupport caching rubocop:disable Lint/RescueException

Defined Under Namespace

Modules: Delete, Fetch, FetchMulti, Read, ReadMulti, Write, WriteMulti

Class Method Summary collapse

Class Method Details

.enabled?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/datadog/tracing/contrib/active_support/cache/instrumentation.rb', line 108

def enabled?
  Tracing.enabled? && Datadog.configuration.tracing[:active_support][:enabled]
end

.finish_trace_cache(payload) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/datadog/tracing/contrib/active_support/cache/instrumentation.rb', line 53

def finish_trace_cache(payload)
  return unless enabled?

  # 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
    if defined?(::Rails)
      store, = *Array.wrap(::Rails.configuration.cache_store).flatten
      span.set_tag(Ext::TAG_CACHE_BACKEND, store)
    end

    normalized_key = ::ActiveSupport::Cache.expand_cache_key(payload.fetch(:key))
    cache_key = Core::Utils.truncate(normalized_key, Ext::QUANTIZE_CACHE_MAX_KEY_SIZE)
    span.set_tag(Ext::TAG_CACHE_KEY, cache_key)

    span.set_error(payload[:exception]) if payload[:exception]
  ensure
    span.finish
  end
rescue StandardError => e
  Datadog.logger.debug(e.message)
end

.finish_trace_cache_multi(payload) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/datadog/tracing/contrib/active_support/cache/instrumentation.rb', line 80

def finish_trace_cache_multi(payload)
  return unless enabled?

  # 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
    if defined?(::Rails)
      store, = *Array.wrap(::Rails.configuration.cache_store).flatten
      span.set_tag(Ext::TAG_CACHE_BACKEND, store)
    end
    normalized_keys = payload.fetch(:keys, []).map do |key|
      ::ActiveSupport::Cache.expand_cache_key(key)
    end
    cache_keys = Core::Utils.truncate(normalized_keys, Ext::QUANTIZE_CACHE_MAX_KEY_SIZE)
    span.set_tag(Ext::TAG_CACHE_KEY_MULTI, cache_keys)

    span.set_error(payload[:exception]) if payload[:exception]
  ensure
    span.finish
  end
rescue StandardError => e
  Datadog.logger.debug(e.message)
end

.start_trace_cache(payload) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/datadog/tracing/contrib/active_support/cache/instrumentation.rb', line 17

def start_trace_cache(payload)
  return unless enabled?

  # 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 = Tracing.active_span
  return if current_span.try(:name) == Ext::SPAN_CACHE &&
    (
      payload[:action] == Ext::RESOURCE_CACHE_GET &&
      current_span.try(:resource) == Ext::RESOURCE_CACHE_GET ||
      payload[:action] == Ext::RESOURCE_CACHE_MGET &&
      current_span.try(:resource) == Ext::RESOURCE_CACHE_MGET
    )

  tracing_context = payload.fetch(:tracing_context)

  # create a new ``Span`` and add it to the tracing context
  service = Datadog.configuration.tracing[:active_support][:cache_service]
  type = Ext::SPAN_TYPE_CACHE
  span = Tracing.trace(Ext::SPAN_CACHE, service: service, span_type: type)
  span.resource = payload.fetch(:action)

  span.set_tag(Tracing::Metadata::Ext::TAG_COMPONENT, Ext::TAG_COMPONENT)
  span.set_tag(Tracing::Metadata::Ext::TAG_OPERATION, Ext::TAG_OPERATION_CACHE)

  # Tag as an external peer service
  span.set_tag(Tracing::Metadata::Ext::TAG_PEER_SERVICE, service)

  tracing_context[:dd_cache_span] = span
rescue StandardError => e
  Datadog.logger.debug(e.message)
end