Skip to content

Commit 2933c34

Browse files
committed
Add cache_key.enabled option for ActiveSupport
1 parent 646d17a commit 2933c34

File tree

4 files changed

+171
-2
lines changed

4 files changed

+171
-2
lines changed

lib/datadog/tracing/contrib/active_support/cache/events/cache.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ def on_start(span, event, _id, payload)
8181

8282
span.set_tag('EVENT', event)
8383

84-
set_cache_key(span, key, mapping[:multi_key])
84+
if Datadog.configuration.tracing[:active_support][:cache_key].enabled
85+
set_cache_key(span, key, mapping[:multi_key])
86+
end
8587
rescue StandardError => e
8688
Datadog.logger.error(e.message)
8789
Datadog::Core::Telemetry::Logger.report(e)

lib/datadog/tracing/contrib/active_support/cache/instrumentation.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require_relative '../../../../core/utils'
44
require_relative '../../../metadata/ext'
55
require_relative '../ext'
6+
require_relative 'event'
67

78
module Datadog
89
module Tracing
@@ -58,7 +59,8 @@ def trace(action, store, key: nil, multi_key: nil)
5859
end
5960

6061
span.set_tag(Ext::TAG_CACHE_BACKEND, store) if store
61-
set_cache_key(span, key, multi_key)
62+
63+
set_cache_key(span, key, multi_key) if Datadog.configuration.tracing[:active_support][:cache_key].enabled
6264

6365
yield
6466
end

lib/datadog/tracing/contrib/active_support/configuration/settings.rb

+10
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ class Settings < Contrib::Configuration::Settings
3939
)
4040
end
4141
end
42+
43+
# grouped "cache_key.*" settings
44+
settings :cache_key do
45+
# enable or disabling the inclusion of the cache_key in the span
46+
option :enabled do |o|
47+
# cache_key.enabled
48+
o.type :bool
49+
o.default true
50+
end
51+
end
4252
end
4353
end
4454
end

spec/datadog/tracing/contrib/rails/cache_spec.rb

+155
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,21 @@
117117
expect(set.get_tag(Datadog::Tracing::Metadata::Ext::TAG_OPERATION))
118118
.to eq('cache')
119119
end
120+
121+
context 'when cache_key.enabled is false' do
122+
before do
123+
Datadog.configuration.tracing[:active_support][:cache_key].enabled = false
124+
end
125+
126+
it do
127+
expect(read).to eq(50)
128+
129+
expect(spans).to have(2).items
130+
get, = spans
131+
expect(get.name).to eq('rails.cache')
132+
expect(get.get_tag('rails.cache.key')).to be_nil
133+
end
134+
end
120135
end
121136

122137
describe '#read_multi' do
@@ -149,6 +164,20 @@
149164
.to eq('cache')
150165
end
151166
end
167+
168+
context 'when cache_key.enabled is false' do
169+
before do
170+
Datadog.configuration.tracing[:active_support][:cache_key].enabled = false
171+
end
172+
173+
it do
174+
expect(read_multi).to eq(Hash[multi_keys.zip([51, 52, 53])])
175+
expect(spans).to have(1 + multi_keys.size).items
176+
get = spans[0]
177+
expect(get.name).to eq('rails.cache')
178+
expect(get.get_tag('rails.cache.keys')).to be_nil
179+
end
180+
end
152181
end
153182

154183
describe '#write' do
@@ -191,6 +220,20 @@
191220
expect(span.get_tag('rails.cache.key')).to eq('custom-key/x/y/User:3')
192221
end
193222
end
223+
224+
context 'when cache_key.enabled is false' do
225+
before do
226+
Datadog.configuration.tracing[:active_support][:cache_key].enabled = false
227+
end
228+
229+
let(:key) { ['custom-key', %w[x y], user] }
230+
let(:user) { double('User', cache_key: 'User:3') }
231+
232+
it 'does not expand key using ActiveSupport when cache_key.enabled false' do
233+
write
234+
expect(span.get_tag('rails.cache.key')).to be_nil
235+
end
236+
end
194237
end
195238

196239
describe '#write_multi' do
@@ -240,6 +283,27 @@
240283
expect(span.get_tag('rails.cache.keys')).to eq('["custom-key/x/y/User:3"]')
241284
end
242285
end
286+
287+
context 'when cache_key.enabled is false' do
288+
before do
289+
Datadog.configuration.tracing[:active_support][:cache_key].enabled = false
290+
end
291+
292+
it do
293+
write_multi
294+
expect(span.name).to eq('rails.cache')
295+
expect(span.type).to eq('cache')
296+
expect(span.resource).to eq('MSET')
297+
expect(span.service).to eq('rails-cache')
298+
expect(span.get_tag('rails.cache.backend')).to eq('file_store')
299+
expect(span.get_tag('rails.cache.keys')).to be_nil
300+
301+
expect(span.get_tag(Datadog::Tracing::Metadata::Ext::TAG_COMPONENT))
302+
.to eq('active_support')
303+
expect(span.get_tag(Datadog::Tracing::Metadata::Ext::TAG_OPERATION))
304+
.to eq('cache')
305+
end
306+
end
243307
end
244308

