Class: Sentry::Rails::LogSubscribers::ActiveRecordSubscriber
- Inherits:
-
Sentry::Rails::LogSubscriber
- Object
- ActiveSupport::LogSubscriber
- Sentry::Rails::LogSubscriber
- Sentry::Rails::LogSubscribers::ActiveRecordSubscriber
- Defined in:
- lib/sentry/rails/log_subscribers/active_record_subscriber.rb
Overview
LogSubscriber for ActiveRecord events that captures database queries and logs them using Sentry's structured logging system.
This subscriber captures sql.active_record events and formats them with relevant database information including SQL queries, duration, database configuration, and caching information.
Constant Summary collapse
- EXCLUDED_NAMES =
["SCHEMA", "TRANSACTION"].freeze
- EMPTY_ARRAY =
[].freeze
Constants inherited from Sentry::Rails::LogSubscriber
Sentry::Rails::LogSubscriber::ORIGIN
Instance Method Summary collapse
-
#sql(event) ⇒ Object
Handle sql.active_record events.
- #type_casted_binds(event) ⇒ Object
Methods inherited from Sentry::Rails::LogSubscriber
Instance Method Details
#sql(event) ⇒ Object
Handle sql.active_record events
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/sentry/rails/log_subscribers/active_record_subscriber.rb', line 28 def sql(event) return unless Sentry.initialized? return if logger && !logger.info? return if EXCLUDED_NAMES.include?(event.payload[:name]) sql = event.payload[:sql] statement_name = event.payload[:name] # Rails 5.0.0 doesn't include :cached in the payload, it was added in Rails 5.1 cached = event.payload.fetch(:cached, false) connection_id = event.payload[:connection_id] attributes = { sql: sql, duration_ms: duration_ms(event), cached: cached } binds = event.payload[:binds] if Sentry.configuration.data_collection.database_query_data && (binds && !binds.empty?) type_casted_binds = type_casted_binds(event) type_casted_binds.each_with_index do |value, index| bind = binds[index] name = bind.respond_to?(:name) ? bind.name : index.to_s attributes["db.query.parameter.#{name}"] = value.to_s end end attributes[:statement_name] = statement_name if statement_name && statement_name != "SQL" attributes[:connection_id] = connection_id if connection_id maybe_add_db_config_attributes(attributes, event.payload) = (statement_name) log_structured_event( message: , level: :info, attributes: attributes ) rescue => e log_debug("[#{self.class}] failed to log sql event: #{e.}") end |
#type_casted_binds(event) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/sentry/rails/log_subscribers/active_record_subscriber.rb', line 75 def type_casted_binds(event) binds = event.payload[:type_casted_binds] # When a query is cached, binds are a callable, # and under JRuby they're always a callable. if binds.respond_to?(:call) binds.call else binds end end |