diff --git a/sentry-rails/lib/sentry/rails/tracing.rb b/sentry-rails/lib/sentry/rails/tracing.rb index 82509ab8b..6a1aa36ba 100644 --- a/sentry-rails/lib/sentry/rails/tracing.rb +++ b/sentry-rails/lib/sentry/rails/tracing.rb @@ -55,6 +55,17 @@ def instrument(name, payload = {}, &block) super(name, payload, &block) end + + # Rails 8.2 reports Active Record queries through build_handle, bypassing instrument + if ::ActiveSupport::Notifications::Instrumenter.method_defined?(:build_handle) + def build_handle(name, payload) + if Tracing.subscribed_tracing_events.include?(name) + payload[START_TIMESTAMP_NAME] ||= Time.now.utc.to_f if name[0] != "!" && payload.is_a?(Hash) + end + + super + end + end end end @@ -64,6 +75,12 @@ def self.remove_active_support_notifications_patch def instrument(name, payload = {}, &block) super end + + if ::ActiveSupport::Notifications::Instrumenter.method_defined?(:build_handle) + def build_handle(name, payload) + super + end + end end end end diff --git a/sentry-rails/spec/sentry/rails/tracing/active_record_subscriber_spec.rb b/sentry-rails/spec/sentry/rails/tracing/active_record_subscriber_spec.rb index 45b80d321..5ba8f198d 100644 --- a/sentry-rails/spec/sentry/rails/tracing/active_record_subscriber_spec.rb +++ b/sentry-rails/spec/sentry/rails/tracing/active_record_subscriber_spec.rb @@ -151,6 +151,25 @@ def foo expect(data["db.name"]).to include("db.sqlite3") expect(data["db.system"]).to eq("sqlite3") end + + it "records database query events reported through Instrumenter#build_handle", skip: Rails.version.to_f < 7.1 do + transaction = Sentry.start_transaction(sampled: true) + Sentry.get_current_scope.set_span(transaction) + + handle = ActiveSupport::Notifications.instrumenter.build_handle("sql.active_record", sql: "SELECT 1", name: "SQL") + handle.start + handle.finish + + transaction.finish + + transaction = transport.events.first.to_h + expect(transaction[:spans].count).to eq(1) + + span = transaction[:spans][0] + expect(span[:op]).to eq("db.sql.active_record") + expect(span[:description]).to eq("SELECT 1") + expect(span[:start_timestamp]).to be <= span[:timestamp] + end end context "when transaction is not sampled" do