Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Unreleased

### Features

- Add support for ActiveRecord binds in the log events ([#2761](https://github.com/getsentry/sentry-ruby/pull/2761))

## 6.0.0

### Breaking Changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ def sql(event)
cached: cached
}

binds = event.payload[:binds]

if Sentry.configuration.send_default_pii && !binds&.empty?
type_casted_binds = event.payload[:type_casted_binds]

binds.each_with_index do |bind, index|
name = bind.is_a?(Symbol) ? bind : bind.name
attributes["db.query.parameter.#{name}"] = type_casted_binds[index].to_s
end
end

attributes[:statement_name] = statement_name if statement_name && statement_name != "SQL"
attributes[:connection_id] = connection_id if connection_id

Expand Down
2 changes: 2 additions & 0 deletions sentry-rails/spec/dummy/test_rails_app/apps/5-2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
end

create_table :posts, force: true do |t|
t.string :title
t.timestamps
end

create_table :comments, force: true do |t|
Expand Down
2 changes: 2 additions & 0 deletions sentry-rails/spec/dummy/test_rails_app/apps/6-0.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
end

create_table :posts, force: true do |t|
t.string :title
t.timestamps
end

create_table :comments, force: true do |t|
Expand Down
2 changes: 2 additions & 0 deletions sentry-rails/spec/dummy/test_rails_app/apps/6-1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
end

create_table :posts, force: true do |t|
t.string :title
t.timestamps
end

create_table :comments, force: true do |t|
Expand Down
2 changes: 2 additions & 0 deletions sentry-rails/spec/dummy/test_rails_app/apps/7-0.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
end

create_table :posts, force: true do |t|
t.string :title
t.timestamps
end

create_table :comments, force: true do |t|
Expand Down
2 changes: 2 additions & 0 deletions sentry-rails/spec/dummy/test_rails_app/apps/7-1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
end

create_table :posts, force: true do |t|
t.string :title
t.timestamps
end

create_table :comments, force: true do |t|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
config.rails.structured_logging.subscribers = { active_record: Sentry::Rails::LogSubscribers::ActiveRecordSubscriber }
end
end

describe "integration with ActiveSupport::Notifications" do
it "logs SQL events when database queries are executed" do
Post.create!
Expand Down Expand Up @@ -45,6 +46,62 @@
expect(log_event[:attributes][:sql][:value]).to include("posts")
end

context "when send_default_pii is enabled" do
before do
Sentry.configuration.send_default_pii = true
end

after do
Sentry.configuration.send_default_pii = false
end

it "logs SELECT queries with binds in attributes" do
post = Post.create!(title: "test")

Sentry.get_current_client.flush
sentry_transport.events.clear
sentry_transport.envelopes.clear

created_at = Time.new(2025, 10, 28, 13, 11, 44)
Post.where(id: post.id, title: post.title, created_at: created_at).to_a

Sentry.get_current_client.flush

log_event = sentry_logs.find { |log| log[:body]&.include?("Database query") }
expect(log_event).not_to be_nil

# Follow Sentry convention: db.query.parameter.<key> with string values
expect(log_event[:attributes]["db.query.parameter.id"][:value]).to eq(post.id.to_s)
expect(log_event[:attributes]["db.query.parameter.id"][:type]).to eql("string")

expect(log_event[:attributes]["db.query.parameter.title"][:value]).to eql(post.title)
expect(log_event[:attributes]["db.query.parameter.title"][:type]).to eql("string")

expect(log_event[:attributes]["db.query.parameter.created_at"][:value]).to include("2025-10-28 13:11:44")
expect(log_event[:attributes]["db.query.parameter.created_at"][:type]).to eql("string")
end
end

context "when send_default_pii is disabled" do
it "logs SELECT queries without binds in attributes" do
post = Post.create!(title: "test")

Sentry.get_current_client.flush
sentry_transport.events.clear
sentry_transport.envelopes.clear

Post.where(id: post.id, title: post.title).to_a

Sentry.get_current_client.flush

log_event = sentry_logs.find { |log| log[:body]&.include?("Database query") }
expect(log_event).not_to be_nil

expect(log_event[:attributes]["db.query.parameter.id"]).to be_nil
expect(log_event[:attributes]["db.query.parameter.title"]).to be_nil
end
end

if Rails.version.to_f > 5.1
it "excludes SCHEMA events" do
ActiveSupport::Notifications.instrument("sql.active_record",
Expand Down
Loading