diff --git a/docs/GettingStarted.md b/docs/GettingStarted.md index 921d66dcf6d..e0b483d50b4 100644 --- a/docs/GettingStarted.md +++ b/docs/GettingStarted.md @@ -562,6 +562,7 @@ cache.read('city') | --------------- | - | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------- | | `enabled` | `DD_TRACE_ACTIVE_SUPPORT_ENABLED` | `Bool` | Whether the integration should create spans. | `true` | | `cache_service` | | `String` | Name of application running the `active_support` instrumentation. May be overridden by `global_default_service_name`. [See _Additional Configuration_ for more details](#additional-configuration) | `active_support-cache` | +| `cache_store` | | `Array` | Specifies which cache stores to instrument. Accepts a list of store names (e.g. `memory_store`, `file_store`, or symbols like `:file_store`). If set, only the listed stores will be traced. By default (`nil`), it traces all stores. | `nil` | ### AWS diff --git a/lib/datadog/tracing/contrib/active_support/cache/events/cache.rb b/lib/datadog/tracing/contrib/active_support/cache/events/cache.rb index 24badb58e84..d1149d694b7 100644 --- a/lib/datadog/tracing/contrib/active_support/cache/events/cache.rb +++ b/lib/datadog/tracing/contrib/active_support/cache/events/cache.rb @@ -45,9 +45,15 @@ def span_options 'cache_write_multi.active_support' => { resource: Ext::RESOURCE_CACHE_MSET, multi_key: true } }.freeze - def trace?(event, _payload) + def trace?(event, payload) return false if !Tracing.enabled? || !configuration.enabled + if (cache_store = configuration[:cache_store]) + store = cache_backend(payload[:store]) + + return false unless cache_store.include?(store) + end + # DEV-3.0: Backwards compatibility code for the 2.x gem series. # DEV-3.0: See documentation at {Datadog::Tracing::Contrib::ActiveSupport::Cache::Instrumentation} # DEV-3.0: for the complete information about this backwards compatibility code. diff --git a/lib/datadog/tracing/contrib/active_support/configuration/settings.rb b/lib/datadog/tracing/contrib/active_support/configuration/settings.rb index d7323c5afbb..344848555a1 100644 --- a/lib/datadog/tracing/contrib/active_support/configuration/settings.rb +++ b/lib/datadog/tracing/contrib/active_support/configuration/settings.rb @@ -49,6 +49,19 @@ class Settings < Contrib::Configuration::Settings o.default true end end + + # Specifies which cache stores to trace. + # Accepts a list, with the same format as `config.cache_store` + # (e.g. `memory_store`, `file_store`, or symbols like `:file_store`). + # Defaults to `nil`, which traces all cache stores. + # @see https://github.com/rails/rails/blob/b7520a13adda46c0cc5f3fb4a4c1726633af2bba/guides/source/caching_with_rails.md?plain=1#L576-L582 + option :cache_store do |o| + o.type :array, nilable: true + o.default nil + o.after_set do |stores| + stores&.map!(&:to_s) # Convert symbols to strings to match the Rails configuration format + end + end end end end diff --git a/spec/datadog/tracing/contrib/rails/cache_spec.rb b/spec/datadog/tracing/contrib/rails/cache_spec.rb index 334e5a86428..6920b85f37c 100644 --- a/spec/datadog/tracing/contrib/rails/cache_spec.rb +++ b/spec/datadog/tracing/contrib/rails/cache_spec.rb @@ -200,6 +200,59 @@ .to eq('cache') end + context 'when :cache_store config includes the backend' do + before do + Datadog.configuration.tracing[:active_support][:cache_store] = ['other_store', 'file_store'] + end + + it 'creates a span' do + write + expect(span.get_tag('rails.cache.backend')).to eq('file_store') + end + + context 'as a symbol' do + before do + Datadog.configuration.tracing[:active_support][:cache_store] = [:file_store] + end + + it 'creates a span' do + write + expect(span.get_tag('rails.cache.backend')).to eq('file_store') + end + end + end + + context 'when :cache_store config does not include the backend' do + before do + Datadog.configuration.tracing[:active_support][:cache_store] = ['other_store'] + end + + it 'creates a span for the non-excluded backend' do + expect { write }.to_not(change { fetch_spans.size }) + end + + context 'with an empty array' do + before do + Datadog.configuration.tracing[:active_support][:cache_store] = [] + end + + it 'does not create a span' do + expect { write }.to_not(change { fetch_spans.size }) + end + end + end + + context 'when :cache_store config is not set' do + before do + Datadog.configuration.tracing[:active_support].reset! + end + + it 'creates a span' do + write + expect(span.get_tag('rails.cache.backend')).to eq('file_store') + end + end + context 'with custom cache_service' do before do Datadog.configuration.tracing[:active_support][:cache_service] = 'service-cache'