sentry-rails 5.26.0 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,116 @@
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.subscribers = { action_controller: Sentry::Rails::LogSubscribers::ActionControllerSubscriber }
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
+
32
+ controller = payload[:controller]
33
+ action = payload[:action]
34
+
35
+ status = extract_status(payload)
36
+
37
+ attributes = {
38
+ controller: controller,
39
+ action: action,
40
+ duration_ms: duration_ms(event),
41
+ method: payload[:method],
42
+ path: payload[:path],
43
+ format: payload[:format]
44
+ }
45
+
46
+ attributes[:status] = status if status
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.nil?
102
+ :info
103
+ elsif status >= 200 && status < 400
104
+ :info
105
+ elsif status >= 400 && status < 500
106
+ :warn
107
+ elsif status >= 500
108
+ :error
109
+ else
110
+ :info
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,88 @@
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 ActionMailer events that captures email delivery
10
+ # and processing events using Sentry's structured logging system.
11
+ #
12
+ # This subscriber captures deliver.action_mailer and process.action_mailer events
13
+ # and formats them with relevant email information while respecting PII settings.
14
+ #
15
+ # @example Usage
16
+ # # Enable structured logging for ActionMailer
17
+ # Sentry.init do |config|
18
+ # config.enable_logs = true
19
+ # config.rails.structured_logging = true
20
+ # config.rails.structured_logging.subscribers = { action_mailer: Sentry::Rails::LogSubscribers::ActionMailerSubscriber }
21
+ # end
22
+ class ActionMailerSubscriber < Sentry::Rails::LogSubscriber
23
+ include ParameterFilter
24
+
25
+ # Handle deliver.action_mailer events
26
+ #
27
+ # @param event [ActiveSupport::Notifications::Event] The email delivery event
28
+ def deliver(event)
29
+ payload = event.payload
30
+
31
+ mailer = payload[:mailer]
32
+
33
+ attributes = {
34
+ mailer: mailer,
35
+ duration_ms: duration_ms(event),
36
+ perform_deliveries: payload[:perform_deliveries]
37
+ }
38
+
39
+ attributes[:delivery_method] = payload[:delivery_method] if payload[:delivery_method]
40
+ attributes[:date] = payload[:date].to_s if payload[:date]
41
+
42
+ if Sentry.configuration.send_default_pii
43
+ attributes[:message_id] = payload[:message_id] if payload[:message_id]
44
+ end
45
+
46
+ message = "Email delivered via #{mailer}"
47
+
48
+ # Log the structured event
49
+ log_structured_event(
50
+ message: message,
51
+ level: :info,
52
+ attributes: attributes
53
+ )
54
+ end
55
+
56
+ # Handle process.action_mailer events
57
+ #
58
+ # @param event [ActiveSupport::Notifications::Event] The email processing event
59
+ def process(event)
60
+ payload = event.payload
61
+
62
+ mailer = payload[:mailer]
63
+ action = payload[:action]
64
+ duration = duration_ms(event)
65
+
66
+ attributes = {
67
+ mailer: mailer,
68
+ action: action,
69
+ duration_ms: duration
70
+ }
71
+
72
+ if Sentry.configuration.send_default_pii && payload[:params]
73
+ filtered_params = filter_sensitive_params(payload[:params])
74
+ attributes[:params] = filtered_params unless filtered_params.empty?
75
+ end
76
+
77
+ message = "#{mailer}##{action}"
78
+
79
+ log_structured_event(
80
+ message: message,
81
+ level: :info,
82
+ attributes: attributes
83
+ )
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,155 @@
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 ActiveJob events that captures background job execution
10
+ # and logs them using Sentry's structured logging system.
11
+ #
12
+ # This subscriber captures various ActiveJob events including job execution,
13
+ # enqueueing, retries, and failures with relevant job information.
14
+ #
15
+ # @example Usage
16
+ # # Enable structured logging for ActiveJob
17
+ # Sentry.init do |config|
18
+ # config.enable_logs = true
19
+ # config.rails.structured_logging = true
20
+ # config.rails.structured_logging.subscribers = { active_job: Sentry::Rails::LogSubscribers::ActiveJobSubscriber }
21
+ # end
22
+ class ActiveJobSubscriber < Sentry::Rails::LogSubscriber
23
+ include ParameterFilter
24
+
25
+ # Handle perform.active_job events
26
+ #
27
+ # @param event [ActiveSupport::Notifications::Event] The job performance event
28
+ def perform(event)
29
+ job = event.payload[:job]
30
+ duration = duration_ms(event)
31
+
32
+ attributes = {
33
+ job_class: job.class.name,
34
+ job_id: job.job_id,
35
+ queue_name: job.queue_name,
36
+ duration_ms: duration,
37
+ executions: job.executions,
38
+ priority: job.priority
39
+ }
40
+
41
+ attributes[:adapter] = job.class.queue_adapter.class.name
42
+
43
+ if job.scheduled_at
44
+ attributes[:scheduled_at] = job.scheduled_at.iso8601
45
+ attributes[:delay_ms] = ((Time.current - job.scheduled_at) * 1000).round(2)
46
+ end
47
+
48
+ if Sentry.configuration.send_default_pii && job.arguments.present?
49
+ filtered_args = filter_sensitive_arguments(job.arguments)
50
+ attributes[:arguments] = filtered_args unless filtered_args.empty?
51
+ end
52
+
53
+ message = "Job performed: #{job.class.name}"
54
+
55
+ log_structured_event(
56
+ message: message,
57
+ level: :info,
58
+ attributes: attributes
59
+ )
60
+ end
61
+
62
+ # Handle enqueue.active_job events
63
+ #
64
+ # @param event [ActiveSupport::Notifications::Event] The job enqueue event
65
+ def enqueue(event)
66
+ job = event.payload[:job]
67
+
68
+ attributes = {
69
+ job_class: job.class.name,
70
+ job_id: job.job_id,
71
+ queue_name: job.queue_name,
72
+ priority: job.priority
73
+ }
74
+
75
+ attributes[:adapter] = job.class.queue_adapter.class.name if job.class.respond_to?(:queue_adapter)
76
+
77
+ if job.scheduled_at
78
+ attributes[:scheduled_at] = job.scheduled_at.iso8601
79
+ attributes[:delay_seconds] = (job.scheduled_at - Time.current).round(2)
80
+ end
81
+
82
+ message = "Job enqueued: #{job.class.name}"
83
+
84
+ log_structured_event(
85
+ message: message,
86
+ level: :info,
87
+ attributes: attributes
88
+ )
89
+ end
90
+
91
+ def retry_stopped(event)
92
+ job = event.payload[:job]
93
+ error = event.payload[:error]
94
+
95
+ attributes = {
96
+ job_class: job.class.name,
97
+ job_id: job.job_id,
98
+ queue_name: job.queue_name,
99
+ executions: job.executions,
100
+ error_class: error.class.name,
101
+ error_message: error.message
102
+ }
103
+
104
+ message = "Job retry stopped: #{job.class.name}"
105
+
106
+ log_structured_event(
107
+ message: message,
108
+ level: :error,
109
+ attributes: attributes
110
+ )
111
+ end
112
+
113
+ def discard(event)
114
+ job = event.payload[:job]
115
+ error = event.payload[:error]
116
+
117
+ attributes = {
118
+ job_class: job.class.name,
119
+ job_id: job.job_id,
120
+ queue_name: job.queue_name,
121
+ executions: job.executions
122
+ }
123
+
124
+ attributes[:error_class] = error.class.name if error
125
+ attributes[:error_message] = error.message if error
126
+
127
+ message = "Job discarded: #{job.class.name}"
128
+
129
+ log_structured_event(
130
+ message: message,
131
+ level: :warn,
132
+ attributes: attributes
133
+ )
134
+ end
135
+
136
+ private
137
+
138
+ def filter_sensitive_arguments(arguments)
139
+ return [] unless arguments.is_a?(Array)
140
+
141
+ arguments.map do |arg|
142
+ case arg
143
+ when Hash
144
+ filter_sensitive_params(arg)
145
+ when String
146
+ arg.length > 100 ? "[FILTERED: #{arg.length} chars]" : arg
147
+ else
148
+ arg
149
+ end
150
+ end
151
+ end
152
+ end
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,134 @@
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 ActiveRecord events that captures database queries
10
+ # and logs them using Sentry's structured logging system.
11
+ #
12
+ # This subscriber captures sql.active_record events and formats them
13
+ # with relevant database information including SQL queries, duration,
14
+ # database configuration, and caching information.
15
+ #
16
+ # @example Usage
17
+ # # Automatically attached when structured logging is enabled for :active_record
18
+ # Sentry.init do |config|
19
+ # config.enable_logs = true
20
+ # config.rails.structured_logging = true
21
+ # config.rails.structured_logging.subscribers = { active_record: Sentry::Rails::LogSubscribers::ActiveRecordSubscriber }
22
+ # end
23
+ class ActiveRecordSubscriber < Sentry::Rails::LogSubscriber
24
+ include ParameterFilter
25
+
26
+ EXCLUDED_NAMES = ["SCHEMA", "TRANSACTION"].freeze
27
+
28
+ # Handle sql.active_record events
29
+ #
30
+ # @param event [ActiveSupport::Notifications::Event] The SQL event
31
+ def sql(event)
32
+ return if EXCLUDED_NAMES.include?(event.payload[:name])
33
+
34
+ sql = event.payload[:sql]
35
+ statement_name = event.payload[:name]
36
+
37
+ # Rails 5.0.0 doesn't include :cached in the payload, it was added in Rails 5.1
38
+ cached = event.payload.fetch(:cached, false)
39
+ connection_id = event.payload[:connection_id]
40
+
41
+ db_config = extract_db_config(event.payload)
42
+
43
+ attributes = {
44
+ sql: sql,
45
+ duration_ms: duration_ms(event),
46
+ cached: cached
47
+ }
48
+
49
+ attributes[:statement_name] = statement_name if statement_name && statement_name != "SQL"
50
+ attributes[:connection_id] = connection_id if connection_id
51
+
52
+ add_db_config_attributes(attributes, db_config)
53
+
54
+ message = build_log_message(statement_name)
55
+
56
+ log_structured_event(
57
+ message: message,
58
+ level: :info,
59
+ attributes: attributes
60
+ )
61
+ end
62
+
63
+ private
64
+
65
+ def build_log_message(statement_name)
66
+ if statement_name && statement_name != "SQL"
67
+ "Database query: #{statement_name}"
68
+ else
69
+ "Database query"
70
+ end
71
+ end
72
+
73
+ def extract_db_config(payload)
74
+ connection = payload[:connection]
75
+
76
+ return unless connection
77
+
78
+ extract_db_config_from_connection(connection)
79
+ end
80
+
81
+ def add_db_config_attributes(attributes, db_config)
82
+ return unless db_config
83
+
84
+ attributes[:db_system] = db_config[:adapter] if db_config[:adapter]
85
+
86
+ if db_config[:database]
87
+ db_name = db_config[:database]
88
+
89
+ if db_config[:adapter] == "sqlite3" && db_name.include?("/")
90
+ db_name = File.basename(db_name)
91
+ end
92
+
93
+ attributes[:db_name] = db_name
94
+ end
95
+
96
+ attributes[:server_address] = db_config[:host] if db_config[:host]
97
+ attributes[:server_port] = db_config[:port] if db_config[:port]
98
+ attributes[:server_socket_address] = db_config[:socket] if db_config[:socket]
99
+ end
100
+
101
+ if ::Rails.version.to_f >= 6.1
102
+ def extract_db_config_from_connection(connection)
103
+ if connection.pool.respond_to?(:db_config)
104
+ db_config = connection.pool.db_config
105
+ if db_config.respond_to?(:configuration_hash)
106
+ return db_config.configuration_hash
107
+ elsif db_config.respond_to?(:config)
108
+ return db_config.config
109
+ end
110
+ end
111
+
112
+ extract_db_config_fallback(connection)
113
+ end
114
+ else
115
+ # Rails 6.0 and earlier use spec API
116
+ def extract_db_config_from_connection(connection)
117
+ if connection.pool.respond_to?(:spec)
118
+ spec = connection.pool.spec
119
+ if spec.respond_to?(:config)
120
+ return spec.config
121
+ end
122
+ end
123
+
124
+ extract_db_config_fallback(connection)
125
+ end
126
+ end
127
+
128
+ def extract_db_config_fallback(connection)
129
+ connection.config if connection.respond_to?(:config)
130
+ end
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sentry
4
+ module Rails
5
+ module LogSubscribers
6
+ # Shared utility module for filtering sensitive parameters in log subscribers.
7
+ #
8
+ # This module provides consistent parameter filtering across all Sentry Rails
9
+ # log subscribers, leveraging Rails' built-in parameter filtering when available.
10
+ # It automatically detects the correct Rails parameter filtering API based on
11
+ # the Rails version and includes the appropriate implementation module.
12
+ #
13
+ # @example Usage in a log subscriber
14
+ # class MySubscriber < Sentry::Rails::LogSubscriber
15
+ # include Sentry::Rails::LogSubscribers::ParameterFilter
16
+ #
17
+ # def my_event(event)
18
+ # if Sentry.configuration.send_default_pii && event.payload[:params]
19
+ # filtered_params = filter_sensitive_params(event.payload[:params])
20
+ # attributes[:params] = filtered_params unless filtered_params.empty?
21
+ # end
22
+ # end
23
+ # end
24
+ module ParameterFilter
25
+ EMPTY_HASH = {}.freeze
26
+
27
+ if ::Rails.version.to_f >= 6.0
28
+ def self.backend
29
+ ActiveSupport::ParameterFilter
30
+ end
31
+ else
32
+ def self.backend
33
+ ActionDispatch::Http::ParameterFilter
34
+ end
35
+ end
36
+
37
+ # Filter sensitive parameters from a hash, respecting Rails configuration.
38
+ #
39
+ # @param params [Hash] The parameters to filter
40
+ # @return [Hash] Filtered parameters with sensitive data removed
41
+ def filter_sensitive_params(params)
42
+ return EMPTY_HASH unless params.is_a?(Hash)
43
+
44
+ filter_parameters = ::Rails.application.config.filter_parameters
45
+ parameter_filter = ParameterFilter.backend.new(filter_parameters)
46
+
47
+ parameter_filter.filter(params)
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -9,17 +9,6 @@ module Sentry
9
9
  super
