From df11d4c48ce05bb9e3ea2d11f0e12832cc0f50e5 Mon Sep 17 00:00:00 2001 From: Ariel Valentin Date: Sat, 7 Oct 2023 23:31:36 -0500 Subject: [PATCH] fix: Rails 7.1 --- .../instrumentation/active_job/subscriber.rb | 9 ++++++--- .../active_job/subscriber_test.rb | 16 ++++++++++++---- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/instrumentation/active_job/lib/opentelemetry/instrumentation/active_job/subscriber.rb b/instrumentation/active_job/lib/opentelemetry/instrumentation/active_job/subscriber.rb index ac20cfbcf..bcb8c6b6f 100644 --- a/instrumentation/active_job/lib/opentelemetry/instrumentation/active_job/subscriber.rb +++ b/instrumentation/active_job/lib/opentelemetry/instrumentation/active_job/subscriber.rb @@ -22,7 +22,7 @@ def call(payload) 'rails.active_job.execution.counter' => job.executions.to_i, 'rails.active_job.provider_job_id' => job.provider_job_id.to_s, 'rails.active_job.priority' => job.priority, - 'rails.active_job.scheduled_at' => job.scheduled_at + 'rails.active_job.scheduled_at' => job.scheduled_at&.to_f } otel_attributes['net.transport'] = 'inproc' if TEST_ADAPTERS.include?(job.class.queue_adapter_name) @@ -136,7 +136,7 @@ def start(name, id, payload) OpenTelemetry.handle_error(exception: e) end - super + super if ::ActiveJob.version < Gem::Version.new('7.1') end def finish(_name, _id, payload) @@ -160,7 +160,7 @@ def finish(_name, _id, payload) OpenTelemetry.handle_error(exception: e) end - super + super if ::ActiveJob.version < Gem::Version.new('7.1') ensure begin span&.finish @@ -176,6 +176,9 @@ def finish(_name, _id, payload) def self.install attach_to :active_job + + # ActiveSupport::Notifications.subscribe("render_template.action_view", ActionView::LogSubscriber::Start.new) + # ActiveSupport::Notifications.subscribe("render_layout.action_view", ActionView::LogSubscriber::Start.new) end def self.uninstall diff --git a/instrumentation/active_job/test/instrumentation/active_job/subscriber_test.rb b/instrumentation/active_job/test/instrumentation/active_job/subscriber_test.rb index edbf4d911..3e09c2521 100644 --- a/instrumentation/active_job/test/instrumentation/active_job/subscriber_test.rb +++ b/instrumentation/active_job/test/instrumentation/active_job/subscriber_test.rb @@ -167,13 +167,21 @@ end end - it 'is set correctly for jobs that do wait' do + it 'is set correctly for jobs that do wait in Rails 7.0 or older' do + skip 'scheduled jobs behave differently in Rails 7.1 and newer' if ActiveJob.version < Gem::Version.new('7.1') + job = TestJob.set(wait: 0.second).perform_later - # Only the sending span is a 'scheduled' thing - _(publish_span.attributes['rails.active_job.scheduled_at']).must_equal(job.scheduled_at) + _(publish_span.attributes['rails.active_job.scheduled_at']).must_equal(job.scheduled_at.to_f) + _(process_span.attributes['rails.active_job.scheduled_at']).must_equal(job.scheduled_at.to_f) + end + + it 'is set correctly for jobs that do wait in Rails 7.1 and newer' do + skip 'scheduled jobs behave differently in Rails 7.0 and older' if ActiveJob.version >= Gem::Version.new('7.1') + + job = TestJob.set(wait: 0.second).perform_later - # The processing span isn't a 'scheduled' thing + _(publish_span.attributes['rails.active_job.scheduled_at']).must_equal(job.scheduled_at.to_f) _(process_span.attributes['rails.active_job.scheduled_at']).must_be_nil end end