Skip to content

Commit

Permalink
fix(active-job): honour dynamic changes in configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
lavoiesl committed Jul 22, 2024
1 parent 246fc10 commit 432007d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@ module ActiveJob
module Handlers
# Handles `enqueue.active_job` and `enqueue_at.active_job` to generate egress spans
class Enqueue < Default
def initialize(...)
super
@span_name_formatter = if @config[:span_naming] == :job_class
->(job) { "#{job.class.name} publish" }
else
->(job) { "#{job.queue_name} publish" }
end
end

# Overrides the `Default#start_span` method to create an egress span
# and registers it with the current context
#
Expand All @@ -28,10 +19,22 @@ def initialize(...)
# @return [Hash] with the span and generated context tokens
def start_span(name, _id, payload)
job = payload.fetch(:job)
span = tracer.start_span(@span_name_formatter.call(job), kind: :producer, attributes: @mapper.call(payload))
span = tracer.start_span(span_name(job), kind: :producer, attributes: @mapper.call(payload))
OpenTelemetry.propagation.inject(job.__otel_headers) # This must be transmitted over the wire
{ span: span, ctx_token: OpenTelemetry::Context.attach(OpenTelemetry::Trace.context_with_span(span)) }
end

private

def span_name(job)
prefix = if @config[:span_naming] == :job_class
job.class.name
else
job.queue_name
end

"#{prefix} publish"
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@ module ActiveJob
module Handlers
# Handles perform.active_job to generate ingress spans
class Perform < Default
def initialize(...)
super
@span_name_formatter = if @config[:span_naming] == :job_class
->(job) { "#{job.class.name} process" }
else
->(job) { "#{job.queue_name} process" }
end
end

# Overrides the `Default#start_span` method to create an ingress span
# and registers it with the current context
#
Expand All @@ -30,7 +21,7 @@ def start_span(name, _id, payload)
job = payload.fetch(:job)
parent_context = OpenTelemetry.propagation.extract(job.__otel_headers)

span_name = @span_name_formatter.call(job)
span_name = span_name(job)

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

OpenTelemetry::Context.attach(internal_context)
end

private

def span_name(job)
prefix = if @config[:span_naming] == :job_class
job.class.name
else
job.queue_name
end

"#{prefix} process"
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,22 @@
_(CallbacksJob.context_after).must_be :valid?
end
end

describe 'with a configuration modified after installation' do
let(:job_class) { TestJob }
let(:publish_span) { spans.find { |s| s.name == "#{job_class.name} publish" } }
let(:process_span) { spans.find { |s| s.name == "#{job_class.name} process" } }

before do
instance_config = instrumentation.instance_variable_get(:@config)
instance_config[:span_naming] = :job_class
end

it 'uses the updated configuration' do
TestJob.perform_later

_(publish_span).wont_be_nil
_(process_span).wont_be_nil
end
end
end

0 comments on commit 432007d

Please sign in to comment.