10
10
  end
11
11
  end
12
-
13
- module OldStreamingReporter
14
- def self.included(base)
15
- base.send(:alias_method_chain, :log_error, :raven)
16
- end
17
-
18
- def log_error_with_raven(exception)
19
- Sentry::Rails.capture_exception(exception)
20
- log_error_without_raven(exception)
21
- end
22
- end
23
12
  end
24
13
  end
25
14
  end
@@ -49,6 +49,7 @@ module Sentry
49
49
  setup_backtrace_cleanup_callback
50
50
  inject_breadcrumbs_logger
51
51
  activate_tracing
52
+ activate_structured_logging
52
53
 
53
54
  register_error_subscriber(app) if ::Rails.version.to_f >= 7.0 && Sentry.configuration.rails.register_error_subscriber
54
55
 
@@ -100,17 +101,12 @@ module Sentry
100
101
  end
101
102
 
102
103
  def inject_breadcrumbs_logger
103
- if Sentry.configuration.breadcrumbs_logger.include?(:active_support_logger)
104
+ if Sentry.configuration.breadcrumbs_logger.include?(:active_support_logger) ||
105
+ ## legacy name redirected for backwards compat
106
+ Sentry.configuration.breadcrumbs_logger.include?(:monotonic_active_support_logger)
104
107
  require "sentry/rails/breadcrumb/active_support_logger"
