|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "sentry/rails/log_subscriber" |
| 4 | +require "sentry/rails/log_subscribers/parameter_filter" |
| 5 | + |
| 6 | +module Sentry |
| 7 | + module Rails |
| 8 | + module LogSubscribers |
| 9 | + # LogSubscriber for ActionController events that captures HTTP request processing |
| 10 | + # and logs them using Sentry's structured logging system. |
| 11 | + # |
| 12 | + # This subscriber captures process_action.action_controller events and formats them |
| 13 | + # with relevant request information including controller, action, HTTP status, |
| 14 | + # request parameters, and performance metrics. |
| 15 | + # |
| 16 | + # @example Usage |
| 17 | + # # Enable structured logging for ActionController |
| 18 | + # Sentry.init do |config| |
| 19 | + # config.enable_logs = true |
| 20 | + # config.rails.structured_logging = true |
| 21 | + # config.rails.structured_logging.attach_to = [:action_controller] |
| 22 | + # end |
| 23 | + class ActionControllerSubscriber < Sentry::Rails::LogSubscriber |
| 24 | + include ParameterFilter |
| 25 | + |
| 26 | + # Handle process_action.action_controller events |
| 27 | + # |
| 28 | + # @param event [ActiveSupport::Notifications::Event] The controller action event |
| 29 | + def process_action(event) |
| 30 | + payload = event.payload |
| 31 | + duration = event.time.round(2) |
| 32 | + |
| 33 | + controller = payload[:controller] |
| 34 | + action = payload[:action] |
| 35 | + |
| 36 | + status = extract_status(payload) |
| 37 | + |
| 38 | + attributes = { |
| 39 | + controller: controller, |
| 40 | + action: action, |
| 41 | + status: status, |
| 42 | + duration_ms: duration, |
| 43 | + method: payload[:method], |
| 44 | + path: payload[:path], |
| 45 | + format: payload[:format] |
| 46 | + } |
| 47 | + |
| 48 | + if payload[:view_runtime] |
| 49 | + attributes[:view_runtime_ms] = payload[:view_runtime].round(2) |
| 50 | + end |
| 51 | + |
| 52 | + if payload[:db_runtime] |
| 53 | + attributes[:db_runtime_ms] = payload[:db_runtime].round(2) |
| 54 | + end |
| 55 | + |
| 56 | + if Sentry.configuration.send_default_pii && payload[:params] |
| 57 | + filtered_params = filter_sensitive_params(payload[:params]) |
| 58 | + attributes[:params] = filtered_params unless filtered_params.empty? |
| 59 | + end |
| 60 | + |
| 61 | + level = level_for_request(payload) |
| 62 | + message = "#{controller}##{action}" |
| 63 | + |
| 64 | + log_structured_event( |
| 65 | + message: message, |
| 66 | + level: level, |
| 67 | + attributes: attributes |
| 68 | + ) |
| 69 | + end |
| 70 | + |
| 71 | + private |
| 72 | + |
| 73 | + def extract_status(payload) |
| 74 | + if payload[:status] |
| 75 | + payload[:status] |
| 76 | + elsif payload[:exception] |
| 77 | + case payload[:exception].first |
| 78 | + when "ActionController::RoutingError" |
| 79 | + 404 |
| 80 | + when "ActionController::BadRequest" |
| 81 | + 400 |
| 82 | + else |
| 83 | + 500 |
| 84 | + end |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + def level_for_request(payload) |
| 89 | + status = payload[:status] |
| 90 | + |
| 91 | + # In Rails < 6.0 status is not set when an action raised an exception |
| 92 | + if status.nil? && payload[:exception] |
| 93 | + case payload[:exception].first |
| 94 | + when "ActionController::RoutingError" |
| 95 | + :warn |
| 96 | + when "ActionController::BadRequest" |
| 97 | + :warn |
| 98 | + else |
| 99 | + :error |
| 100 | + end |
| 101 | + elsif status >= 200 && status < 400 |
| 102 | + :info |
| 103 | + elsif status >= 400 && status < 500 |
| 104 | + :warn |
| 105 | + elsif status >= 500 |
| 106 | + :error |
| 107 | + else |
| 108 | + :info |
| 109 | + end |
| 110 | + end |
| 111 | + end |
| 112 | + end |
| 113 | + end |
| 114 | +end |
0 commit comments