Commit c6c87003 authored by Alex Kalderimis's avatar Alex Kalderimis

Store duration and allow logging to specific file

parent 0f06648f
...@@ -3,39 +3,53 @@ ...@@ -3,39 +3,53 @@
module ActiveRecord module ActiveRecord
class QueryRecorder class QueryRecorder
attr_reader :log, :skip_cached, :cached, :data attr_reader :log, :skip_cached, :cached, :data
UNKNOWN = %w(unknown unknown).freeze
def initialize(skip_cached: true, query_recorder_debug: false, &block) UNKNOWN = %w[unknown unknown].freeze
@data = Hash.new { |h, k| h[k] = { count: 0, occurrences: [], backtrace: [] } }
def initialize(skip_cached: true, log_file: nil, query_recorder_debug: false, &block)
@data = Hash.new { |h, k| h[k] = { count: 0, occurrences: [], backtrace: [], durations: [] } }
@log = [] @log = []
@cached = [] @cached = []
@skip_cached = skip_cached @skip_cached = skip_cached
@query_recorder_debug = query_recorder_debug @query_recorder_debug = ENV['QUERY_RECORDER_DEBUG'] || query_recorder_debug
@log_file = File.open(log_file, 'w') if log_file && @query_recorder_debug
# force replacement of bind parameters to give tests the ability to check for ids # force replacement of bind parameters to give tests the ability to check for ids
ActiveRecord::Base.connection.unprepared_statement do ActiveRecord::Base.connection.unprepared_statement do
ActiveSupport::Notifications.subscribed(method(:callback), 'sql.active_record', &block) ActiveSupport::Notifications.subscribed(method(:callback), 'sql.active_record', &block)
end end
end end
def show_backtrace(values) def show_backtrace(values, duration)
values[:sql].lines.each do |line| values[:sql].lines.each do |line|
Rails.logger.debug("QueryRecorder SQL: --> #{line}") print_to_log(:SQL, line)
end end
print_to_log(:DURATION, duration)
Gitlab::BacktraceCleaner.clean_backtrace(caller).each do |line| Gitlab::BacktraceCleaner.clean_backtrace(caller).each do |line|
Rails.logger.debug("QueryRecorder backtrace: --> #{line}") print_to_log(:backtrace, line)
end
end
def print_to_log(label, line)
msg = "QueryRecorder #{label}: --> #{line}"
if @log_file
@log_file.puts(msg)
else
Rails.logger.debug(msg)
end end
end end
def get_sql_source(sql) def get_sql_source(sql)
matches = sql.match(/,line:(?<line>.*):in\s+`(?<method>.*)'\*\//) matches = sql.match(%r{,line:(?<line>.*):in\s+`(?<method>.*)'\*/})
matches ? [matches[:line], matches[:method]] : UNKNOWN matches ? [matches[:line], matches[:method]] : UNKNOWN
end end
def store_sql_by_source(values: {}, backtrace: nil) def store_sql_by_source(values: {}, duration: nil, backtrace: nil)
full_name = get_sql_source(values[:sql]).join(':') full_name = get_sql_source(values[:sql]).join(':')
@data[full_name][:count] += 1 @data[full_name][:count] += 1
@data[full_name][:occurrences] << values[:sql] @data[full_name][:occurrences] << values[:sql]
@data[full_name][:backtrace] << backtrace @data[full_name][:backtrace] << backtrace
@data[full_name][:durations] << duration
end end
def find_query(query_regexp, limit, first_only: false) def find_query(query_regexp, limit, first_only: false)
...@@ -57,14 +71,14 @@ module ActiveRecord ...@@ -57,14 +71,14 @@ module ActiveRecord
end end
def callback(name, start, finish, message_id, values) def callback(name, start, finish, message_id, values)
store_backtrace = ENV['QUERY_RECORDER_DEBUG'] || @query_recorder_debug duration = finish - start
backtrace = store_backtrace ? show_backtrace(values) : nil backtrace = @query_recorder_debug ? show_backtrace(values, duration) : nil
if values[:cached] && skip_cached if values[:cached] && skip_cached
@cached << values[:sql] @cached << values[:sql]
elsif !values[:name]&.include?("SCHEMA") elsif !values[:name]&.include?("SCHEMA")
@log << values[:sql] @log << values[:sql]
store_sql_by_source(values: values, backtrace: backtrace) store_sql_by_source(values: values, duration: duration, backtrace: backtrace)
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment