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

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

Overview

Defines instrumentation for ActiveSupport caching

Defined Under Namespace

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

Class Method Summary collapse

Class Method Details

.enabled?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/datadog/tracing/contrib/active_support/cache/instrumentation.rb', line 69

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

.nested_multiread?Boolean

In most of the cases, ‘#fetch()` and `#read()` calls are nested. Instrument both does not add any value. This method checks if these two operations are nested.

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/datadog/tracing/contrib/active_support/cache/instrumentation.rb', line 52

def nested_multiread?
  current_span = Tracing.active_span
  current_span && current_span.name == Ext::SPAN_CACHE && current_span.resource == Ext::RESOURCE_CACHE_MGET
end

.nested_read?Boolean

In most of the cases, ‘#fetch()` and `#read()` calls are nested. Instrument both does not add any value. This method checks if these two operations are nested.

Returns:

  • (Boolean)


46
47
48
49
# File 'lib/datadog/tracing/contrib/active_support/cache/instrumentation.rb', line 46

def nested_read?
  current_span = Tracing.active_span
  current_span && current_span.name == Ext::SPAN_CACHE && current_span.resource == Ext::RESOURCE_CACHE_GET
end

.set_cache_key(span, single_key, multi_key) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/datadog/tracing/contrib/active_support/cache/instrumentation.rb', line 57

def set_cache_key(span, single_key, multi_key)
  if multi_key
    resolved_key = multi_key.map { |key| ::ActiveSupport::Cache.expand_cache_key(key) }
    cache_key = Core::Utils.truncate(resolved_key, Ext::QUANTIZE_CACHE_MAX_KEY_SIZE)
    span.set_tag(Ext::TAG_CACHE_KEY_MULTI, cache_key)
  else
    resolved_key = ::ActiveSupport::Cache.expand_cache_key(single_key)
    cache_key = Core::Utils.truncate(resolved_key, Ext::QUANTIZE_CACHE_MAX_KEY_SIZE)
    span.set_tag(Ext::TAG_CACHE_KEY, cache_key)
  end
end

.trace(action, store, key: nil, multi_key: nil) ⇒ Object

Parameters:

  • action (String)

    type of cache operation. Will be set as the span resource.

  • key (Object) (defaults to: nil)

    redis cache key. Used for actions with a single key locator.

  • multi_key (Array<Object>) (defaults to: nil)

    list of redis cache keys. Used for actions with a multiple key locators.



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

def trace(action, store, key: nil, multi_key: nil)
  return yield unless enabled?

  # create a new ``Span`` and add it to the tracing context
  Tracing.trace(
    Ext::SPAN_CACHE,
    span_type: Ext::SPAN_TYPE_CACHE,
    service: Datadog.configuration.tracing[:active_support][:cache_service],
    resource: action
  ) do |span|
    span.set_tag(Tracing::Metadata::Ext::TAG_COMPONENT, Ext::TAG_COMPONENT)
    span.set_tag(Tracing::Metadata::Ext::TAG_OPERATION, Ext::TAG_OPERATION_CACHE)

    if span.service != Datadog.configuration.service
      span.set_tag(Tracing::Contrib::Ext::Metadata::TAG_BASE_SERVICE, Datadog.configuration.service)
    end

    span.set_tag(Ext::TAG_CACHE_BACKEND, store) if store
    set_cache_key(span, key, multi_key)

    yield
  end
end