|
22 | 22 | before do |
23 | 23 | instrumentation.instance_variable_set(:@config, config) |
24 | 24 | exporter.reset |
| 25 | + |
| 26 | + ::ActiveJob::Base.queue_adapter = :async |
| 27 | + ::ActiveJob::Base.queue_adapter.immediate = true |
25 | 28 | end |
26 | 29 |
|
27 | 30 | after do |
| 31 | + begin |
| 32 | + ::ActiveJob::Base.queue_adapter.shutdown |
| 33 | + rescue StandardError |
| 34 | + nil |
| 35 | + end |
| 36 | + ::ActiveJob::Base.queue_adapter = :inline |
28 | 37 | instrumentation.instance_variable_set(:@config, config) |
29 | 38 | end |
30 | 39 |
|
|
77 | 86 |
|
78 | 87 | describe 'span kind' do |
79 | 88 | 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 | + |
80 | 96 | TestJob.perform_later |
81 | 97 |
|
82 | 98 | _(send_span.kind).must_equal(:client) |
83 | 99 | _(process_span.kind).must_equal(:server) |
84 | 100 | end |
85 | 101 |
|
86 | 102 | 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 | | - |
90 | 103 | TestJob.perform_later |
91 | | - ::ActiveJob::Base.queue_adapter.shutdown |
92 | 104 |
|
93 | 105 | _(send_span.kind).must_equal(:producer) |
94 | 106 | _(process_span.kind).must_equal(:consumer) |
95 | | - ensure |
96 | | - ::ActiveJob::Base.queue_adapter = :inline |
97 | 107 | end |
98 | 108 | end |
99 | 109 |
|
|
115 | 125 | end |
116 | 126 |
|
117 | 127 | it 'is set correctly for async jobs' do |
118 | | - ::ActiveJob::Base.queue_adapter = :async |
119 | | - |
120 | 128 | TestJob.perform_later |
121 | | - ::ActiveJob::Base.queue_adapter.shutdown |
122 | 129 |
|
123 | 130 | [send_span, process_span].each do |span| |
124 | 131 | _(span.attributes['net.transport']).must_equal('inproc') |
125 | 132 | end |
126 | | - |
127 | | - ensure |
128 | | - ::ActiveJob::Base.queue_adapter = :inline |
129 | 133 | end |
130 | 134 | end |
131 | 135 |
|
|
157 | 161 | end |
158 | 162 |
|
159 | 163 | it 'is set correctly for jobs that do wait' do |
160 | | - ::ActiveJob::Base.queue_adapter = :async |
161 | | - |
162 | 164 | job = TestJob.set(wait: 0.second).perform_later |
163 | | - ::ActiveJob::Base.queue_adapter.shutdown |
164 | 165 |
|
165 | 166 | # Only the sending span is a 'scheduled' thing |
166 | 167 | _(send_span.attributes['messaging.active_job.scheduled_at']).must_equal(job.scheduled_at) |
167 | 168 | assert(send_span.attributes['messaging.active_job.scheduled_at']) |
168 | 169 |
|
169 | 170 | # The processing span isn't a 'scheduled' thing |
170 | 171 | _(process_span.attributes['messaging.active_job.scheduled_at']).must_be_nil |
171 | | - |
172 | | - ensure |
173 | | - ::ActiveJob::Base.queue_adapter = :inline |
174 | 172 | end |
175 | 173 | end |
176 | 174 |
|
177 | 175 | describe 'messaging.system' do |
178 | 176 | 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 |
179 | 184 | TestJob.perform_later |
180 | 185 |
|
181 | 186 | [send_span, process_span].each do |span| |
|
184 | 189 | end |
185 | 190 |
|
186 | 191 | it 'is set correctly for the async adapter' do |
187 | | - ::ActiveJob::Base.queue_adapter = :async |
188 | | - |
189 | 192 | TestJob.perform_later |
190 | | - ::ActiveJob::Base.queue_adapter.shutdown |
191 | 193 |
|
192 | 194 | [send_span, process_span].each do |span| |
193 | 195 | _(span.attributes['messaging.system']).must_equal('async') |
194 | 196 | end |
195 | | - |
196 | | - ensure |
197 | | - ::ActiveJob::Base.queue_adapter = :inline |
198 | 197 | end |
199 | 198 | end |
200 | 199 |
|
|
205 | 204 | end |
206 | 205 |
|
207 | 206 | 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 |
219 | 212 |
|
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. |
222 | 215 | end |
223 | 216 | end |
224 | 217 |
|
|
227 | 220 |
|
228 | 221 | [send_span, process_span].each do |span| |
229 | 222 | _(span.attributes['messaging.destination_kind']).must_equal('queue') |
230 | | - _(span.attributes['messaging.system']).must_equal('inline') |
| 223 | + _(span.attributes['messaging.system']).must_equal('async') |
231 | 224 | _(span.attributes['messaging.message_id']).must_equal(job.job_id) |
232 | 225 | end |
233 | 226 | end |
|
299 | 292 | # of execution *will* be the context where the job was enqueued, because rails |
300 | 293 | # ends up doing job.around_enqueue { job.around_perform { block } } inline. |
301 | 294 | it 'creates span links in separate traces' do |
302 | | - ::ActiveJob::Base.queue_adapter = :async |
303 | | - |
304 | 295 | TestJob.perform_later |
305 | | - ::ActiveJob::Base.queue_adapter.shutdown |
306 | 296 |
|
307 | 297 | _(send_span.trace_id).wont_equal(process_span.trace_id) |
308 | 298 |
|
309 | 299 | _(process_span.total_recorded_links).must_equal(1) |
310 | 300 | _(process_span.links[0].span_context.trace_id).must_equal(send_span.trace_id) |
311 | 301 | _(process_span.links[0].span_context.span_id).must_equal(send_span.span_id) |
312 | | - ensure |
313 | | - ::ActiveJob::Base.queue_adapter = :inline |
314 | 302 | end |
315 | 303 |
|
316 | 304 | it 'propagates baggage' do |
317 | | - ::ActiveJob::Base.queue_adapter = :async |
318 | | - |
319 | 305 | ctx = OpenTelemetry::Baggage.set_value('testing_baggage', 'it_worked') |
320 | 306 | OpenTelemetry::Context.with_current(ctx) do |
321 | 307 | BaggageJob.perform_later |
322 | 308 | end |
323 | | - ::ActiveJob::Base.queue_adapter.shutdown |
324 | 309 |
|
325 | 310 | _(send_span.trace_id).wont_equal(process_span.trace_id) |
326 | 311 |
|
327 | 312 | _(process_span.total_recorded_links).must_equal(1) |
328 | 313 | _(process_span.links[0].span_context.trace_id).must_equal(send_span.trace_id) |
329 | 314 | _(process_span.links[0].span_context.span_id).must_equal(send_span.span_id) |
330 | 315 | _(process_span.attributes['success']).must_equal(true) |
331 | | - ensure |
332 | | - ::ActiveJob::Base.queue_adapter = :inline |
333 | 316 | end |
334 | 317 | end |
335 | 318 |
|
336 | 319 | describe 'when configured to do parent/child spans' do |
337 | 320 | let(:config) { { propagation_style: :child, span_naming: :queue } } |
338 | 321 |
|
339 | 322 | it 'creates a parent/child relationship' do |
340 | | - ::ActiveJob::Base.queue_adapter = :async |
341 | | - |
342 | 323 | TestJob.perform_later |
343 | | - ::ActiveJob::Base.queue_adapter.shutdown |
344 | 324 |
|
345 | 325 | _(process_span.total_recorded_links).must_equal(0) |
346 | 326 |
|
347 | 327 | _(send_span.trace_id).must_equal(process_span.trace_id) |
348 | 328 | _(process_span.parent_span_id).must_equal(send_span.span_id) |
349 | | - ensure |
350 | | - ::ActiveJob::Base.queue_adapter = :inline |
351 | 329 | end |
352 | 330 |
|
353 | 331 | it 'propagates baggage' do |
354 | | - ::ActiveJob::Base.queue_adapter = :async |
355 | | - |
356 | 332 | ctx = OpenTelemetry::Baggage.set_value('testing_baggage', 'it_worked') |
357 | 333 | OpenTelemetry::Context.with_current(ctx) do |
358 | 334 | BaggageJob.perform_later |
359 | 335 | end |
360 | | - ::ActiveJob::Base.queue_adapter.shutdown |
361 | | - |
362 | 336 | _(process_span.total_recorded_links).must_equal(0) |
363 | 337 |
|
364 | 338 | _(send_span.trace_id).must_equal(process_span.trace_id) |
365 | 339 | _(process_span.parent_span_id).must_equal(send_span.span_id) |
366 | 340 | _(process_span.attributes['success']).must_equal(true) |
367 | | - ensure |
368 | | - ::ActiveJob::Base.queue_adapter = :inline |
369 | 341 | end |
370 | 342 | end |
371 | 343 |
|
372 | 344 | describe 'when explicitly configure for no propagation' do |
373 | 345 | let(:config) { { propagation_style: :none, span_naming: :queue } } |
374 | 346 |
|
375 | 347 | it 'skips link creation and does not create parent/child relationship' do |
376 | | - ::ActiveJob::Base.queue_adapter = :async |
377 | | - |
378 | 348 | TestJob.perform_later |
379 | | - ::ActiveJob::Base.queue_adapter.shutdown |
380 | 349 |
|
381 | 350 | _(process_span.total_recorded_links).must_equal(0) |
382 | 351 |
|
383 | 352 | _(send_span.trace_id).wont_equal(process_span.trace_id) |
384 | 353 | _(process_span.parent_span_id).wont_equal(send_span.span_id) |
385 | | - ensure |
386 | | - ::ActiveJob::Base.queue_adapter = :inline |
387 | 354 | end |
388 | 355 |
|
389 | 356 | it 'still propagates baggage' do |
390 | | - ::ActiveJob::Base.queue_adapter = :async |
391 | | - |
392 | 357 | ctx = OpenTelemetry::Baggage.set_value('testing_baggage', 'it_worked') |
393 | 358 | OpenTelemetry::Context.with_current(ctx) do |
394 | 359 | BaggageJob.perform_later |
395 | 360 | end |
396 | | - ::ActiveJob::Base.queue_adapter.shutdown |
397 | 361 |
|
398 | 362 | _(process_span.total_recorded_links).must_equal(0) |
399 | 363 |
|
400 | 364 | _(send_span.trace_id).wont_equal(process_span.trace_id) |
401 | 365 | _(process_span.parent_span_id).wont_equal(send_span.span_id) |
402 | 366 | _(process_span.attributes['success']).must_equal(true) |
403 | | - ensure |
404 | | - ::ActiveJob::Base.queue_adapter = :inline |
405 | 367 | end |
406 | 368 | end |
407 | 369 | end |
|
0 commit comments