|
12 | 12 | config.rails.structured_logging.subscribers = { active_record: Sentry::Rails::LogSubscribers::ActiveRecordSubscriber } |
13 | 13 | end |
14 | 14 | end |
| 15 | + |
15 | 16 | describe "integration with ActiveSupport::Notifications" do |
16 | 17 | it "logs SQL events when database queries are executed" do |
17 | 18 | Post.create! |
|
45 | 46 | expect(log_event[:attributes][:sql][:value]).to include("posts") |
46 | 47 | end |
47 | 48 |
|
| 49 | + context "when send_default_pii is enabled" do |
| 50 | + before do |
| 51 | + Sentry.configuration.send_default_pii = true |
| 52 | + end |
| 53 | + |
| 54 | + after do |
| 55 | + Sentry.configuration.send_default_pii = false |
| 56 | + end |
| 57 | + |
| 58 | + it "logs SELECT queries with binds in attributes" do |
| 59 | + post = Post.create!(title: "test") |
| 60 | + |
| 61 | + Sentry.get_current_client.flush |
| 62 | + sentry_transport.events.clear |
| 63 | + sentry_transport.envelopes.clear |
| 64 | + |
| 65 | + created_at = Time.new(2025, 10, 28, 13, 11, 44) |
| 66 | + Post.where(id: post.id, title: post.title, created_at: created_at).to_a |
| 67 | + |
| 68 | + Sentry.get_current_client.flush |
| 69 | + |
| 70 | + log_event = sentry_logs.find { |log| log[:body]&.include?("Database query") } |
| 71 | + expect(log_event).not_to be_nil |
| 72 | + |
| 73 | + # Follow Sentry convention: db.query.parameter.<key> with string values |
| 74 | + expect(log_event[:attributes]["db.query.parameter.id"][:value]).to eq(post.id.to_s) |
| 75 | + expect(log_event[:attributes]["db.query.parameter.id"][:type]).to eql("string") |
| 76 | + |
| 77 | + expect(log_event[:attributes]["db.query.parameter.title"][:value]).to eql(post.title) |
| 78 | + expect(log_event[:attributes]["db.query.parameter.title"][:type]).to eql("string") |
| 79 | + |
| 80 | + expect(log_event[:attributes]["db.query.parameter.created_at"][:value]).to include("2025-10-28 13:11:44") |
| 81 | + expect(log_event[:attributes]["db.query.parameter.created_at"][:type]).to eql("string") |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + context "when send_default_pii is disabled" do |
| 86 | + it "logs SELECT queries without binds in attributes" do |
| 87 | + post = Post.create!(title: "test") |
| 88 | + |
| 89 | + Sentry.get_current_client.flush |
| 90 | + sentry_transport.events.clear |
| 91 | + sentry_transport.envelopes.clear |
| 92 | + |
| 93 | + Post.where(id: post.id, title: post.title).to_a |
| 94 | + |
| 95 | + Sentry.get_current_client.flush |
| 96 | + |
| 97 | + log_event = sentry_logs.find { |log| log[:body]&.include?("Database query") } |
| 98 | + expect(log_event).not_to be_nil |
| 99 | + |
| 100 | + expect(log_event[:attributes]["db.query.parameter.id"]).to be_nil |
| 101 | + expect(log_event[:attributes]["db.query.parameter.title"]).to be_nil |
| 102 | + end |
| 103 | + end |
| 104 | + |
48 | 105 | if Rails.version.to_f > 5.1 |
49 | 106 | it "excludes SCHEMA events" do |
50 | 107 | ActiveSupport::Notifications.instrument("sql.active_record", |
|
0 commit comments