-
-
Notifications
You must be signed in to change notification settings - Fork 512
/
Copy pathdelayed_job_spec.rb
469 lines (369 loc) · 15.3 KB
/
delayed_job_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
require "spec_helper"
RSpec.describe Sentry::DelayedJob do
before do
perform_basic_setup
end
let(:transport) do
Sentry.get_current_client.transport
end
class Post
def raise_error
1 / 0
end
def tagged_error(number: 1)
Sentry.set_tags(number: number)
raise
end
def tagged_report(number: 1)
Sentry.set_tags(number: number)
Sentry.capture_message("tagged report")
end
def report
Sentry.capture_message("report")
end
def do_nothing
end
def self.class_do_nothing
end
def do_nothing_with_args(a)
end
end
it "sets correct extra/tags context for each job" do
Post.new.delay.report
enqueued_job = Delayed::Backend::ActiveRecord::Job.last
enqueued_job.invoke_job
expect(transport.events.count).to eq(1)
event = transport.events.last.to_hash
expect(event[:message]).to eq("report")
expect(event[:contexts][:"Delayed-Job"][:id]).to eq(enqueued_job.id.to_s)
expect(event[:tags]).to eq({ "delayed_job.id" => enqueued_job.id.to_s, "delayed_job.queue" => nil })
end
it "doesn't leak scope data outside of the job" do
Post.new.delay.report
enqueued_job = Delayed::Backend::ActiveRecord::Job.last
enqueued_job.invoke_job
expect(transport.events.count).to eq(1)
expect(Sentry.get_current_scope.extra).to eq({})
expect(Sentry.get_current_scope.tags).to eq({})
end
it "doesn't share scope data between jobs" do
Post.new.delay.tagged_report
enqueued_job = Delayed::Backend::ActiveRecord::Job.last
enqueued_job.invoke_job
expect(transport.events.count).to eq(1)
event = transport.events.last.to_hash
expect(event[:message]).to eq("tagged report")
expect(event[:tags]).to eq({ "delayed_job.id" => enqueued_job.id.to_s, "delayed_job.queue" => nil, number: 1 })
Post.new.delay.report
enqueued_job = Delayed::Backend::ActiveRecord::Job.last
enqueued_job.invoke_job
expect(transport.events.count).to eq(2)
event = transport.events.last.to_hash
expect(event[:tags]).to eq({ "delayed_job.id" => enqueued_job.id.to_s, "delayed_job.queue" => nil })
end
context "when a job failed" do
let(:enqueued_job) do
Post.new.delay.raise_error
enqueued_job = Delayed::Backend::ActiveRecord::Job.last
end
it "reports exception" do
expect do
enqueued_job.invoke_job
end.to raise_error(ZeroDivisionError)
expect(transport.events.count).to eq(1)
event = transport.events.last.to_hash
expect(event[:sdk]).to eq({ name: "sentry.ruby.delayed_job", version: described_class::VERSION })
expect(event.dig(:exception, :values, 0, :type)).to eq("ZeroDivisionError")
expect(event[:tags]).to eq({ "delayed_job.id" => enqueued_job.id.to_s, "delayed_job.queue" => nil })
end
it "doesn't leak scope data" do
Post.new.delay.tagged_error
enqueued_job = Delayed::Backend::ActiveRecord::Job.last
expect do
enqueued_job.invoke_job
end.to raise_error(RuntimeError)
expect(transport.events.count).to eq(1)
event = transport.events.last.to_hash
expect(event[:tags]).to eq({ "delayed_job.id" => enqueued_job.id.to_s, "delayed_job.queue" => nil, number: 1 })
expect(Sentry.get_current_scope.extra).to eq({})
expect(Sentry.get_current_scope.tags).to eq({})
Post.new.delay.raise_error
enqueued_job = Delayed::Backend::ActiveRecord::Job.last
expect do
enqueued_job.invoke_job
end.to raise_error(ZeroDivisionError)
expect(transport.events.count).to eq(2)
event = transport.events.last.to_hash
expect(event[:tags]).to eq({ "delayed_job.id" => enqueued_job.id.to_s, "delayed_job.queue" => nil })
expect(Sentry.get_current_scope.extra).to eq({})
expect(Sentry.get_current_scope.tags).to eq({})
end
context "with report_after_job_retries set to true" do
before do
Sentry.configuration.delayed_job.report_after_job_retries = true
end
after do
Sentry.configuration.delayed_job.report_after_job_retries = false
end
it "reports exception after the last retry" do
enqueued_job.update(attempts: Delayed::Worker.max_attempts.succ)
expect do
enqueued_job.invoke_job
end.to raise_error(ZeroDivisionError)
expect(transport.events.count).to eq(1)
end
# Default max_attemps is defined on Delayed::Worker.max_attempts == 25.
# However, users can customize max_attempts on the job class, and DelayedJob
# will respect that.
# Sentry needs to report an exception if report_after_retries is true and
# custom job-level max_attempts is reached.
# See https://github.com/collectiveidea/delayed_job#custom-jobs
it "reports exception after the job's custom max_attempts" do
enqueued_job.update(attempts: 2)
allow(enqueued_job).to receive(:max_attempts).and_return(3)
expect do
enqueued_job.invoke_job
end.to raise_error(ZeroDivisionError)
expect(transport.events.count).to eq(1)
end
it "skips report if not on the last retry" do
enqueued_job.update(attempts: 0)
expect do
enqueued_job.invoke_job
end.to raise_error(ZeroDivisionError)
expect(transport.events.count).to eq(0)
end
end
end
context "with ActiveJob" do
require "rails"
require "active_job"
require "sentry-rails"
class ReportingJob < ActiveJob::Base
self.queue_adapter = :delayed_job
def perform
Sentry.set_tags(number: 1)
Sentry.capture_message("report from ActiveJob")
end
end
class FailedJob < ActiveJob::Base
self.queue_adapter = :delayed_job
def perform
Sentry.set_tags(number: 2)
1 / 0
end
end
before do
ActiveJob::Base.logger = nil
# because we don't create a full Rails app here, we need to apply sentry-rails' ActiveJob extension manually
require "sentry/rails/active_job"
ActiveJob::Base.send(:prepend, Sentry::Rails::ActiveJobExtensions)
if Sentry.configuration.rails.skippable_job_adapters.empty?
Sentry.configuration.rails.skippable_job_adapters << "ActiveJob::QueueAdapters::DelayedJobAdapter"
end
end
context "when the job succeeded" do
before do
ReportingJob.perform_later
enqueued_job = Delayed::Backend::ActiveRecord::Job.last
enqueued_job.invoke_job
end
it "doesn't leak scope data" do
expect(transport.events.count).to eq(1)
expect(Sentry.get_current_scope.tags).to eq({})
end
it "injects ActiveJob information to the event" do
expect(transport.events.count).to eq(1)
event = transport.events.last.to_hash
expect(event[:message]).to eq("report from ActiveJob")
expect(event[:tags]).to match({ "delayed_job.id" => anything, "delayed_job.queue" => "default", number: 1 })
expect(event[:contexts][:"Active-Job"][:job_class]).to eq("ReportingJob")
end
end
context "when the job failed" do
before do
FailedJob.perform_later
enqueued_job = Delayed::Backend::ActiveRecord::Job.last
expect do
enqueued_job.invoke_job
end.to raise_error(ZeroDivisionError)
end
it "doesn't duplicate error reporting" do
expect(transport.events.count).to eq(1)
expect(Sentry.get_current_scope.tags).to eq({})
end
it "injects ActiveJob information to the event" do
expect(transport.events.count).to eq(1)
event = transport.events.last.to_hash
expect(event.dig(:exception, :values, 0, :type)).to eq("ZeroDivisionError")
expect(event[:tags]).to match({ "delayed_job.id" => anything, "delayed_job.queue" => "default", number: 2 })
expect(event[:contexts][:"Active-Job"][:job_class]).to eq("FailedJob")
end
end
context "when tracing is enabled" do
before do
perform_basic_setup do |config|
config.traces_sample_rate = 1.0
config.rails.skippable_job_adapters << "ActiveJob::QueueAdapters::DelayedJobAdapter"
end
end
it "records transaction" do
ReportingJob.perform_later
enqueued_job = Delayed::Backend::ActiveRecord::Job.last
enqueued_job.invoke_job
expect(transport.events.count).to eq(2)
transaction = transport.events.last
expect(transaction.transaction).to eq("ReportingJob")
expect(transaction.contexts.dig(:trace, :trace_id)).to be_a(String)
expect(transaction.contexts.dig(:trace, :span_id)).to be_a(String)
expect(transaction.contexts.dig(:trace, :status)).to eq("ok")
end
it "passes job context into the sampling context" do
expect_any_instance_of(Sentry::Transaction).to receive(:set_initial_sample_decision) do |**args|
expect(args.dig(:sampling_context, Sentry::DelayedJob::Plugin::DELAYED_JOB_CONTEXT_KEY, :priority)).to eq(7)
expect(args.dig(:sampling_context, Sentry::DelayedJob::Plugin::ACTIVE_JOB_CONTEXT_KEY, :job_class)).to eq('ReportingJob')
end
ReportingJob.set(priority: 7).perform_later
enqueued_job = Delayed::Backend::ActiveRecord::Job.last
enqueued_job.invoke_job
end
it "records transaction with exception" do
FailedJob.perform_later
enqueued_job = Delayed::Backend::ActiveRecord::Job.last
begin
enqueued_job.invoke_job
rescue ZeroDivisionError
nil
end
expect(transport.events.count).to eq(2)
transaction = transport.events.last
expect(transaction.transaction).to eq("FailedJob")
expect(transaction.contexts.dig(:trace, :trace_id)).to be_a(String)
expect(transaction.contexts.dig(:trace, :span_id)).to be_a(String)
expect(transaction.contexts.dig(:trace, :status)).to eq("internal_error")
event = transport.events.last
expect(event.contexts.dig(:trace, :trace_id)).to eq(transaction.contexts.dig(:trace, :trace_id))
end
context "with instrumenter :otel" do
before do
perform_basic_setup do |config|
config.traces_sample_rate = 1.0
config.instrumenter = :otel
config.rails.skippable_job_adapters << "ActiveJob::QueueAdapters::DelayedJobAdapter"
end
end
it "does not record transaction" do
FailedJob.perform_later
enqueued_job = Delayed::Backend::ActiveRecord::Job.last
begin
enqueued_job.invoke_job
rescue ZeroDivisionError
nil
end
expect(transport.events.count).to eq(1)
event = transport.events.last
expect(event).to be_a(Sentry::ErrorEvent)
end
end
end
end
context ".compute_job_class" do
it 'returns the class and method name for a delayed instance method call' do
Post.new.delay.do_nothing
enqueued_job = Delayed::Backend::ActiveRecord::Job.last
expect(Sentry::DelayedJob::Plugin.compute_job_class(enqueued_job.payload_object)).to eq("Post#do_nothing")
end
it 'returns the class and method name for a delayed class method call' do
Post.delay.class_do_nothing
enqueued_job = Delayed::Backend::ActiveRecord::Job.last
expect(Sentry::DelayedJob::Plugin.compute_job_class(enqueued_job.payload_object)).to eq("Post#class_do_nothing")
end
it 'returns the class name for anything else' do
expect(Sentry::DelayedJob::Plugin.compute_job_class("something")).to eq("String")
expect(Sentry::DelayedJob::Plugin.compute_job_class(Sentry::DelayedJob::Plugin)).to eq("Class")
end
end
context "when tracing is enabled" do
before do
perform_basic_setup do |config|
config.traces_sample_rate = 1.0
end
end
it "records transaction" do
Post.new.delay.do_nothing
enqueued_job = Delayed::Backend::ActiveRecord::Job.last
enqueued_job.invoke_job
expect(transport.events.count).to eq(1)
transaction = transport.events.last
expect(transaction.transaction).to eq("Post#do_nothing")
expect(transaction.transaction_info).to eq({ source: :task })
expect(transaction.contexts.dig(:trace, :trace_id)).to be_a(String)
expect(transaction.contexts.dig(:trace, :span_id)).to be_a(String)
expect(transaction.contexts.dig(:trace, :status)).to eq("ok")
expect(transaction.contexts.dig(:trace, :op)).to eq("queue.delayed_job")
end
it "records transaction with exception" do
Post.new.delay.raise_error
enqueued_job = Delayed::Backend::ActiveRecord::Job.last
begin
enqueued_job.invoke_job
rescue ZeroDivisionError
nil
end
expect(transport.events.count).to eq(2)
transaction = transport.events.last
expect(transaction.transaction).to eq("Post#raise_error")
expect(transaction.transaction_info).to eq({ source: :task })
expect(transaction.contexts.dig(:trace, :trace_id)).to be_a(String)
expect(transaction.contexts.dig(:trace, :span_id)).to be_a(String)
expect(transaction.contexts.dig(:trace, :status)).to eq("internal_error")
event = transport.events.last
expect(event.contexts.dig(:trace, :trace_id)).to eq(transaction.contexts.dig(:trace, :trace_id))
end
context "with upstream trace" do
before do
transaction = Sentry.start_transaction
Sentry.get_current_scope.set_span(transaction)
Post.new.delay.do_nothing_with_args(1)
end
let(:parent_transaction) { Sentry.get_current_scope.span }
let(:enqueued_job) { Delayed::Backend::ActiveRecord::Job.last }
it "injects the trace propagation headers to args for PerformableMethod" do
payload_object = enqueued_job.payload_object
expect(payload_object).to be_a(Delayed::PerformableMethod)
expect(payload_object.args.last).to include(:sentry)
expect(payload_object.args.last[:sentry]["sentry-trace"]).to eq(parent_transaction.to_sentry_trace)
expect(payload_object.args.last[:sentry]["baggage"]).to eq(parent_transaction.to_baggage)
end
it "invokes the job with correct args" do
payload_object = enqueued_job.payload_object
expect(payload_object.object).to be_a(Post)
expect(payload_object.object).to receive(:do_nothing_with_args).with(1)
enqueued_job.invoke_job
end
it "continues the trace" do
enqueued_job.invoke_job
expect(transport.events.count).to eq(1)
transaction = transport.events.last
expect(transaction.transaction).to eq("Post#do_nothing_with_args")
expect(transaction.contexts.dig(:trace, :trace_id)).to eq(parent_transaction.trace_id)
expect(transaction.contexts.dig(:trace, :parent_span_id)).to eq(parent_transaction.span_id)
expect(transaction.dynamic_sampling_context).to eq(parent_transaction.get_baggage.dynamic_sampling_context)
end
end
end
end
RSpec.describe Sentry::DelayedJob, "not initialized" do
class Thing
def self.invoked_method; end
end
it "doesn't swallow jobs" do
expect(Thing).to receive(:invoked_method)
Delayed::Job.delete_all
expect(Delayed::Job.count).to eq(0)
Thing.delay.invoked_method
expect(Delayed::Job.count).to eq(1)
Delayed::Worker.new.run(Delayed::Job.last)
expect(Delayed::Job.count).to eq(0)
end
end