-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathcache_spec.rb
536 lines (435 loc) · 17.7 KB
/
cache_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
require 'spec_helper'
require 'datadog/tracing/contrib/analytics_examples'
require 'securerandom'
require 'datadog/tracing/contrib/rails/ext'
require 'datadog/tracing/contrib/rails/rails_helper'
RSpec.describe 'Rails cache' do
include_context 'Rails test application'
before do
Datadog.configuration.tracing[:active_support][:cache_service] = 'rails-cache'
end
after do
Datadog.configuration.tracing[:active_support].reset!
end
before { app }
let(:cache) { Rails.cache }
let(:key) { 'custom-key' }
let(:multi_keys) { %w[custom-key-1 custom-key-2 custom-key-3] }
shared_examples 'an instrumented cache method' do
context 'disabled at integration level' do
before { Datadog.configuration.tracing[:active_support].enabled = false }
after { Datadog.configuration.tracing[:active_support].reset! }
it 'does not instrument' do
expect { subject }.to_not(change { fetch_spans })
end
end
context 'disabled at tracer level' do
before do
Datadog.configure do |c|
c.tracing.enabled = false
end
end
after { Datadog.configuration.tracing.reset! }
it 'does not instrument' do
expect { subject }.to_not(change { fetch_spans })
end
end
context "with a cache different from Rails' default store" do
let(:cache) { ActiveSupport::Cache::MemoryStore.new }
before do
expect(cache).to_not be_a(Rails.cache.class) # Sanity check that they are different
end
it 'returns the matching backend type' do
subject
expect(spans[0].get_tag('rails.cache.backend')).to eq('memory_store')
end
end
context 'with a cache not in the ActiveSupport::Cache:: namespace' do
let(:cache_class) { stub_const('My::CustomCache', Class.new(ActiveSupport::Cache::MemoryStore)) }
let(:cache) { cache_class.new }
it 'returns the matching backend type' do
subject
expect(spans[0].get_tag('rails.cache.backend')).to eq('custom_cache')
end
end
context 'with an unnamespaced cache class' do
let(:cache_class) { stub_const('CustomCache', Class.new(ActiveSupport::Cache::MemoryStore)) }
let(:cache) { cache_class.new }
it 'returns the matching backend type' do
subject
expect(spans[0].get_tag('rails.cache.backend')).to eq('custom_cache')
end
end
it_behaves_like 'measured span for integration', false do
before { subject }
let(:span) { spans[0] }
end
end
describe '#read' do
subject(:read) { cache.read(key) }
before { cache.write(key, 50) }
it_behaves_like 'an instrumented cache method'
it do
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 eq(key)
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
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
subject(:read_multi) { cache.read_multi(*multi_keys) }
before { multi_keys.each { |key| cache.write(key, 50 + key[-1].to_i) } }
it_behaves_like 'an instrumented cache method'
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]
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(JSON.parse(get.get_tag('rails.cache.keys'))).to eq(multi_keys)
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
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]
expect(get.name).to eq('rails.cache')
expect(get.get_tag('rails.cache.keys')).to be_nil
end
end
end
describe '#write' do
subject(:write) { cache.write(key, 50) }
it_behaves_like 'an instrumented cache method'
it do
write
expect(span.name).to eq('rails.cache')
expect(span.type).to eq('cache')
expect(span.resource).to eq('SET')
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 eq(key)
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
context 'with custom cache_service' do
before do
Datadog.configuration.tracing[:active_support][:cache_service] = 'service-cache'
end
it 'uses the proper service name' do
write
expect(span.service).to eq('service-cache')
end
end
context 'with complex cache key' do
let(:key) { ['custom-key', %w[x y], user] }
let(:user) { double('User', cache_key: 'User:3') }
it 'expands key using ActiveSupport' do
write
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
let(:values) { multi_keys.map { |k| 50 + k[-1].to_i } }
subject(:write_multi) { cache.write_multi(Hash[multi_keys.zip(values)], opt_name: :opt_value) }
context 'when the method is defined' do
before do
unless ::ActiveSupport::Cache::Store.public_method_defined?(:write_multi)
skip 'Test is not applicable to this Rails version'
end
end
it_behaves_like 'an instrumented cache method'
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(JSON.parse(span.get_tag('rails.cache.keys'))).to eq(multi_keys)
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
context 'with custom cache_service' do
before { Datadog.configuration.tracing[:active_support][:cache_service] = 'service-cache' }
it 'uses the proper service name' do
write_multi
expect(span.service).to eq('service-cache')
end
end
context 'with complex cache key' do
let(:key) { ['custom-key', %w[x y], user] }
let(:user) { double('User', cache_key: 'User:3') }
it 'expands key using ActiveSupport' do
cache.write_multi(key => 0)
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
before do
skip 'Test is not applicable to this Rails version' if ::ActiveSupport::Cache::Store.public_method_defined?(:fetch)
end
it do
expect(::ActiveSupport::Cache::Store.ancestors).not_to(
include(::Datadog::Tracing::Contrib::ActiveSupport::Cache::Instrumentation::Fetch)
)
end
it do
expect { subject }.to raise_error NoMethodError
end
end
end
describe '#delete' do
subject(:delete) { cache.delete(key) }
it_behaves_like 'an instrumented cache method'
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 eq(key)
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
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
subject(:fetch) { cache.fetch(key) { 'default' } }
it_behaves_like 'an instrumented cache method'
context 'with exception' do
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 eq('exception')
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
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
subject(:fetch_multi) { cache.fetch_multi(*multi_keys, expires_in: 42) { |key| 50 + key[-1].to_i } }
context 'when the method is defined' do
before do
unless ::ActiveSupport::Cache::Store.public_method_defined?(:fetch_multi)
skip 'Test is not applicable to this Rails version'
end
end
it_behaves_like 'an instrumented cache method'
context 'with exception' do
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 eq('["exception", "another", "one"]')
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
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
before do
if ::ActiveSupport::Cache::Store.public_method_defined?(:fetch_multi)
skip 'Test is not applicable to this Rails version'
end
end
it do
expect(::ActiveSupport::Cache::Store.ancestors).not_to(
include(::Datadog::Tracing::Contrib::ActiveSupport::Cache::Instrumentation::FetchMulti)
)
end
it do
expect { subject }.to raise_error NoMethodError
end
end
end
context 'with very large cache key' do
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 have(max_key_size).items
expect(span.get_tag('rails.cache.key')).to end_with('...')
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
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