Skip to content

Commit b1ff9b5

Browse files
committed
fix(rails): fix logging of cached queries with binds
1 parent a5cee1b commit b1ff9b5

File tree

2 files changed

+42
-24
lines changed

2 files changed

+42
-24
lines changed

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def sql(event)
5050

5151
binds = event.payload[:binds]
5252

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

5656
binds.each_with_index do |bind, index|
@@ -75,13 +75,15 @@ def sql(event)
7575
)
7676
end
7777

78-
if RUBY_ENGINE == "jruby"
79-
def type_casted_binds(event)
80-
event.payload[:type_casted_binds].call
81-
end
82-
else
83-
def type_casted_binds(event)
84-
event.payload[:type_casted_binds]
78+
def type_casted_binds(event)
79+
binds = event.payload[:type_casted_binds]
80+
81+
# When a query is cached, binds are a callable,
82+
# and under JRuby they're always a callable.
83+
if binds.respond_to?(:call)
84+
binds.call
85+
else
86+
binds
8587
end
8688
end
8789

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

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,38 @@
146146

147147
expect(log_event).not_to be_nil
148148
end
149+
150+
it "includes cached flag when query is cached", skip: Rails.version.to_f < 5.1 ? "Rails 5.0.0 doesn't include cached flag in sql.active_record events" : false do
151+
post = Post.create!(title: "test")
152+
153+
Sentry.get_current_client.flush
154+
sentry_transport.events.clear
155+
sentry_transport.envelopes.clear
156+
157+
ActiveRecord::Base.cache do
158+
Post.where(id: post.id).first
159+
Post.where(id: post.id).first
160+
161+
Post.where(title: post.title).first
162+
Post.where(title: post.title).first
163+
end
164+
165+
Sentry.get_current_client.flush
166+
167+
cached_logs = sentry_logs.select { |log| log[:attributes].dig(:cached, :value) == true }
168+
169+
expect(cached_logs.size).to be(2)
170+
171+
id_query, title_query = cached_logs
172+
173+
expect(id_query[:attributes]["db.query.parameter.id"]).to eq(
174+
type: "string", value: post.id.to_s
175+
)
176+
177+
expect(title_query[:attributes]["db.query.parameter.title"]).to eq(
178+
type: "string", value: "test"
179+
)
180+
end
149181
end
150182

151183
context "when send_default_pii is disabled" do
@@ -277,22 +309,6 @@
277309
expect(log_event[:attributes][:sql][:value]).to include("SELECT 1")
278310
end
279311
end
280-
281-
describe "caching information" do
282-
it "includes cached flag when query is cached", skip: Rails.version.to_f < 5.1 ? "Rails 5.0.0 doesn't include cached flag in sql.active_record events" : false do
283-
ActiveRecord::Base.cache do
284-
post = Post.create!
285-
286-
Post.find(post.id)
287-
Post.find(post.id)
288-
289-
Sentry.get_current_client.flush
290-
291-
cached_log = sentry_logs.find { |log| log[:attributes]&.dig(:cached, :value) == true }
292-
expect(cached_log).not_to be_nil
293-
end
294-
end
295-
end
296312
end
297313

298314
context "when logger is silenced" do

0 commit comments

Comments
 (0)