Skip to content
Open
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
17 changes: 17 additions & 0 deletions sentry-rails/lib/sentry/rails/tracing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down