Skip to content

Commit 579cfbd

Browse files
chore: Stabilize ActiveJob Instrumentation Tests (#1222)
* chore: Stabilize ActiveJob Instrumentation Tests Prior to this change tests would intermittently fail due to threads executing out of order. This change ensures that the AsyncAdapter runs in the main thread and blocks execution to ensure jobs are completed before making assertions on spans. Co-authored-by: Andrew Hayworth <[email protected]>
1 parent 50408cf commit 579cfbd

File tree

2 files changed

+32
-69
lines changed

2 files changed

+32
-69
lines changed

instrumentation/active_job/test/instrumentation/active_job/patches/active_job_callbacks_test.rb

Lines changed: 31 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,18 @@
2222
before do
2323
instrumentation.instance_variable_set(:@config, config)
2424
exporter.reset
25+
26+
::ActiveJob::Base.queue_adapter = :async
27+
::ActiveJob::Base.queue_adapter.immediate = true
2528
end
2629

2730
after do
31+
begin
32+
::ActiveJob::Base.queue_adapter.shutdown
33+
rescue StandardError
34+
nil
35+
end
36+
::ActiveJob::Base.queue_adapter = :inline
2837
instrumentation.instance_variable_set(:@config, config)
2938
end
3039

@@ -77,23 +86,24 @@
7786

7887
describe 'span kind' do
7988
it 'sets correct span kinds for inline jobs' do
89+
begin
90+
::ActiveJob::Base.queue_adapter.shutdown
91+
rescue StandardError
92+
nil
93+
end
94+
::ActiveJob::Base.queue_adapter = :inline
95+
8096
TestJob.perform_later
8197

8298
_(send_span.kind).must_equal(:client)
8399
_(process_span.kind).must_equal(:server)
84100
end
85101

86102
it 'sets correct span kinds for all other jobs' do
87-
# Change the queue adapter so we get the right behavior
88-
::ActiveJob::Base.queue_adapter = :async
89-
90103
TestJob.perform_later
91-
::ActiveJob::Base.queue_adapter.shutdown
92104

93105
_(send_span.kind).must_equal(:producer)
94106
_(process_span.kind).must_equal(:consumer)
95-
ensure
96-
::ActiveJob::Base.queue_adapter = :inline
97107
end
98108
end
99109

@@ -115,17 +125,11 @@
115125
end
116126

117127
it 'is set correctly for async jobs' do
118-
::ActiveJob::Base.queue_adapter = :async
119-
120128
TestJob.perform_later
121-
::ActiveJob::Base.queue_adapter.shutdown
122129

123130
[send_span, process_span].each do |span|
124131
_(span.attributes['net.transport']).must_equal('inproc')
125132
end
126-
127-
ensure
128-
::ActiveJob::Base.queue_adapter = :inline
129133
end
130134
end
131135

@@ -157,25 +161,26 @@
157161
end
158162

159163
it 'is set correctly for jobs that do wait' do
160-
::ActiveJob::Base.queue_adapter = :async
161-
162164
job = TestJob.set(wait: 0.second).perform_later
163-
::ActiveJob::Base.queue_adapter.shutdown
164165

165166
# Only the sending span is a 'scheduled' thing
166167
_(send_span.attributes['messaging.active_job.scheduled_at']).must_equal(job.scheduled_at)
167168
assert(send_span.attributes['messaging.active_job.scheduled_at'])
168169

169170
# The processing span isn't a 'scheduled' thing
170171
_(process_span.attributes['messaging.active_job.scheduled_at']).must_be_nil
171-
172-
ensure
173-
::ActiveJob::Base.queue_adapter = :inline
174172
end
175173
end
176174

177175
describe 'messaging.system' do
178176
it 'is set correctly for the inline adapter' do
177+
begin
178+
::ActiveJob::Base.queue_adapter.shutdown
179+
rescue StandardError
180+
nil
181+
end
182+
183+
::ActiveJob::Base.queue_adapter = :inline
179184
TestJob.perform_later
180185

181186
[send_span, process_span].each do |span|
@@ -184,17 +189,11 @@
184189
end
185190

186191
it 'is set correctly for the async adapter' do
187-
::ActiveJob::Base.queue_adapter = :async
188-
189192
TestJob.perform_later
190-
::ActiveJob::Base.queue_adapter.shutdown
191193

192194
[send_span, process_span].each do |span|
193195
_(span.attributes['messaging.system']).must_equal('async')
194196
end
195-
196-
ensure
197-
::ActiveJob::Base.queue_adapter = :inline
198197
end
199198
end
200199

@@ -205,20 +204,14 @@
205204
end
206205

207206
it 'tracks correctly for jobs that do retry' do
208-
::ActiveJob::Base.queue_adapter = :async
209-
210-
RetryJob.perform_now
211-
::ActiveJob::Base.queue_adapter.shutdown
212-
213-
# 1 enqueue, 2 perform
214-
_(spans.count).must_equal(3)
215-
216-
span = spans.last
217-
_(span.kind).must_equal(:consumer)
218-
_(span.attributes['messaging.active_job.executions']).must_equal(2)
207+
begin
208+
RetryJob.perform_later
209+
rescue StandardError
210+
nil
211+
end
219212

220-
ensure
221-
::ActiveJob::Base.queue_adapter = :inline
213+
executions = spans.filter { |s| s.kind == :consumer }.sum { |s| s.attributes['messaging.active_job.executions'] }
214+
_(executions).must_equal(3) # total of 3 runs. The initial and 2 retries.
222215
end
223216
end
224217

@@ -227,7 +220,7 @@
227220

228221
[send_span, process_span].each do |span|
229222
_(span.attributes['messaging.destination_kind']).must_equal('queue')
230-
_(span.attributes['messaging.system']).must_equal('inline')
223+
_(span.attributes['messaging.system']).must_equal('async')
231224
_(span.attributes['messaging.message_id']).must_equal(job.job_id)
232225
end
233226
end
@@ -299,109 +292,78 @@
299292
# of execution *will* be the context where the job was enqueued, because rails
300293
# ends up doing job.around_enqueue { job.around_perform { block } } inline.
301294
it 'creates span links in separate traces' do
302-
::ActiveJob::Base.queue_adapter = :async
303-
304295
TestJob.perform_later
305-
::ActiveJob::Base.queue_adapter.shutdown
306296

307297
_(send_span.trace_id).wont_equal(process_span.trace_id)
308298

309299
_(process_span.total_recorded_links).must_equal(1)
310300
_(process_span.links[0].span_context.trace_id).must_equal(send_span.trace_id)
311301
_(process_span.links[0].span_context.span_id).must_equal(send_span.span_id)
312-
ensure
313-
::ActiveJob::Base.queue_adapter = :inline
314302
end
315303

316304
it 'propagates baggage' do
317-
::ActiveJob::Base.queue_adapter = :async
318-
319305
ctx = OpenTelemetry::Baggage.set_value('testing_baggage', 'it_worked')
320306
OpenTelemetry::Context.with_current(ctx) do
321307
BaggageJob.perform_later
322308
end
323-
::ActiveJob::Base.queue_adapter.shutdown
324309

325310
_(send_span.trace_id).wont_equal(process_span.trace_id)
326311

327312
_(process_span.total_recorded_links).must_equal(1)
328313
_(process_span.links[0].span_context.trace_id).must_equal(send_span.trace_id)
329314
_(process_span.links[0].span_context.span_id).must_equal(send_span.span_id)
330315
_(process_span.attributes['success']).must_equal(true)
331-
ensure
332-
::ActiveJob::Base.queue_adapter = :inline
333316
end
334317
end
335318

336319
describe 'when configured to do parent/child spans' do
337320
let(:config) { { propagation_style: :child, span_naming: :queue } }
338321

339322
it 'creates a parent/child relationship' do
340-
::ActiveJob::Base.queue_adapter = :async
341-
342323
TestJob.perform_later
343-
::ActiveJob::Base.queue_adapter.shutdown
344324

345325
_(process_span.total_recorded_links).must_equal(0)
346326

347327
_(send_span.trace_id).must_equal(process_span.trace_id)
348328
_(process_span.parent_span_id).must_equal(send_span.span_id)
349-
ensure
350-
::ActiveJob::Base.queue_adapter = :inline
351329
end
352330

353331
it 'propagates baggage' do
354-
::ActiveJob::Base.queue_adapter = :async
355-
356332
ctx = OpenTelemetry::Baggage.set_value('testing_baggage', 'it_worked')
357333
OpenTelemetry::Context.with_current(ctx) do
358334
BaggageJob.perform_later
359335
end
360-
::ActiveJob::Base.queue_adapter.shutdown
361-
362336
_(process_span.total_recorded_links).must_equal(0)
363337

364338
_(send_span.trace_id).must_equal(process_span.trace_id)
365339
_(process_span.parent_span_id).must_equal(send_span.span_id)
366340
_(process_span.attributes['success']).must_equal(true)
367-
ensure
368-
::ActiveJob::Base.queue_adapter = :inline
369341
end
370342
end
371343

372344
describe 'when explicitly configure for no propagation' do
373345
let(:config) { { propagation_style: :none, span_naming: :queue } }
374346

375347
it 'skips link creation and does not create parent/child relationship' do
376-
::ActiveJob::Base.queue_adapter = :async
377-
378348
TestJob.perform_later
379-
::ActiveJob::Base.queue_adapter.shutdown
380349

381350
_(process_span.total_recorded_links).must_equal(0)
382351

383352
_(send_span.trace_id).wont_equal(process_span.trace_id)
384353
_(process_span.parent_span_id).wont_equal(send_span.span_id)
385-
ensure
386-
::ActiveJob::Base.queue_adapter = :inline
387354
end
388355

389356
it 'still propagates baggage' do
390-
::ActiveJob::Base.queue_adapter = :async
391-
392357
ctx = OpenTelemetry::Baggage.set_value('testing_baggage', 'it_worked')
393358
OpenTelemetry::Context.with_current(ctx) do
394359
BaggageJob.perform_later
395360
end
396-
::ActiveJob::Base.queue_adapter.shutdown
397361

398362
_(process_span.total_recorded_links).must_equal(0)
399363

400364
_(send_span.trace_id).wont_equal(process_span.trace_id)
401365
_(process_span.parent_span_id).wont_equal(send_span.span_id)
402366
_(process_span.attributes['success']).must_equal(true)
403-
ensure
404-
::ActiveJob::Base.queue_adapter = :inline
405367
end
406368
end
407369
end

instrumentation/active_job/test/test_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Copyright The OpenTelemetry Authors
44
#
55
# SPDX-License-Identifier: Apache-2.0
6+
ENV['OTEL_LOG_LEVEL'] ||= 'fatal'
67

78
require 'active_job'
89
require 'opentelemetry-instrumentation-active_job'

0 commit comments

Comments
 (0)