Skip to content

Commit df6e43f

Browse files
authored
fix(active-job): Honour dynamic changes in configuration (#1079)
fix(active-job): honour dynamic changes in configuration
1 parent e7377e0 commit df6e43f

File tree

3 files changed

+44
-20
lines changed

3 files changed

+44
-20
lines changed

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,6 @@ module ActiveJob
1010
module Handlers
1111
# Handles `enqueue.active_job` and `enqueue_at.active_job` to generate egress spans
1212
class Enqueue < Default
13-
def initialize(...)
14-
super
15-
@span_name_formatter = if @config[:span_naming] == :job_class
16-
->(job) { "#{job.class.name} publish" }
17-
else
18-
->(job) { "#{job.queue_name} publish" }
19-
end
20-
end
21-
2213
# Overrides the `Default#start_span` method to create an egress span
2314
# and registers it with the current context
2415
#
@@ -28,10 +19,22 @@ def initialize(...)
2819
# @return [Hash] with the span and generated context tokens
2920
def start_span(name, _id, payload)
3021
job = payload.fetch(:job)
31-
span = tracer.start_span(@span_name_formatter.call(job), kind: :producer, attributes: @mapper.call(payload))
22+
span = tracer.start_span(span_name(job), kind: :producer, attributes: @mapper.call(payload))
3223
OpenTelemetry.propagation.inject(job.__otel_headers) # This must be transmitted over the wire
3324
{ span: span, ctx_token: OpenTelemetry::Context.attach(OpenTelemetry::Trace.context_with_span(span)) }
3425
end
26+
27+
private
28+
29+
def span_name(job)
30+
prefix = if @config[:span_naming] == :job_class
31+
job.class.name
32+
else
33+
job.queue_name
34+
end
35+
36+
"#{prefix} publish"
37+
end
3538
end
3639
end
3740
end

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,6 @@ module ActiveJob
1010
module Handlers
1111
# Handles perform.active_job to generate ingress spans
1212
class Perform < Default
13-
def initialize(...)
14-
super
15-
@span_name_formatter = if @config[:span_naming] == :job_class
16-
->(job) { "#{job.class.name} process" }
17-
else
18-
->(job) { "#{job.queue_name} process" }
19-
end
20-
end
21-
2213
# Overrides the `Default#start_span` method to create an ingress span
2314
# and registers it with the current context
2415
#
@@ -30,7 +21,7 @@ def start_span(name, _id, payload)
3021
job = payload.fetch(:job)
3122
parent_context = OpenTelemetry.propagation.extract(job.__otel_headers)
3223

33-
span_name = @span_name_formatter.call(job)
24+
span_name = span_name(job)
3425

3526
# TODO: Refactor into a propagation strategy
3627
propagation_style = @config[:propagation_style]
@@ -57,6 +48,18 @@ def attach_consumer_context(span)
5748

5849
OpenTelemetry::Context.attach(internal_context)
5950
end
51+
52+
private
53+
54+
def span_name(job)
55+
prefix = if @config[:span_naming] == :job_class
56+
job.class.name
57+
else
58+
job.queue_name
59+
end
60+
61+
"#{prefix} process"
62+
end
6063
end
6164
end
6265
end

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,4 +261,22 @@
261261
_(CallbacksJob.context_after).must_be :valid?
262262
end
263263
end
264+
265+
describe 'with a configuration modified after installation' do
266+
let(:job_class) { TestJob }
267+
let(:publish_span) { spans.find { |s| s.name == "#{job_class.name} publish" } }
268+
let(:process_span) { spans.find { |s| s.name == "#{job_class.name} process" } }
269+
270+
before do
271+
instance_config = instrumentation.instance_variable_get(:@config)
272+
instance_config[:span_naming] = :job_class
273+
end
274+
275+
it 'uses the updated configuration' do
276+
TestJob.perform_later
277+
278+
_(publish_span).wont_be_nil
279+
_(process_span).wont_be_nil
280+
end
281+
end
264282
end

0 commit comments

Comments
 (0)