Skip to content

Commit

Permalink
Copy/paste tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bouwkast committed Oct 23, 2024
1 parent e8188fc commit e87ed63
Showing 1 changed file with 180 additions and 0 deletions.
180 changes: 180 additions & 0 deletions spec/datadog/tracing/contrib/rails/cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,34 @@
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

expect(read).to eq(50)

expect(spans).to have(2).items
get, set = spans
expect(get.name).to eq('rails.cache')
expect(get.type).to eq('cache')
expect(get.resource).to eq('GET')
expect(get.service).to eq('rails-cache')
expect(get.get_tag('rails.cache.backend')).to eq('file_store')
expect(get.get_tag('rails.cache.key')).to be_nil
expect(set.name).to eq('rails.cache')

expect(get.get_tag(Datadog::Tracing::Metadata::Ext::TAG_COMPONENT))
.to eq('active_support')
expect(get.get_tag(Datadog::Tracing::Metadata::Ext::TAG_OPERATION))
.to eq('cache')

expect(set.get_tag(Datadog::Tracing::Metadata::Ext::TAG_COMPONENT))
.to eq('active_support')
expect(set.get_tag(Datadog::Tracing::Metadata::Ext::TAG_OPERATION))
.to eq('cache')
end
end

describe '#read_multi' do
Expand Down Expand Up @@ -149,6 +177,34 @@
.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

expect(read_multi).to eq(Hash[multi_keys.zip([51, 52, 53])])
expect(spans).to have(1 + multi_keys.size).items
get = spans[0]
expect(get.name).to eq('rails.cache')
expect(get.type).to eq('cache')
expect(get.resource).to eq('MGET')
expect(get.service).to eq('rails-cache')
expect(get.get_tag('rails.cache.backend')).to eq('file_store')
expect(get.get_tag('rails.cache.keys')).to be_nil
expect(get.get_tag(Datadog::Tracing::Metadata::Ext::TAG_COMPONENT))
.to eq('active_support')
expect(get.get_tag(Datadog::Tracing::Metadata::Ext::TAG_OPERATION))
.to eq('cache')

spans[1..-1].each do |set|
expect(set.name).to eq('rails.cache')
expect(set.get_tag(Datadog::Tracing::Metadata::Ext::TAG_COMPONENT))
.to eq('active_support')
expect(set.get_tag(Datadog::Tracing::Metadata::Ext::TAG_OPERATION))
.to eq('cache')
end
end
end

describe '#write' do
Expand Down Expand Up @@ -191,6 +247,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
Expand Down Expand Up @@ -240,6 +310,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
Expand Down Expand Up @@ -278,6 +369,25 @@
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

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

describe '#fetch' do
Expand Down Expand Up @@ -306,6 +416,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
Expand Down Expand Up @@ -340,6 +476,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
Expand Down Expand Up @@ -378,4 +538,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

0 comments on commit e87ed63

Please sign in to comment.