Module: ActiveRecord::SqlAnalyzer::Monkeypatches::Query

Defined in:
lib/active_record/sql_analyzer/monkeypatches/query.rb

Instance Method Summary collapse

Instance Method Details

#execute(sql, *args) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/active_record/sql_analyzer/monkeypatches/query.rb', line 6

def execute(sql, *args)
  return super unless SqlAnalyzer.config

  safe_sql = nil

  SqlAnalyzer.config[:analyzers].each do |analyzer|
    if SqlAnalyzer.config[:should_log_sample_proc].call(analyzer[:name])
      # This is here rather than above intentionally.
      # We assume we're not going to be analyzing 100% of queries and want to only re-encode
      # when it's actually relevant.
      safe_sql ||= sql.encode(Encoding::UTF_8, invalid: :replace, undef: :replace)

      if safe_sql =~ analyzer[:table_regex]
        SqlAnalyzer.background_processor << {
          sql: safe_sql,
          caller: caller,
          logger: analyzer[:logger_instance],
          tag: Thread.current[:_ar_analyzer_tag],
          request_path: Thread.current[:_ar_analyzer_request_path]
        }
      end
    end
  end

  super
end