Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
marcotc committed Sep 7, 2022
1 parent eaa1469 commit e821fdf
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
8 changes: 8 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,11 @@ if RUBY_VERSION >= '2.4.0' && (RUBY_PLATFORM =~ /^x86_64-(darwin|linux)/)
gem 'sorbet', '= 0.5.9672'
gem 'spoom', '~> 1.1'
end

gem 'rails', '~> 6.1.0'
gem 'pg', '>= 1.1', platform: :ruby
gem 'activerecord-jdbcpostgresql-adapter', '>= 61', platform: :jruby
gem 'sidekiq', '>= 6.1.2'
gem 'sprockets', '< 4'
gem 'lograge', '~> 0.11'
gem 'i18n', '1.8.7', platform: :jruby # Removal pending: https://github.com/ruby-i18n/i18n/issues/555#issuecomment-772112169
30 changes: 30 additions & 0 deletions lib/datadog/core/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,36 @@ def self.extract_host_port(host_port)

[match[1], match[2].to_i]
end

# Remove the prefixed module names from `Class#name` or `Module#name`.
# @example
# `App::Api::User` returns `User`.
# @param [String] class_name the result from a `Class#name` or `Module#name` call
# @return [String] the class or module name without its enclosing modules
def self.extract_class_name(class_name)
if idx = class_name.rindex("::")
class_name[(idx + 2)..-1]
else
class_name
end
end

# Converts CamelCase strings to snake_case.
# @example
# `MyClass` returns `my_class`.
# @param [String] string a CamelCase string
# @return [String] the snake_case version of the provided string
def self.camel_to_snake_case(string)
first_letter = true
string.gsub(/[A-Z]/) { |upcase_char|
if first_letter # Don't add `_` at the beginning of the string.
first_letter = false
upcase_char.downcase
else
"_#{upcase_char.downcase}"
end
}
end
end
end
end
37 changes: 27 additions & 10 deletions lib/datadog/tracing/contrib/active_support/cache/instrumentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def start_trace_cache(payload)
Datadog.logger.debug(e.message)
end

def finish_trace_cache(payload)
def finish_trace_cache(payload, instance)
return unless enabled?

# retrieve the tracing context and continue the trace
Expand All @@ -59,11 +59,28 @@ def finish_trace_cache(payload)
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
# Rails-compatible storage adapter identification.
#
# This will return the same result as the symbol provided to `config.cache_store` in the
# Rails configuration block: https://github.com/rails/rails/blob/f2ab66da735f9382a923471e27aca12f07ec05cd/guides/source/caching_with_rails.md#configuration
#
# For example:
# `config.cache_store = :memory_store, { size: 64.megabytes }` will return `memory_store`.
#
# This makes the cache backend immediately identifiable by the user.
#
# A few downsides: it is a string-heavy process to convert the current adapter back into
# its Rails configuration name. Also, this process assumes all cache instances live under a flat
# namespace: if the user creates a custom cache adapter called `App::MemoryStore` it will clash
# with the built-in `memory_store` adapter when reported through the `rails.cache.backend` tag.
#
# DEV: This is implemented in its current form to ensure backwards compatibility:
# DEV: the previous iteration would to always return `Rails.configuration.cache_store`
# DEV: regardless of the storage backend being used, which is incorrect.
# DEV: This version supports any backend class that is currently being instrumented.
class_name = Utils.extract_class_name(instance.class.name)
store = Utils.camel_to_snake_case(class_name)
span.set_tag(Ext::TAG_CACHE_BACKEND, store)

normalized_key = ::ActiveSupport::Cache.expand_cache_key(payload.fetch(:key))
cache_key = Core::Utils.truncate(normalized_key, Ext::QUANTIZE_CACHE_MAX_KEY_SIZE)
Expand Down Expand Up @@ -128,7 +145,7 @@ def read(*args, &block)
raise e
end
ensure
Instrumentation.finish_trace_cache(payload)
Instrumentation.finish_trace_cache(payload, self)
end
end

Expand Down Expand Up @@ -174,7 +191,7 @@ def fetch(*args, &block)
raise e
end
ensure
Instrumentation.finish_trace_cache(payload)
Instrumentation.finish_trace_cache(payload, self)
end
end

Expand Down Expand Up @@ -222,7 +239,7 @@ def write(*args, &block)
raise e
end
ensure
Instrumentation.finish_trace_cache(payload)
Instrumentation.finish_trace_cache(payload, self)
end
end

Expand Down Expand Up @@ -268,7 +285,7 @@ def delete(*args, &block)
raise e
end
ensure
Instrumentation.finish_trace_cache(payload)
Instrumentation.finish_trace_cache(payload, self)
end
end
end
Expand Down

0 comments on commit e821fdf

Please sign in to comment.