|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "spec_helper" |
| 4 | +require "sentry/rails/log_subscribers/action_controller_subscriber" |
| 5 | + |
| 6 | +RSpec.describe Sentry::Rails::LogSubscribers::ActionControllerSubscriber, type: :request 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 = [:action_controller] |
| 12 | + end |
| 13 | + end |
| 14 | + |
| 15 | + describe "integration with ActiveSupport::Notifications" do |
| 16 | + it "logs controller action events when requests are processed" do |
| 17 | + sentry_transport.events.clear |
| 18 | + sentry_transport.envelopes.clear |
| 19 | + |
| 20 | + get "/world" |
| 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] == "HelloController#world" } |
| 27 | + expect(log_event).not_to be_nil |
| 28 | + expect(log_event[:level]).to eq("info") |
| 29 | + expect(log_event[:attributes][:controller][:value]).to eq("HelloController") |
| 30 | + expect(log_event[:attributes][:action][:value]).to eq("world") |
| 31 | + expect(log_event[:attributes][:status][:value]).to eq(200) |
| 32 | + expect(log_event[:attributes][:duration_ms][:value]).to be > 0 |
| 33 | + expect(log_event[:attributes][:method][:value]).to eq("GET") |
| 34 | + expect(log_event[:attributes][:path][:value]).to eq("/world") |
| 35 | + expect(log_event[:attributes][:format][:value]).to eq(:html) |
| 36 | + end |
| 37 | + |
| 38 | + it "logs different status codes appropriately" do |
| 39 | + sentry_transport.events.clear |
| 40 | + sentry_transport.envelopes.clear |
| 41 | + |
| 42 | + get "/not_found" |
| 43 | + |
| 44 | + Sentry.get_current_client.log_event_buffer.flush |
| 45 | + |
| 46 | + expect(sentry_logs).not_to be_empty |
| 47 | + |
| 48 | + log_event = sentry_logs.find { |log| log[:body] == "HelloController#not_found" } |
| 49 | + expect(log_event).not_to be_nil |
| 50 | + expect(log_event[:level]).to eq("warn") |
| 51 | + expect(log_event[:attributes][:status][:value]).to eq(400) |
| 52 | + end |
| 53 | + |
| 54 | + it "logs error status codes with error level" do |
| 55 | + sentry_transport.events.clear |
| 56 | + sentry_transport.envelopes.clear |
| 57 | + |
| 58 | + get "/exception" |
| 59 | + |
| 60 | + Sentry.get_current_client.log_event_buffer.flush |
| 61 | + |
| 62 | + expect(sentry_logs).not_to be_empty |
| 63 | + |
| 64 | + log_event = sentry_logs.find { |log| log[:body] == "HelloController#exception" } |
| 65 | + expect(log_event).not_to be_nil |
| 66 | + expect(log_event[:level]).to eq("error") |
| 67 | + expect(log_event[:attributes][:status][:value]).to eq(500) |
| 68 | + end |
| 69 | + |
| 70 | + it "includes view runtime when available" do |
| 71 | + sentry_transport.events.clear |
| 72 | + sentry_transport.envelopes.clear |
| 73 | + |
| 74 | + get "/view" |
| 75 | + |
| 76 | + Sentry.get_current_client.log_event_buffer.flush |
| 77 | + |
| 78 | + expect(sentry_logs).not_to be_empty |
| 79 | + |
| 80 | + log_event = sentry_logs.find { |log| log[:body] == "HelloController#view" } |
| 81 | + expect(log_event).not_to be_nil |
| 82 | + expect(log_event[:attributes][:view_runtime_ms]).to be_present |
| 83 | + expect(log_event[:attributes][:view_runtime_ms][:value]).to be >= 0 |
| 84 | + end |
| 85 | + |
| 86 | + it "includes database runtime when available" do |
| 87 | + sentry_transport.events.clear |
| 88 | + sentry_transport.envelopes.clear |
| 89 | + |
| 90 | + Post.create! |
| 91 | + get "/posts" |
| 92 | + |
| 93 | + Sentry.get_current_client.log_event_buffer.flush |
| 94 | + |
| 95 | + expect(sentry_logs).not_to be_empty |
| 96 | + |
| 97 | + log_event = sentry_logs.find { |log| log[:body] == "PostsController#index" } |
| 98 | + expect(log_event).not_to be_nil |
| 99 | + expect(log_event[:attributes][:db_runtime_ms]).to be_present |
| 100 | + expect(log_event[:attributes][:db_runtime_ms][:value]).to be >= 0 |
| 101 | + end |
| 102 | + |
| 103 | + context "when send_default_pii is enabled" do |
| 104 | + before do |
| 105 | + Sentry.configuration.send_default_pii = true |
| 106 | + end |
| 107 | + |
| 108 | + after do |
| 109 | + Sentry.configuration.send_default_pii = false |
| 110 | + end |
| 111 | + |
| 112 | + it "includes filtered request parameters" do |
| 113 | + sentry_transport.events.clear |
| 114 | + sentry_transport.envelopes.clear |
| 115 | + |
| 116 | + get "/world", params: { safe_param: "value", password: "secret" } |
| 117 | + |
| 118 | + Sentry.get_current_client.log_event_buffer.flush |
| 119 | + |
| 120 | + expect(sentry_logs).not_to be_empty |
| 121 | + |
| 122 | + log_event = sentry_logs.find { |log| log[:body] == "HelloController#world" } |
| 123 | + expect(log_event).not_to be_nil |
| 124 | + expect(log_event[:attributes][:params]).to be_present |
| 125 | + expect(log_event[:attributes][:params][:value]).to include("safe_param" => "value") |
| 126 | + expect(log_event[:attributes][:params][:value]).not_to include("password") |
| 127 | + end |
| 128 | + |
| 129 | + it "filters sensitive parameter names" do |
| 130 | + sentry_transport.events.clear |
| 131 | + sentry_transport.envelopes.clear |
| 132 | + |
| 133 | + get "/world", params: { |
| 134 | + normal_param: "value", |
| 135 | + password: "secret", |
| 136 | + api_key: "key123", |
| 137 | + credit_card: "1234567890", |
| 138 | + authorization: "Bearer token" |
| 139 | + } |
| 140 | + |
| 141 | + Sentry.get_current_client.log_event_buffer.flush |
| 142 | + |
| 143 | + expect(sentry_logs).not_to be_empty |
| 144 | + |
| 145 | + log_event = sentry_logs.find { |log| log[:body] == "HelloController#world" } |
| 146 | + expect(log_event).not_to be_nil |
| 147 | + |
| 148 | + params = log_event[:attributes][:params][:value] |
| 149 | + expect(params).to include("normal_param" => "value") |
| 150 | + expect(params).not_to have_key("password") |
| 151 | + expect(params).not_to have_key("api_key") |
| 152 | + expect(params).not_to have_key("credit_card") |
| 153 | + expect(params).not_to have_key("authorization") |
| 154 | + end |
| 155 | + end |
| 156 | + |
| 157 | + context "when send_default_pii is disabled" do |
| 158 | + it "does not include request parameters" do |
| 159 | + sentry_transport.events.clear |
| 160 | + sentry_transport.envelopes.clear |
| 161 | + |
| 162 | + get "/world", params: { param: "value" } |
| 163 | + |
| 164 | + Sentry.get_current_client.log_event_buffer.flush |
| 165 | + |
| 166 | + expect(sentry_logs).not_to be_empty |
| 167 | + |
| 168 | + log_event = sentry_logs.find { |log| log[:body] == "HelloController#world" } |
| 169 | + expect(log_event).not_to be_nil |
| 170 | + expect(log_event[:attributes]).not_to have_key(:params) |
| 171 | + end |
| 172 | + end |
| 173 | + end |
| 174 | + |
| 175 | + describe "when logging is disabled" do |
| 176 | + before do |
| 177 | + make_basic_app do |config| |
| 178 | + config.enable_logs = false |
| 179 | + config.rails.structured_logging.enabled = true |
| 180 | + config.rails.structured_logging.attach_to = [:action_controller] |
| 181 | + end |
| 182 | + end |
| 183 | + |
| 184 | + it "does not log events when logging is disabled" do |
| 185 | + initial_log_count = sentry_logs.count |
| 186 | + |
| 187 | + get "/world" |
| 188 | + |
| 189 | + if Sentry.get_current_client&.log_event_buffer |
| 190 | + Sentry.get_current_client.log_event_buffer.flush |
| 191 | + end |
| 192 | + |
| 193 | + expect(sentry_logs.count).to eq(initial_log_count) |
| 194 | + end |
| 195 | + end |
| 196 | +end |
0 commit comments