105
108
  Sentry::Rails::Breadcrumb::ActiveSupportLogger.inject(Sentry.configuration.rails.active_support_logger_subscription_items)
106
109
  end
107
-
108
- if Sentry.configuration.breadcrumbs_logger.include?(:monotonic_active_support_logger)
109
- return warn "Usage of `monotonic_active_support_logger` require a version of Rails >= 6.1, please upgrade your Rails version or use another logger" if ::Rails.version.to_f < 6.1
110
-
111
- require "sentry/rails/breadcrumb/monotonic_active_support_logger"
112
- Sentry::Rails::Breadcrumb::MonotonicActiveSupportLogger.inject
113
- end
114
110
  end
115
111
 
116
112
  def setup_backtrace_cleanup_callback
@@ -138,6 +134,12 @@ module Sentry
138
134
  end
139
135
  end
140
136
 
137
+ def activate_structured_logging
138
+ if Sentry.configuration.rails.structured_logging.enabled? && Sentry.configuration.enable_logs
139
+ Sentry::Rails::StructuredLogging.attach(Sentry.configuration.rails.structured_logging)
140
+ end
141
+ end
142
+
141
143
  def register_error_subscriber(app)
142
144
  require "sentry/rails/error_subscriber"
143
145
  app.executor.error_reporter.subscribe(Sentry::Rails::ErrorSubscriber.new)
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "sentry/rails/log_subscriber"
4
+ require "sentry/rails/log_subscribers/action_controller_subscriber"
5
+ require "sentry/rails/log_subscribers/active_record_subscriber"
6
+ require "sentry/rails/log_subscribers/active_job_subscriber"
7
+ require "sentry/rails/log_subscribers/action_mailer_subscriber"
8
+
9
+ module Sentry
10
+ module Rails
11
+ module StructuredLogging
12
+ class << self
13
+ def attach(config)
14
+ config.subscribers.each do |component, subscriber_class|
15
+ subscriber_class.attach_to component
16
+ end
17
+ rescue => e
18
+ Sentry.configuration.sdk_logger.error("Failed to attach structured loggers: #{e.message}")
19
+ Sentry.configuration.sdk_logger.error(e.backtrace.join("\n"))
20
+ end
21
+
22
+ def detach(config)
23
+ config.subscribers.each do |component, subscriber_class|
24
+ subscriber_class.detach_from component
25
+ end
26
+ rescue => e
27
+ Sentry.configuration.sdk_logger.debug("Error during detaching loggers: #{e.message}")
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Sentry
4
4
  module Rails