245309
context 'when the method is not defined' do
@@ -278,6 +342,27 @@
278342
expect(span.get_tag(Datadog::Tracing::Metadata::Ext::TAG_OPERATION))
279343
.to eq('cache')
280344
end
345+
346+
context 'when cache_key.enabled is false' do
347+
before do
348+
Datadog.configuration.tracing[:active_support][:cache_key].enabled = false
349+
end
350+
351+
it do
352+
delete
353+
expect(span.name).to eq('rails.cache')
354+
expect(span.type).to eq('cache')
355+
expect(span.resource).to eq('DELETE')
356+
expect(span.service).to eq('rails-cache')
357+
expect(span.get_tag('rails.cache.backend')).to eq('file_store')
358+
expect(span.get_tag('rails.cache.key')).to be_nil
359+
360+
expect(span.get_tag(Datadog::Tracing::Metadata::Ext::TAG_COMPONENT))
361+
.to eq('active_support')
362+
expect(span.get_tag(Datadog::Tracing::Metadata::Ext::TAG_OPERATION))
363+
.to eq('cache')
364+
end
365+
end
281366
end
282367

283368
describe '#fetch' do
@@ -306,6 +391,32 @@
306391
.to eq('cache')
307392
end
308393
end
394+
395+
context 'when cache_key.enabled is false' do
396+
before do
397+
Datadog.configuration.tracing[:active_support][:cache_key].enabled = false
398+
end
399+
400+
subject(:fetch) { cache.fetch('exception') { raise 'oops' } }
401+
402+
it do
403+
expect { fetch }.to raise_error(StandardError)
404+
405+
expect(span.name).to eq('rails.cache')
406+
expect(span.type).to eq('cache')
407+
expect(span.resource).to eq('GET')
408+
expect(span.service).to eq('rails-cache')
409+
expect(span.get_tag('rails.cache.backend')).to eq('file_store')
410+
expect(span.get_tag('rails.cache.key')).to be_nil
411+
expect(span.get_tag('error.type')).to eq('RuntimeError')
412+
expect(span.get_tag('error.message')).to eq('oops')
413+
414+
expect(span.get_tag(Datadog::Tracing::Metadata::Ext::TAG_COMPONENT))
415+
.to eq('active_support')
416+
expect(span.get_tag(Datadog::Tracing::Metadata::Ext::TAG_OPERATION))
417+
.to eq('cache')
418+
end
419+
end
309420
end
310421

311422
describe '#fetch_multi' do
@@ -340,6 +451,30 @@
340451
.to eq('cache')
341452
end
342453
end
454+
455+
context 'with exception and when cache_key.enabled is false' do
456+
before do
457+
Datadog.configuration.tracing[:active_support][:cache_key].enabled = false
458+
end
459+
subject(:fetch_multi) { cache.fetch_multi('exception', 'another', 'one') { raise 'oops' } }
460+
461+
it do
462+
expect { fetch_multi }.to raise_error(StandardError)
463+
expect(span.name).to eq('rails.cache')
464+
expect(span.type).to eq('cache')
465+
expect(span.resource).to eq('MGET')
466+
expect(span.service).to eq('rails-cache')
467+
expect(span.get_tag('rails.cache.backend')).to eq('file_store')
468+
expect(span.get_tag('rails.cache.keys')).to be_nil
469+
expect(span.get_tag('error.type')).to eq('RuntimeError')
470+
expect(span.get_tag('error.message')).to eq('oops')
471+
472+
expect(span.get_tag(Datadog::Tracing::Metadata::Ext::TAG_COMPONENT))
473+
.to eq('active_support')
474+
expect(span.get_tag(Datadog::Tracing::Metadata::Ext::TAG_OPERATION))
475+
.to eq('cache')
476+
end
477+
end
343478
end
344479

345480
context 'when the method is not defined' do
@@ -378,4 +513,24 @@
378513
.to eq('cache')
379514
end
380515
end
516+
517+
context 'with very large cache key and when cache_key.enabled is false' do
518+
before do
519+
Datadog.configuration.tracing[:active_support][:cache_key].enabled = false
520+
end
521+
it 'truncates key too large' do
522+
max_key_size = Datadog::Tracing::Contrib::ActiveSupport::Ext::QUANTIZE_CACHE_MAX_KEY_SIZE
523+
large_key = ''.ljust(max_key_size * 2, SecureRandom.hex)
524+
cache.write(large_key, 'foobar')
525+
526+
expect(large_key.size).to be > max_key_size
527+
expect(span.name).to eq('rails.cache')
528+
expect(span.get_tag('rails.cache.key')).to be_nil
529+
530+
expect(span.get_tag(Datadog::Tracing::Metadata::Ext::TAG_COMPONENT))
531+
.to eq('active_support')
532+
expect(span.get_tag(Datadog::Tracing::Metadata::Ext::TAG_OPERATION))
533+
.to eq('cache')
534+
end
535+
end
381536
end

0 commit comments

Comments
 (0)