Skip to content

Commit 7c22df6

Browse files
committed
feat(rails): add binds to ActiveRecord logs
1 parent f951c95 commit 7c22df6

7 files changed

Lines changed: 75 additions & 0 deletions

File tree

sentry-rails/lib/sentry/rails/log_subscribers/active_record_subscriber.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ def sql(event)
4646
cached: cached
4747
}
4848

49+
if Sentry.configuration.send_default_pii && !(binds = event.payload[:binds]).empty?
50+
type_casted_binds = event.payload[:type_casted_binds]
51+
52+
binds.each_with_index do |bind, index|
53+
name = bind.is_a?(Symbol) ? bind : bind.name.to_sym
54+
attributes[name] = type_casted_binds[index]
55+
end
56+
end
57+
4958
attributes[:statement_name] = statement_name if statement_name && statement_name != "SQL"
5059
attributes[:connection_id] = connection_id if connection_id
5160

sentry-rails/spec/dummy/test_rails_app/apps/5-2.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
end
3131

3232
create_table :posts, force: true do |t|
33+
t.string :title
34+
t.timestamps
3335
end
3436

3537
create_table :comments, force: true do |t|

sentry-rails/spec/dummy/test_rails_app/apps/6-0.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
end
3131

3232
create_table :posts, force: true do |t|
33+
t.string :title
34+
t.timestamps
3335
end
3436

3537
create_table :comments, force: true do |t|

sentry-rails/spec/dummy/test_rails_app/apps/6-1.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
end
3131

3232
create_table :posts, force: true do |t|
33+
t.string :title
34+
t.timestamps
3335
end
3436

3537
create_table :comments, force: true do |t|

sentry-rails/spec/dummy/test_rails_app/apps/7-0.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
end
3131

3232
create_table :posts, force: true do |t|
33+
t.string :title
34+
t.timestamps
3335
end
3436

3537
create_table :comments, force: true do |t|

sentry-rails/spec/dummy/test_rails_app/apps/7-1.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
end
3131

3232
create_table :posts, force: true do |t|
33+
t.string :title
34+
t.timestamps
3335
end
3436

3537
create_table :comments, force: true do |t|

sentry-rails/spec/sentry/rails/log_subscribers/active_record_subscriber_spec.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
config.rails.structured_logging.subscribers = { active_record: Sentry::Rails::LogSubscribers::ActiveRecordSubscriber }
1313
end
1414
end
15+
1516
describe "integration with ActiveSupport::Notifications" do
1617
it "logs SQL events when database queries are executed" do
1718
Post.create!
@@ -45,6 +46,61 @@
4546
expect(log_event[:attributes][:sql][:value]).to include("posts")
4647
end
4748

49+
context "when send_default_pii is enabled" do
50+
before do
51+
Sentry.configuration.send_default_pii = true
52+
end
53+
54+
after do
55+
Sentry.configuration.send_default_pii = false
56+
end
57+
58+
it "logs SELECT queries with binds in attributes" do
59+
post = Post.create!(title: "test")
60+
61+
Sentry.get_current_client.flush
62+
sentry_transport.events.clear
63+
sentry_transport.envelopes.clear
64+
65+
created_at = Time.new(2025, 10, 28, 13, 11, 44)
66+
Post.where(id: post.id, title: post.title, created_at: created_at).to_a
67+
68+
Sentry.get_current_client.flush
69+
70+
log_event = sentry_logs.find { |log| log[:body]&.include?("Database query") }
71+
expect(log_event).not_to be_nil
72+
73+
expect(log_event[:attributes][:id][:value]).to be(post.id)
74+
expect(log_event[:attributes][:id][:type]).to eql("integer")
75+
76+
expect(log_event[:attributes][:title][:value]).to eql(post.title)
77+
expect(log_event[:attributes][:title][:type]).to eql("string")
78+
79+
expect(log_event[:attributes][:created_at][:value]).to include("2025-10-28 13:11:44")
80+
expect(log_event[:attributes][:created_at][:type]).to eql("string")
81+
end
82+
end
83+
84+
context "when send_default_pii is disabled" do
85+
it "logs SELECT queries without binds in attributes" do
86+
post = Post.create!(title: "test")
87+
88+
Sentry.get_current_client.flush
89+
sentry_transport.events.clear
90+
sentry_transport.envelopes.clear
91+
92+
Post.where(id: post.id, title: post.title).to_a
93+
94+
Sentry.get_current_client.flush
95+
96+
log_event = sentry_logs.find { |log| log[:body]&.include?("Database query") }
97+
expect(log_event).not_to be_nil
98+
99+
expect(log_event[:attributes][:id]).to be_nil
100+
expect(log_event[:attributes][:title]).to be_nil
101+
end
102+
end
103+
48104
if Rails.version.to_f > 5.1
49105
it "excludes SCHEMA events" do
50106
ActiveSupport::Notifications.instrument("sql.active_record",

0 commit comments

Comments
 (0)