Skip to content

Add Rails cache store instrumentation filter #4693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,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` |

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| `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` |
| `cache_store` | | `Array` | Specifies which cache stores to instrument. Accepts an array of store names such as `memory_store`, `file_store`, or symbols like `:file_store`. If set, only the listed stores are traced. Defaults to `nil`, which traces all stores. | `nil` |

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the option be in plural, i.e. cache_stores?


### AWS

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the customer-supplied array duped during assignment? Otherwise this would mutate their data.

end
end
end
end
end
Expand Down
53 changes: 53 additions & 0 deletions spec/datadog/tracing/contrib/rails/cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Loading