|
| 1 | +### Available Built-in Subscribers |
| 2 | + |
| 3 | +The Rails SDK includes several built-in log subscribers that you can enable: |
| 4 | + |
| 5 | +#### Default Subscribers (enabled automatically) |
| 6 | +- **ActiveRecord**: Database queries with SQL, duration, connection info, and caching status |
| 7 | +- **ActionController**: HTTP requests with controller, action, parameters, response status, and timing |
| 8 | + |
| 9 | +#### Additional Subscribers (opt-in) |
| 10 | +- **ActiveJob**: Background job execution (`perform`), enqueueing (`enqueue`), retry failures (`retry_stopped`), and job discarding (`discard`) |
| 11 | +- **ActionMailer**: Email delivery (`deliver`) and processing (`process`) events |
| 12 | + |
| 13 | +### Custom Subscriber Configuration |
| 14 | + |
| 15 | +You can customize which subscribers are active: |
| 16 | + |
| 17 | +```ruby |
| 18 | +Sentry.init do |config| |
| 19 | + config.dsn = "___PUBLIC_DSN___" |
| 20 | + |
| 21 | + config.enable_logs = true |
| 22 | + |
| 23 | + config.rails.structured_logging.enabled = true |
| 24 | + |
| 25 | + config.rails.structured_logging.subscribers = { |
| 26 | + active_record: Sentry::Rails::LogSubscribers::ActiveRecordSubscriber, |
| 27 | + action_controller: Sentry::Rails::LogSubscribers::ActionControllerSubscriber |
| 28 | + } |
| 29 | +end |
| 30 | +``` |
| 31 | + |
| 32 | +To enable additional subscribers, add them to the configuration: |
| 33 | + |
| 34 | +```ruby |
| 35 | +Sentry.init do |config| |
| 36 | + config.dsn = "___PUBLIC_DSN___" |
| 37 | + |
| 38 | + config.enable_logs = true |
| 39 | + |
| 40 | + config.rails.structured_logging.enabled = true |
| 41 | + |
| 42 | + config.rails.structured_logging.subscribers = { |
| 43 | + active_record: Sentry::Rails::LogSubscribers::ActiveRecordSubscriber, |
| 44 | + action_controller: Sentry::Rails::LogSubscribers::ActionControllerSubscriber, |
| 45 | + active_job: Sentry::Rails::LogSubscribers::ActiveJobSubscriber, |
| 46 | + action_mailer: Sentry::Rails::LogSubscribers::ActionMailerSubscriber |
| 47 | + } |
| 48 | +end |
| 49 | +``` |
| 50 | + |
| 51 | +To disable specific subscribers: |
| 52 | + |
| 53 | +```ruby |
| 54 | +Sentry.init do |config| |
| 55 | + config.dsn = "___PUBLIC_DSN___" |
| 56 | + |
| 57 | + config.enable_logs = true |
| 58 | + |
| 59 | + config.rails.structured_logging.enabled = true |
| 60 | + |
| 61 | + config.rails.structured_logging.subscribers = { |
| 62 | + active_record: Sentry::Rails::LogSubscribers::ActiveRecordSubscriber |
| 63 | + # ActionController subscriber disabled |
| 64 | + } |
| 65 | +end |
| 66 | +``` |
| 67 | + |
| 68 | +### Creating Custom Log Subscribers |
| 69 | + |
| 70 | +You can create custom log subscribers by extending the base class: |
| 71 | + |
| 72 | +```ruby |
| 73 | +class MyCustomSubscriber < Sentry::Rails::LogSubscriber |
| 74 | + # Attach to your component's instrumentation events |
| 75 | + attach_to :my_component |
| 76 | + |
| 77 | + def my_event(event) |
| 78 | + log_structured_event( |
| 79 | + message: "Custom event occurred", |
| 80 | + level: :info, |
| 81 | + attributes: { |
| 82 | + duration_ms: event.duration, |
| 83 | + custom_data: event.payload[:custom_data], |
| 84 | + user_id: event.payload[:user_id] |
| 85 | + } |
| 86 | + ) |
| 87 | + end |
| 88 | + |
| 89 | + def another_event(event) |
| 90 | + log_structured_event( |
| 91 | + message: "Another custom event", |
| 92 | + level: :warn, |
| 93 | + attributes: { |
| 94 | + event_type: event.payload[:type], |
| 95 | + metadata: event.payload[:metadata] |
| 96 | + } |
| 97 | + ) |
| 98 | + end |
| 99 | +end |
| 100 | +``` |
| 101 | + |
| 102 | +Then register your custom subscriber: |
| 103 | + |
| 104 | +```ruby |
| 105 | +Sentry.init do |config| |
| 106 | + config.dsn = "___PUBLIC_DSN___" |
| 107 | + |
| 108 | + config.enable_logs = true |
| 109 | + |
| 110 | + config.rails.structured_logging.enabled = true |
| 111 | + |
| 112 | + config.rails.structured_logging.subscribers = { |
| 113 | + active_record: Sentry::Rails::LogSubscribers::ActiveRecordSubscriber, |
| 114 | + action_controller: Sentry::Rails::LogSubscribers::ActionControllerSubscriber, |
| 115 | + my_component: MyCustomSubscriber |
| 116 | + } |
| 117 | +end |
| 118 | +``` |
| 119 | + |
| 120 | +### Sensitive Data Filtering |
| 121 | + |
| 122 | +Log subscribers automatically respect Rails' parameter filtering configuration. Sensitive parameters defined in `config.filter_parameters` will be filtered from structured logs: |
| 123 | + |
| 124 | +```ruby |
| 125 | +# config/application.rb |
| 126 | +config.filter_parameters += [:password, :credit_card, :ssn] |
| 127 | +``` |
0 commit comments