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

### Fixes

- Handle positioned binds in logging ([#2787](https://github.com/getsentry/sentry-ruby/pull/2787))

## 6.1.1

### Improvements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ def sql(event)

binds = event.payload[:binds]

if Sentry.configuration.send_default_pii && !binds&.empty?
if Sentry.configuration.send_default_pii && binds&.any?
type_casted_binds = type_casted_binds(event)

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
key = bind.respond_to?(:name) ? bind.name : index.to_s
value = type_casted_binds[index].to_s

attributes["db.query.parameter.#{key}"] = value
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,72 @@
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

it "logs queries with positional (unnamed) binds", skip: RAILS_VERSION >= 8.0 do
Sentry.get_current_client.flush
sentry_transport.events.clear
sentry_transport.envelopes.clear

Post.where("id = ? AND title = ?", 1, "Hello World").first

Sentry.get_current_client.flush

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

it "logs queries with positional (unnamed) binds", skip: RAILS_VERSION < 8.0 do
Sentry.get_current_client.flush
sentry_transport.events.clear
sentry_transport.envelopes.clear

Post.where("id = ? AND title = ?", 1, "Hello World").first

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.0"][:value]).to eq("1")
expect(log_event[:attributes]["db.query.parameter.1"][:value]).to eq("Hello World")
end

it "handles nil binds gracefully" do
Sentry.get_current_client.flush
sentry_transport.events.clear
sentry_transport.envelopes.clear

# In theory, this should never happened
ActiveSupport::Notifications.instrument("sql.active_record",
sql: "SELECT 1",
name: "SQL",
connection: ActiveRecord::Base.connection,
binds: nil
)

Sentry.get_current_client.flush

log_event = sentry_logs.find { |log| log[:attributes]&.dig(:sql, :value) == "SELECT 1" }
expect(log_event).not_to be_nil
expect(log_event[:attributes][:sql][:value]).to eq("SELECT 1")
end

it "when binds are empty array" do
Sentry.get_current_client.flush
sentry_transport.events.clear
sentry_transport.envelopes.clear

Post.connection.execute("SELECT posts.* FROM posts")

Sentry.get_current_client.flush

log_event = sentry_logs.find { |log|
log[:attributes].dig(:sql, :value).include?("SELECT") &&
log[:attributes].dig(:sql, :value).include?("posts")
}

expect(log_event).not_to be_nil
end
end

context "when send_default_pii is disabled" do
Expand Down
Loading