Skip to content

Commit a11b027

Browse files
committed
fix(rails): handle positioned binds in logging
Fixes #2786
1 parent 17d6e49 commit a11b027

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@ def sql(event)
5050

5151
binds = event.payload[:binds]
5252

53-
if Sentry.configuration.send_default_pii && !binds&.empty?
53+
if Sentry.configuration.send_default_pii && binds&.any?
5454
type_casted_binds = type_casted_binds(event)
5555

5656
binds.each_with_index do |bind, index|
57-
name = bind.is_a?(Symbol) ? bind : bind.name
58-
attributes["db.query.parameter.#{name}"] = type_casted_binds[index].to_s
57+
key = bind.respond_to?(:name) ? bind.name : index.to_s
58+
value = type_casted_binds[index].to_s
59+
60+
attributes["db.query.parameter.#{key}"] = value
5961
end
6062
end
6163

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,53 @@
8080
expect(log_event[:attributes]["db.query.parameter.created_at"][:value]).to include("2025-10-28 13:11:44")
8181
expect(log_event[:attributes]["db.query.parameter.created_at"][:type]).to eql("string")
8282
end
83+
84+
it "logs queries with positional (unnamed) binds" do
85+
post = Post.create!(title: "test")
86+
87+
Sentry.get_current_client.flush
88+
sentry_transport.events.clear
89+
sentry_transport.envelopes.clear
90+
91+
Post.where("id = ? AND title = ?", post.id, post.title).first
92+
93+
Sentry.get_current_client.flush
94+
95+
log_event = sentry_logs.find { |log| log[:body]&.include?("Database query") }
96+
expect(log_event).not_to be_nil
97+
98+
expect(log_event[:attributes]["db.query.parameter.0"][:value]).to eq(post.id.to_s)
99+
expect(log_event[:attributes]["db.query.parameter.1"][:value]).to eq(post.title)
100+
end
101+
102+
it "handles nil binds gracefully" do
103+
# In theory, this should never happened
104+
ActiveSupport::Notifications.instrument("sql.active_record",
105+
sql: "SELECT 1",
106+
name: "SQL",
107+
connection: ActiveRecord::Base.connection,
108+
binds: nil
109+
)
110+
111+
Sentry.get_current_client.flush
112+
113+
log_event = sentry_logs.find { |log| log[:attributes]&.dig(:sql, :value) == "SELECT 1" }
114+
expect(log_event).not_to be_nil
115+
expect(log_event[:attributes][:sql][:value]).to eq("SELECT 1")
116+
end
117+
118+
it "when binds are empty array" do
119+
Post.connection.execute("SELECT posts.* FROM posts")
120+
121+
Sentry.get_current_client.flush
122+
123+
log_event = sentry_logs.find { |log|
124+
log[:attributes].dig(:sql, :value).include?("SELECT") &&
125+
log[:attributes].dig(:sql, :value).include?("posts")
126+
}
127+
128+
expect(log_event).not_to be_nil
129+
end
83130
end
84131

85132
context "when send_default_pii is disabled" do

0 commit comments

Comments
 (0)