Skip to content

Commit 8b2a19b

Browse files
committed
fix(rails): support silenced ActiveRecord events
1 parent 98f97b4 commit 8b2a19b

3 files changed

Lines changed: 15 additions & 3 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class ActiveRecordSubscriber < Sentry::Rails::LogSubscriber
3030
# @param event [ActiveSupport::Notifications::Event] The SQL event
3131
def sql(event)
3232
return unless Sentry.initialized?
33+
return if silenced?(event)
3334
return if EXCLUDED_NAMES.include?(event.payload[:name])
3435

3536
sql = event.payload[:sql]
@@ -120,6 +121,10 @@ def add_db_config_attributes(attributes, db_config)
120121
attributes[:server_socket_address] = db_config[:socket] if db_config[:socket]
121122
end
122123

124+
def silenced?(event)
125+
super || ActiveRecord::Base.logger.silencer
126+
end
127+
123128
if ::Rails.version.to_f >= 6.1
124129
def extract_db_config_from_connection(connection)
125130
if connection.pool.respond_to?(:db_config)

sentry-rails/spec/dummy/test_rails_app/config/application.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
require "action_cable/engine"
1010
require "active_storage/engine"
1111

12-
ActiveRecord::Base.logger = Logger.new(nil)
13-
1412
module Sentry
1513
module Rails
1614
module Test
@@ -83,7 +81,6 @@ def self.load_test_schema
8381
# This can be inherited and extended by subclasses
8482
def configure
8583
config.root = Test::Application.root_path
86-
config.logger = ActiveSupport::Logger.new(nil)
8784
config.active_support.deprecation = :silence
8885
config.hosts = nil
8986
config.secret_key_base = "test 123"

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@
4646
expect(log_event[:attributes][:sql][:value]).to include("posts")
4747
end
4848

49+
it "does not log silenced logs" do
50+
ActiveRecord::Base.logger.silence { Post.create! }
51+
52+
Sentry.get_current_client.flush
53+
54+
expect(
55+
sentry_logs.find { |log| log[:body].include?("Database query: Post Create") }
56+
).to be(nil)
57+
end
58+
4959
context "when send_default_pii is enabled" do
5060
before do
5161
Sentry.configuration.send_default_pii = true

0 commit comments

Comments
 (0)