From 9bc3c30163b7bd21430ff477d19b2b7b73dbe473 Mon Sep 17 00:00:00 2001 From: Nathan Griffith Date: Tue, 24 Oct 2023 12:29:12 -0400 Subject: [PATCH] Fix instrumentation of custom cache stores (#3206) Co-authored-by: Marco Costa Co-authored-by: marcotc --- .../active_support/cache/instrumentation.rb | 4 +++- .../tracing/contrib/rails/cache_spec.rb | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/datadog/tracing/contrib/active_support/cache/instrumentation.rb b/lib/datadog/tracing/contrib/active_support/cache/instrumentation.rb index deeeb7584c1..c3196809b48 100644 --- a/lib/datadog/tracing/contrib/active_support/cache/instrumentation.rb +++ b/lib/datadog/tracing/contrib/active_support/cache/instrumentation.rb @@ -101,7 +101,9 @@ def dd_store_name # DEV: String#underscore is available through ActiveSupport, and is # DEV: the exact reverse operation to `#camelize`. - @store_name = self.class.name.underscore.match(%r{active_support/cache/(.*)})[1] + # DEV: String#demodulize is available through ActiveSupport, and is + # DEV: used to remove the module ('*::') part of a constant name. + @store_name = self.class.name.demodulize.underscore end end diff --git a/spec/datadog/tracing/contrib/rails/cache_spec.rb b/spec/datadog/tracing/contrib/rails/cache_spec.rb index 67dbbbbd4e6..950fb784b29 100644 --- a/spec/datadog/tracing/contrib/rails/cache_spec.rb +++ b/spec/datadog/tracing/contrib/rails/cache_spec.rb @@ -61,6 +61,26 @@ end end + context 'with a cache not in the ActiveSupport::Cache:: namespace' do + let(:cache_class) { stub_const('My::CustomCache', Class.new(ActiveSupport::Cache::MemoryStore)) } + let(:cache) { cache_class.new } + + it 'returns the matching backend type' do + subject + expect(spans[0].get_tag('rails.cache.backend')).to eq('custom_cache') + end + end + + context 'with an unnamespaced cache class' do + let(:cache_class) { stub_const('CustomCache', Class.new(ActiveSupport::Cache::MemoryStore)) } + let(:cache) { cache_class.new } + + it 'returns the matching backend type' do + subject + expect(spans[0].get_tag('rails.cache.backend')).to eq('custom_cache') + end + end + it_behaves_like 'measured span for integration', false do before { subject } let(:span) { spans[0] }