Skip to content

Commit 5b1c09d

Browse files
authored
fix: ActiveJob Propagate baggage information properly when performing (#1214)
1 parent fcb40da commit 5b1c09d

File tree

2 files changed

+33
-30
lines changed
  • instrumentation/active_job
    • lib/opentelemetry/instrumentation/active_job/handlers
    • test/opentelemetry/instrumentation/active_job/handlers

2 files changed

+33
-30
lines changed

instrumentation/active_job/lib/opentelemetry/instrumentation/active_job/handlers/perform.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,18 @@ def start_span(name, _id, payload)
3434
span = tracer.start_root_span(span_name, kind: :consumer, attributes: @mapper.call(payload), links: links)
3535
end
3636

37-
{ span: span, ctx_token: attach_consumer_context(span) }
37+
{ span: span, ctx_token: attach_consumer_context(span, parent_context) }
3838
end
3939

4040
# This method attaches a span to multiple contexts:
4141
# 1. Registers the ingress span as the top level ActiveJob span.
4242
# This is used later to enrich the ingress span in children, e.g. setting span status to error when a child event like `discard` terminates due to an error
4343
# 2. Registers the ingress span as the "active" span, which is the default behavior of the SDK.
4444
# @param span [OpenTelemetry::Trace::Span] the currently active span used to record the exception and set the status
45+
# @param parent_context [Context] The context to use as the parent for the consumer context
4546
# @return [Numeric] Context token that must be detached when finished
46-
def attach_consumer_context(span)
47-
consumer_context = OpenTelemetry::Trace.context_with_span(span)
47+
def attach_consumer_context(span, parent_context)
48+
consumer_context = OpenTelemetry::Trace.context_with_span(span, parent_context: parent_context)
4849
internal_context = OpenTelemetry::Instrumentation::ActiveJob.context_with_span(span, parent_context: consumer_context)
4950

5051
OpenTelemetry::Context.attach(internal_context)

instrumentation/active_job/test/opentelemetry/instrumentation/active_job/handlers/perform_test.rb

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -188,21 +188,6 @@
188188
_(process_span.links[0].span_context.span_id).must_equal(publish_span.span_id)
189189
end
190190

191-
it 'propagates baggage' do
192-
ctx = OpenTelemetry::Baggage.set_value('testing_baggage', 'it_worked')
193-
OpenTelemetry::Context.with_current(ctx) do
194-
BaggageJob.perform_later
195-
end
196-
197-
_(publish_span.trace_id).wont_equal(process_span.trace_id)
198-
199-
_(process_span.total_recorded_links).must_equal(1)
200-
_(process_span.links[0].span_context.trace_id).must_equal(publish_span.trace_id)
201-
_(process_span.links[0].span_context.span_id).must_equal(publish_span.span_id)
202-
203-
_(process_span.attributes['success']).must_equal(true)
204-
end
205-
206191
describe 'with an async queue adapter' do
207192
before do
208193
begin
@@ -225,6 +210,21 @@
225210
_(process_span.links[0].span_context.trace_id).must_equal(publish_span.trace_id)
226211
_(process_span.links[0].span_context.span_id).must_equal(publish_span.span_id)
227212
end
213+
214+
it 'propagates baggage' do
215+
ctx = OpenTelemetry::Baggage.set_value('testing_baggage', 'it_worked')
216+
OpenTelemetry::Context.with_current(ctx) do
217+
BaggageJob.perform_later
218+
end
219+
perform_enqueued_jobs
220+
221+
_(publish_span.trace_id).wont_equal(process_span.trace_id)
222+
223+
_(process_span.total_recorded_links).must_equal(1)
224+
_(process_span.links[0].span_context.trace_id).must_equal(publish_span.trace_id)
225+
_(process_span.links[0].span_context.span_id).must_equal(publish_span.span_id)
226+
_(process_span.attributes['success']).must_equal(true)
227+
end
228228
end
229229
end
230230

@@ -251,18 +251,6 @@
251251
_(process_span.parent_span_id).must_equal(publish_span.span_id)
252252
end
253253

254-
it 'propagates baggage' do
255-
ctx = OpenTelemetry::Baggage.set_value('testing_baggage', 'it_worked')
256-
OpenTelemetry::Context.with_current(ctx) do
257-
BaggageJob.perform_later
258-
end
259-
_(process_span.total_recorded_links).must_equal(0)
260-
261-
_(publish_span.trace_id).must_equal(process_span.trace_id)
262-
_(process_span.parent_span_id).must_equal(publish_span.span_id)
263-
_(process_span.attributes['success']).must_equal(true)
264-
end
265-
266254
describe 'with an async queue adapter' do
267255
before do
268256
begin
@@ -284,6 +272,20 @@
284272
_(publish_span.trace_id).must_equal(process_span.trace_id)
285273
_(process_span.parent_span_id).must_equal(publish_span.span_id)
286274
end
275+
276+
it 'propagates baggage' do
277+
ctx = OpenTelemetry::Baggage.set_value('testing_baggage', 'it_worked')
278+
OpenTelemetry::Context.with_current(ctx) do
279+
BaggageJob.perform_later
280+
end
281+
perform_enqueued_jobs
282+
283+
_(process_span.total_recorded_links).must_equal(0)
284+
285+
_(publish_span.trace_id).must_equal(process_span.trace_id)
286+
_(process_span.parent_span_id).must_equal(publish_span.span_id)
287+
_(process_span.attributes['success']).must_equal(true)
288+
end
287289
end
288290
end
289291

0 commit comments

Comments
 (0)