|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "spec_helper" |
| 4 | +require "sentry/rails/log_subscribers/active_record_subscriber" |
| 5 | + |
| 6 | +RSpec.describe Sentry::Rails::LogSubscribers::ActiveRecordSubscriber do |
| 7 | + before do |
| 8 | + make_basic_app do |config| |
| 9 | + config.enable_logs = true |
| 10 | + config.rails.structured_logging.enabled = true |
| 11 | + config.rails.structured_logging.attach_to = [:active_record] |
| 12 | + end |
| 13 | + end |
| 14 | + |
| 15 | + describe "integration with ActiveSupport::Notifications" do |
| 16 | + it "logs SQL events when database queries are executed" do |
| 17 | + sentry_transport.events.clear |
| 18 | + sentry_transport.envelopes.clear |
| 19 | + |
| 20 | + Post.create! |
| 21 | + |
| 22 | + Sentry.get_current_client.log_event_buffer.flush |
| 23 | + |
| 24 | + expect(sentry_logs).not_to be_empty |
| 25 | + |
| 26 | + log_event = sentry_logs.find { |log| log[:body]&.include?("Database query") && log[:attributes][:sql][:value]&.include?("INSERT") } |
| 27 | + expect(log_event).not_to be_nil |
| 28 | + expect(log_event[:level]).to eq("info") |
| 29 | + expect(log_event[:attributes][:sql][:value]).to include("INSERT INTO") |
| 30 | + expect(log_event[:attributes][:duration_ms][:value]).to be > 0 |
| 31 | + end |
| 32 | + |
| 33 | + it "logs SELECT queries with proper attributes" do |
| 34 | + post = Post.create! |
| 35 | + |
| 36 | + Sentry.get_current_client.log_event_buffer.flush |
| 37 | + sentry_transport.events.clear |
| 38 | + sentry_transport.envelopes.clear |
| 39 | + |
| 40 | + Post.find(post.id) |
| 41 | + |
| 42 | + Sentry.get_current_client.log_event_buffer.flush |
| 43 | + |
| 44 | + log_event = sentry_logs.find { |log| log[:body]&.include?("Database query") } |
| 45 | + expect(log_event).not_to be_nil |
| 46 | + expect(log_event[:attributes][:sql][:value]).to include("SELECT") |
| 47 | + expect(log_event[:attributes][:sql][:value]).to include("posts") |
| 48 | + end |
| 49 | + |
| 50 | + it "excludes SCHEMA events" do |
| 51 | + sentry_transport.events.clear |
| 52 | + sentry_transport.envelopes.clear |
| 53 | + |
| 54 | + ActiveSupport::Notifications.instrument("sql.active_record", |
| 55 | + sql: "CREATE TABLE temp_test_table (id INTEGER)", |
| 56 | + name: "SCHEMA", |
| 57 | + connection: ActiveRecord::Base.connection |
| 58 | + ) |
| 59 | + |
| 60 | + Sentry.get_current_client.log_event_buffer.flush |
| 61 | + |
| 62 | + schema_logs = sentry_logs.select { |log| log[:attributes]&.dig(:sql, :value)&.include?("CREATE TABLE") } |
| 63 | + expect(schema_logs).to be_empty |
| 64 | + end |
| 65 | + |
| 66 | + it "excludes events starting with !" do |
| 67 | + subscriber = described_class.new |
| 68 | + event = double("event", name: "!connection.active_record", payload: {}) |
| 69 | + expect(subscriber.send(:excluded_event?, event)).to be true |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + describe "database configuration extraction" do |
| 74 | + it "includes database configuration in log attributes" do |
| 75 | + sentry_transport.events.clear |
| 76 | + sentry_transport.envelopes.clear |
| 77 | + |
| 78 | + Post.create! |
| 79 | + |
| 80 | + Sentry.get_current_client.log_event_buffer.flush |
| 81 | + |
| 82 | + log_event = sentry_logs.find do |log| |
| 83 | + log[:body]&.include?("Database query") && |
| 84 | + log[:attributes]&.dig(:sql, :value)&.include?("INSERT") |
| 85 | + end |
| 86 | + |
| 87 | + expect(log_event).not_to be_nil |
| 88 | + |
| 89 | + attributes = log_event[:attributes] |
| 90 | + expect(attributes[:db_system][:value]).to eq("sqlite3") |
| 91 | + expect(attributes[:db_name][:value]).to eq("db") |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + describe "statement name handling" do |
| 96 | + it "includes statement name in log message when available" do |
| 97 | + post = Post.create! |
| 98 | + sentry_transport.events.clear |
| 99 | + sentry_transport.envelopes.clear |
| 100 | + |
| 101 | + Post.find(post.id) |
| 102 | + |
| 103 | + Sentry.get_current_client.log_event_buffer.flush |
| 104 | + |
| 105 | + log_event = sentry_logs.find do |log| |
| 106 | + log[:body]&.include?("Database query") && |
| 107 | + log[:attributes]&.dig(:sql, :value)&.include?("SELECT") && |
| 108 | + log[:attributes]&.dig(:statement_name, :value)&.include?("Load") |
| 109 | + end |
| 110 | + expect(log_event).not_to be_nil |
| 111 | + expect(log_event[:attributes][:statement_name][:value]).to include("Load") |
| 112 | + end |
| 113 | + |
| 114 | + it "handles queries without specific statement names" do |
| 115 | + sentry_transport.events.clear |
| 116 | + sentry_transport.envelopes.clear |
| 117 | + |
| 118 | + ActiveRecord::Base.connection.execute("SELECT 1") |
| 119 | + |
| 120 | + Sentry.get_current_client.log_event_buffer.flush |
| 121 | + |
| 122 | + log_event = sentry_logs.find do |log| |
| 123 | + log[:body] == "Database query" && |
| 124 | + log[:attributes]&.dig(:sql, :value) == "SELECT 1" |
| 125 | + end |
| 126 | + expect(log_event).not_to be_nil |
| 127 | + expect(log_event[:attributes][:sql][:value]).to include("SELECT 1") |
| 128 | + end |
| 129 | + end |
| 130 | + |
| 131 | + describe "caching information" do |
| 132 | + it "includes cached flag when query is cached" do |
| 133 | + ActiveRecord::Base.cache do |
| 134 | + post = Post.create! |
| 135 | + sentry_transport.events.clear |
| 136 | + sentry_transport.envelopes.clear |
| 137 | + |
| 138 | + Post.find(post.id) |
| 139 | + Post.find(post.id) |
| 140 | + |
| 141 | + Sentry.get_current_client.log_event_buffer.flush |
| 142 | + |
| 143 | + cached_log = sentry_logs.find { |log| log[:attributes]&.dig(:cached, :value) == true } |
| 144 | + expect(cached_log).not_to be_nil |
| 145 | + end |
| 146 | + end |
| 147 | + end |
| 148 | + |
| 149 | + describe "when logging is disabled" do |
| 150 | + before do |
| 151 | + make_basic_app do |config| |
| 152 | + config.enable_logs = false |
| 153 | + config.rails.structured_logging.enabled = true |
| 154 | + config.rails.structured_logging.attach_to = [:active_record] |
| 155 | + end |
| 156 | + end |
| 157 | + |
| 158 | + it "does not log events when logging is disabled" do |
| 159 | + initial_log_count = sentry_logs.count |
| 160 | + |
| 161 | + Post.create! |
| 162 | + |
| 163 | + if Sentry.get_current_client&.log_event_buffer |
| 164 | + Sentry.get_current_client.log_event_buffer.flush |
| 165 | + end |
| 166 | + |
| 167 | + expect(sentry_logs.count).to eq(initial_log_count) |
| 168 | + end |
| 169 | + end |
| 170 | +end |
0 commit comments