5
- VERSION = "5.26.0"
5
+ VERSION = "6.0.0"
6
6
  end
7
7
  end
data/lib/sentry/rails.rb CHANGED
@@ -5,6 +5,7 @@ require "sentry-ruby"
5
5
  require "sentry/integrable"
6
6
  require "sentry/rails/tracing"
7
7
  require "sentry/rails/configuration"
8
+ require "sentry/rails/structured_logging"
8
9
  require "sentry/rails/engine"
9
10
  require "sentry/rails/railtie"
10
11
 
data/sentry-rails.gemspec CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
11
11
  spec.license = 'MIT'
12
12
 
13
13
  spec.platform = Gem::Platform::RUBY
14
- spec.required_ruby_version = '>= 2.4'
14
+ spec.required_ruby_version = '>= 2.7'
15
15
  spec.extra_rdoc_files = ["README.md", "LICENSE.txt"]
16
16
  spec.files = `git ls-files | grep -Ev '^(spec|benchmarks|examples|\.rubocop\.yml)'`.split("\n")
17
17
 
@@ -30,6 +30,6 @@ Gem::Specification.new do |spec|
30
30
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
31
  spec.require_paths = ["lib"]
32
32
 
33
- spec.add_dependency "railties", ">= 5.0"
34
- spec.add_dependency "sentry-ruby", "~> 5.26.0"
33
+ spec.add_dependency "railties", ">= 5.2.0"
34
+ spec.add_dependency "sentry-ruby", "~> 6.0.0"
35
35
  end