Class: BrainzLab::Rails::SqlLogSubscriber

Inherits:
ActiveSupport::LogSubscriber
  • Object
show all
Defined in:
lib/brainzlab/rails/log_subscriber.rb

Overview

SQL query subscriber to track query details

Constant Summary collapse

IGNORED_PAYLOADS =
%w[SCHEMA].freeze

Instance Method Summary collapse

Instance Method Details

#sql(event) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/brainzlab/rails/log_subscriber.rb', line 104

def sql(event)
  return unless LogSubscriber.formatter

  payload = event.payload
  return if IGNORED_PAYLOADS.include?(payload[:name])

  request_id = Thread.current[:brainzlab_request_id]
  return unless request_id

  # Extract source location from the backtrace
  source = extract_source_location(caller)

  # Normalize SQL for pattern detection (remove specific values)
  sql_pattern = normalize_sql(payload[:sql])

  LogSubscriber.formatter.sql_query(request_id,
                                    name: payload[:name],
                                    duration: event.duration,
                                    sql: payload[:sql],
                                    sql_pattern: sql_pattern,
                                    cached: payload[:cached] || payload[:name] == 'CACHE',
                                    source: source)
end