Class: Sentry::Rails::LogSubscribers::ActiveRecordSubscriber

Inherits:
Sentry::Rails::LogSubscriber show all
Includes:
ParameterFilter
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.

Examples:

Usage

# Automatically attached when structured logging is enabled for :active_record
Sentry.init do |config|
  config.enable_logs = true
  config.rails.structured_logging = true
  config.rails.structured_logging.subscribers = { active_record: Sentry::Rails::LogSubscribers::ActiveRecordSubscriber }
end

Constant Summary collapse

EXCLUDED_NAMES =
["SCHEMA", "TRANSACTION"].freeze

Constants included from ParameterFilter

ParameterFilter::EMPTY_HASH

Instance Method Summary collapse

Methods included from ParameterFilter

backend, #filter_sensitive_params

Methods inherited from Sentry::Rails::LogSubscriber

#detach_from

Instance Method Details

#sql(event) ⇒ Object

Handle sql.active_record events

Parameters:

  • event (ActiveSupport::Notifications::Event)

    The SQL event



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
# File 'lib/sentry/rails/log_subscribers/active_record_subscriber.rb', line 31

def sql(event)
  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]

  db_config = extract_db_config(event.payload)

  attributes = {
    sql: sql,
    duration_ms: duration_ms(event),
    cached: cached
  }

  attributes[:statement_name] = statement_name if statement_name && statement_name != "SQL"
  attributes[:connection_id] = connection_id if connection_id

  add_db_config_attributes(attributes, db_config)

  message = build_log_message(statement_name)

  log_structured_event(
    message: message,
    level: :info,
    attributes: attributes
  )
end