-
Notifications
You must be signed in to change notification settings - Fork 375
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
Add cache_key_enabled
option for ActiveSupport
#4022
base: master
Are you sure you want to change the base?
Changes from all commits
e8188fc
e87ed63
1f31567
eb19f98
4197b1a
1c823a0
866c888
45ef5e6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -117,6 +117,21 @@ | |||||
expect(set.get_tag(Datadog::Tracing::Metadata::Ext::TAG_OPERATION)) | ||||||
.to eq('cache') | ||||||
end | ||||||
|
||||||
context 'when cache_key_enabled is false' do | ||||||
before do | ||||||
Datadog.configuration.tracing[:active_support][:cache_key_enabled] = false | ||||||
end | ||||||
|
||||||
it do | ||||||
expect(read).to eq(50) | ||||||
|
||||||
expect(spans).to have(2).items | ||||||
get = spans | ||||||
expect(get.name).to eq('rails.cache') | ||||||
expect(get.get_tag('rails.cache.key')).to be_nil | ||||||
end | ||||||
end | ||||||
end | ||||||
|
||||||
describe '#read_multi' do | ||||||
|
@@ -149,6 +164,20 @@ | |||||
.to eq('cache') | ||||||
end | ||||||
end | ||||||
|
||||||
context 'when cache_key_enabled is false' do | ||||||
before do | ||||||
Datadog.configuration.tracing[:active_support][:cache_key_enabled] = false | ||||||
end | ||||||
|
||||||
it do | ||||||
expect(read_multi).to eq(Hash[multi_keys.zip([51, 52, 53])]) | ||||||
expect(spans).to have(1 + multi_keys.size).items | ||||||
get = spans[0] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ⚪ Code Quality Violation
Suggested change
Improve readability with first (...read more)This rule encourages the use of The use of these methods also helps to make your code more idiomatic, which is a crucial aspect of writing effective Ruby code. Idiomatic code is easier to read, understand, and maintain. It also tends to be more efficient, as idioms often reflect patterns that are optimized for the language. To adhere to this rule, replace the use of array indexing with |
||||||
expect(get.name).to eq('rails.cache') | ||||||
expect(get.get_tag('rails.cache.keys')).to be_nil | ||||||
end | ||||||
end | ||||||
end | ||||||
|
||||||
describe '#write' do | ||||||
|
@@ -191,6 +220,20 @@ | |||||
expect(span.get_tag('rails.cache.key')).to eq('custom-key/x/y/User:3') | ||||||
end | ||||||
end | ||||||
|
||||||
context 'when cache_key_enabled is false' do | ||||||
before do | ||||||
Datadog.configuration.tracing[:active_support][:cache_key_enabled] = false | ||||||
end | ||||||
|
||||||
let(:key) { ['custom-key', %w[x y], user] } | ||||||
let(:user) { double('User', cache_key: 'User:3') } | ||||||
|
||||||
it 'does not expand key using ActiveSupport when cache_key_enabled false' do | ||||||
write | ||||||
expect(span.get_tag('rails.cache.key')).to be_nil | ||||||
end | ||||||
end | ||||||
end | ||||||
|
||||||
describe '#write_multi' do | ||||||
|
@@ -240,6 +283,27 @@ | |||||
expect(span.get_tag('rails.cache.keys')).to eq('["custom-key/x/y/User:3"]') | ||||||
end | ||||||
end | ||||||
|
||||||
context 'when cache_key_enabled is false' do | ||||||
before do | ||||||
Datadog.configuration.tracing[:active_support][:cache_key_enabled] = false | ||||||
end | ||||||
|
||||||
it do | ||||||
write_multi | ||||||
expect(span.name).to eq('rails.cache') | ||||||
expect(span.type).to eq('cache') | ||||||
expect(span.resource).to eq('MSET') | ||||||
expect(span.service).to eq('rails-cache') | ||||||
expect(span.get_tag('rails.cache.backend')).to eq('file_store') | ||||||
expect(span.get_tag('rails.cache.keys')).to be_nil | ||||||
|
||||||
expect(span.get_tag(Datadog::Tracing::Metadata::Ext::TAG_COMPONENT)) | ||||||
.to eq('active_support') | ||||||
expect(span.get_tag(Datadog::Tracing::Metadata::Ext::TAG_OPERATION)) | ||||||
.to eq('cache') | ||||||
end | ||||||
end | ||||||
end | ||||||
|
||||||
context 'when the method is not defined' do | ||||||
|
@@ -278,6 +342,27 @@ | |||||
expect(span.get_tag(Datadog::Tracing::Metadata::Ext::TAG_OPERATION)) | ||||||
.to eq('cache') | ||||||
end | ||||||
|
||||||
context 'when cache_key_enabled is false' do | ||||||
before do | ||||||
Datadog.configuration.tracing[:active_support][:cache_key_enabled] = false | ||||||
end | ||||||
|
||||||
it do | ||||||
delete | ||||||
expect(span.name).to eq('rails.cache') | ||||||
expect(span.type).to eq('cache') | ||||||
expect(span.resource).to eq('DELETE') | ||||||
expect(span.service).to eq('rails-cache') | ||||||
expect(span.get_tag('rails.cache.backend')).to eq('file_store') | ||||||
expect(span.get_tag('rails.cache.key')).to be_nil | ||||||
|
||||||
expect(span.get_tag(Datadog::Tracing::Metadata::Ext::TAG_COMPONENT)) | ||||||
.to eq('active_support') | ||||||
expect(span.get_tag(Datadog::Tracing::Metadata::Ext::TAG_OPERATION)) | ||||||
.to eq('cache') | ||||||
end | ||||||
end | ||||||
end | ||||||
|
||||||
describe '#fetch' do | ||||||
|
@@ -306,6 +391,32 @@ | |||||
.to eq('cache') | ||||||
end | ||||||
end | ||||||
|
||||||
context 'when cache_key_enabled is false' do | ||||||
before do | ||||||
Datadog.configuration.tracing[:active_support][:cache_key_enabled] = false | ||||||
end | ||||||
|
||||||
subject(:fetch) { cache.fetch('exception') { raise 'oops' } } | ||||||
|
||||||
it do | ||||||
expect { fetch }.to raise_error(StandardError) | ||||||
|
||||||
expect(span.name).to eq('rails.cache') | ||||||
expect(span.type).to eq('cache') | ||||||
expect(span.resource).to eq('GET') | ||||||
expect(span.service).to eq('rails-cache') | ||||||
expect(span.get_tag('rails.cache.backend')).to eq('file_store') | ||||||
expect(span.get_tag('rails.cache.key')).to be_nil | ||||||
expect(span.get_tag('error.type')).to eq('RuntimeError') | ||||||
expect(span.get_tag('error.message')).to eq('oops') | ||||||
|
||||||
expect(span.get_tag(Datadog::Tracing::Metadata::Ext::TAG_COMPONENT)) | ||||||
.to eq('active_support') | ||||||
expect(span.get_tag(Datadog::Tracing::Metadata::Ext::TAG_OPERATION)) | ||||||
.to eq('cache') | ||||||
end | ||||||
end | ||||||
end | ||||||
|
||||||
describe '#fetch_multi' do | ||||||
|
@@ -340,6 +451,30 @@ | |||||
.to eq('cache') | ||||||
end | ||||||
end | ||||||
|
||||||
context 'with exception and when cache_key_enabled is false' do | ||||||
before do | ||||||
Datadog.configuration.tracing[:active_support][:cache_key_enabled] = false | ||||||
end | ||||||
subject(:fetch_multi) { cache.fetch_multi('exception', 'another', 'one') { raise 'oops' } } | ||||||
|
||||||
it do | ||||||
expect { fetch_multi }.to raise_error(StandardError) | ||||||
expect(span.name).to eq('rails.cache') | ||||||
expect(span.type).to eq('cache') | ||||||
expect(span.resource).to eq('MGET') | ||||||
expect(span.service).to eq('rails-cache') | ||||||
expect(span.get_tag('rails.cache.backend')).to eq('file_store') | ||||||
expect(span.get_tag('rails.cache.keys')).to be_nil | ||||||
expect(span.get_tag('error.type')).to eq('RuntimeError') | ||||||
expect(span.get_tag('error.message')).to eq('oops') | ||||||
|
||||||
expect(span.get_tag(Datadog::Tracing::Metadata::Ext::TAG_COMPONENT)) | ||||||
.to eq('active_support') | ||||||
expect(span.get_tag(Datadog::Tracing::Metadata::Ext::TAG_OPERATION)) | ||||||
.to eq('cache') | ||||||
end | ||||||
end | ||||||
end | ||||||
|
||||||
context 'when the method is not defined' do | ||||||
|
@@ -378,4 +513,24 @@ | |||||
.to eq('cache') | ||||||
end | ||||||
end | ||||||
|
||||||
context 'with very large cache key and when cache_key_enabled is false' do | ||||||
before do | ||||||
Datadog.configuration.tracing[:active_support][:cache_key_enabled] = false | ||||||
end | ||||||
it 'truncates key too large' do | ||||||
max_key_size = Datadog::Tracing::Contrib::ActiveSupport::Ext::QUANTIZE_CACHE_MAX_KEY_SIZE | ||||||
large_key = ''.ljust(max_key_size * 2, SecureRandom.hex) | ||||||
cache.write(large_key, 'foobar') | ||||||
|
||||||
expect(large_key.size).to be > max_key_size | ||||||
expect(span.name).to eq('rails.cache') | ||||||
expect(span.get_tag('rails.cache.key')).to be_nil | ||||||
|
||||||
expect(span.get_tag(Datadog::Tracing::Metadata::Ext::TAG_COMPONENT)) | ||||||
.to eq('active_support') | ||||||
expect(span.get_tag(Datadog::Tracing::Metadata::Ext::TAG_OPERATION)) | ||||||
.to eq('cache') | ||||||
end | ||||||
end | ||||||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to caution that any addition of configuration like this is an expansion of the public API, so in that sense, we're bound to this method, and other measures (in other integrations) should be consistent with this style.
Allowing this to be disabled is value-add in capability. But users may find it insufficient, and may also need a more sophisticated quantization feature that itself may require more configuration options.
If we stick with
cache_key_enabled
, and we wanted to add regex capability (for example), then we would likely have to docache_key_exclude_pattern
,cache_key_include_pattern
, etc... this is OK, but idiomatically, it may be better to have a subgroup instead (e.g.cache_key.enabled
,cache_key.include_pattern
,cache_key.exclude_pattern
.)So the time to decide on this behavior is now. I would recommend nesting this setting in its own subgroup to produce the
cache_key.enabled
behavior, such that we have the option of idiomatically expanding the API later without introducing a breaking change down the line.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this idea, it seems much more effective and better long-term so I'll refactor to that.