Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(active-job): Honour dynamic changes in configuration #1079

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be best to move this to the Default class and also change all the other spans to have a consistent naming, but I'm not sure if we want to lump this change in the PR, this was merely intended to fix a regression

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separate PR to do just that: #1080

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