Skip to content

Instantly share code, notes, and snippets.

@amkisko
Created April 4, 2025 11:03
Show Gist options
  • Save amkisko/7821df967a8b0bb9282d34e7e3092b3d to your computer and use it in GitHub Desktop.
Save amkisko/7821df967a8b0bb9282d34e7e3092b3d to your computer and use it in GitHub Desktop.
Amazing Rails debug with TracePoint and sql.active_record notifications
require "amazing_print"
class Debug
def self.trace(with_sql: false)
if with_sql
subscriber = ActiveSupport::Notifications.subscribe("sql.active_record") do |event|
payload = event.payload[:sql]
next if payload.match?(/^(SELECT|SET|SHOW|BEGIN|COMMIT|ROLLBACK|RELEASE|SAVEPOINT)/)
# next if payload.include?("audits")
event.payload[:type_casted_binds].each_with_index do |bind, index|
payload = payload.gsub("$#{index + 1}", "'#{bind}'")
end
puts "+ #{payload}"
end
end
@raise_traces = []
raise_trace = TracePoint.new(:raise) do |tp|
@raise_traces << tp
ap({
path: tp.path,
lineno: tp.lineno,
event: tp.event,
method_id: tp.method_id,
raised_exception: tp.raised_exception
})
end
raise_trace.enable
yield
raise_trace.disable
ActiveSupport::Notifications.unsubscribe(subscriber) if with_sql
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment