2019-07-25 05:27:42 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-29 09:47:54 +00:00
|
|
|
module ActiveRecord
|
|
|
|
class QueryRecorder
|
2020-02-12 18:09:21 +00:00
|
|
|
attr_reader :log, :skip_cached, :cached, :data
|
|
|
|
UNKNOWN = %w(unknown unknown).freeze
|
2018-03-29 09:47:54 +00:00
|
|
|
|
2020-02-12 18:09:21 +00:00
|
|
|
def initialize(skip_cached: true, query_recorder_debug: false, &block)
|
|
|
|
@data = Hash.new { |h, k| h[k] = { count: 0, occurrences: [], backtrace: [] } }
|
2018-03-29 09:47:54 +00:00
|
|
|
@log = []
|
|
|
|
@cached = []
|
2018-06-03 10:31:41 +00:00
|
|
|
@skip_cached = skip_cached
|
2020-02-12 18:09:21 +00:00
|
|
|
@query_recorder_debug = query_recorder_debug
|
2019-08-28 09:37:05 +00:00
|
|
|
# force replacement of bind parameters to give tests the ability to check for ids
|
|
|
|
ActiveRecord::Base.connection.unprepared_statement do
|
|
|
|
ActiveSupport::Notifications.subscribed(method(:callback), 'sql.active_record', &block)
|
|
|
|
end
|
2018-03-29 09:47:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def show_backtrace(values)
|
|
|
|
Rails.logger.debug("QueryRecorder SQL: #{values[:sql]}")
|
2020-05-12 00:10:11 +00:00
|
|
|
Gitlab::BacktraceCleaner.clean_backtrace(caller).each do |line|
|
|
|
|
Rails.logger.debug("QueryRecorder backtrace: --> #{line}")
|
|
|
|
end
|
2018-03-29 09:47:54 +00:00
|
|
|
end
|
|
|
|
|
2020-02-12 18:09:21 +00:00
|
|
|
def get_sql_source(sql)
|
|
|
|
matches = sql.match(/,line:(?<line>.*):in\s+`(?<method>.*)'\*\//)
|
|
|
|
matches ? [matches[:line], matches[:method]] : UNKNOWN
|
|
|
|
end
|
|
|
|
|
|
|
|
def store_sql_by_source(values: {}, backtrace: nil)
|
|
|
|
full_name = get_sql_source(values[:sql]).join(':')
|
|
|
|
@data[full_name][:count] += 1
|
|
|
|
@data[full_name][:occurrences] << values[:sql]
|
|
|
|
@data[full_name][:backtrace] << backtrace
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_query(query_regexp, limit, first_only: false)
|
|
|
|
out = []
|
|
|
|
|
|
|
|
@data.each_pair do |k, v|
|
|
|
|
if v[:count] > limit && k.match(query_regexp)
|
|
|
|
out << [k, v[:count]]
|
|
|
|
break if first_only
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
out.flatten! if first_only
|
|
|
|
out
|
|
|
|
end
|
|
|
|
|
|
|
|
def occurrences_by_line_method
|
|
|
|
@occurrences_by_line_method ||= @data.sort_by { |_, v| v[:count] }
|
|
|
|
end
|
|
|
|
|
2018-03-29 09:47:54 +00:00
|
|
|
def callback(name, start, finish, message_id, values)
|
2020-02-12 18:09:21 +00:00
|
|
|
store_backtrace = ENV['QUERY_RECORDER_DEBUG'] || @query_recorder_debug
|
|
|
|
backtrace = store_backtrace ? show_backtrace(values) : nil
|
2018-03-29 09:47:54 +00:00
|
|
|
|
2019-01-15 21:05:36 +00:00
|
|
|
if values[:cached] && skip_cached
|
2018-03-29 09:47:54 +00:00
|
|
|
@cached << values[:sql]
|
|
|
|
elsif !values[:name]&.include?("SCHEMA")
|
|
|
|
@log << values[:sql]
|
2020-02-12 18:09:21 +00:00
|
|
|
store_sql_by_source(values: values, backtrace: backtrace)
|
2018-03-29 09:47:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def count
|
2020-02-12 18:09:21 +00:00
|
|
|
@count ||= @log.count
|
2018-03-29 09:47:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def cached_count
|
2020-02-12 18:09:21 +00:00
|
|
|
@cached_count ||= @cached.count
|
2018-03-29 09:47:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def log_message
|
2020-02-12 18:09:21 +00:00
|
|
|
@log_message ||= @log.join("\n\n")
|
2018-03-29 09:47:54 +00:00
|
|
|
end
|
2019-06-19 09:18:04 +00:00
|
|
|
|
|
|
|
def occurrences
|
2020-02-12 18:09:21 +00:00
|
|
|
@occurrences ||= @log.group_by(&:to_s).transform_values(&:count)
|
2019-06-19 09:18:04 +00:00
|
|
|
end
|
2018-03-29 09:47:54 +00:00
|
|
|
end
|
|
|
|
end
|