From 30b78451e31b15a1bfb41c92396f5b13f21aa33a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Lavoie?= Date: Mon, 22 Jul 2024 10:32:59 -0400 Subject: [PATCH] fix(active-job): honour dynamic changes in configuration --- .../active_job/handlers/enqueue.rb | 23 +++++++++++-------- .../active_job/handlers/perform.rb | 23 +++++++++++-------- .../active_job/handlers/perform_test.rb | 18 +++++++++++++++ 3 files changed, 44 insertions(+), 20 deletions(-) diff --git a/instrumentation/active_job/lib/opentelemetry/instrumentation/active_job/handlers/enqueue.rb b/instrumentation/active_job/lib/opentelemetry/instrumentation/active_job/handlers/enqueue.rb index 4b4c55937..4b1291747 100644 --- a/instrumentation/active_job/lib/opentelemetry/instrumentation/active_job/handlers/enqueue.rb +++ b/instrumentation/active_job/lib/opentelemetry/instrumentation/active_job/handlers/enqueue.rb @@ -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 # @@ -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 diff --git a/instrumentation/active_job/lib/opentelemetry/instrumentation/active_job/handlers/perform.rb b/instrumentation/active_job/lib/opentelemetry/instrumentation/active_job/handlers/perform.rb index bbfd2ed27..853281a5e 100644 --- a/instrumentation/active_job/lib/opentelemetry/instrumentation/active_job/handlers/perform.rb +++ b/instrumentation/active_job/lib/opentelemetry/instrumentation/active_job/handlers/perform.rb @@ -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 # @@ -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] @@ -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 diff --git a/instrumentation/active_job/test/opentelemetry/instrumentation/active_job/handlers/perform_test.rb b/instrumentation/active_job/test/opentelemetry/instrumentation/active_job/handlers/perform_test.rb index cf4c9b976..3227bf62f 100644 --- a/instrumentation/active_job/test/opentelemetry/instrumentation/active_job/handlers/perform_test.rb +++ b/instrumentation/active_job/test/opentelemetry/instrumentation/active_job/handlers/perform_test.rb @@ -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