2020-12-09 10:10:12 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module PerformanceBar
|
|
|
|
# This class fetches Peek stats stored in redis and logs them in a
|
|
|
|
# structured log (so these can be then analyzed in Kibana)
|
|
|
|
class Stats
|
|
|
|
def initialize(redis)
|
|
|
|
@redis = redis
|
|
|
|
end
|
|
|
|
|
|
|
|
def process(id)
|
|
|
|
data = request(id)
|
|
|
|
return unless data
|
|
|
|
|
|
|
|
log_sql_queries(id, data)
|
|
|
|
rescue => err
|
|
|
|
logger.error(message: "failed to process request id #{id}: #{err.message}")
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def request(id)
|
|
|
|
# Peek gem stores request data under peek:requests:request_id key
|
|
|
|
json_data = @redis.get("peek:requests:#{id}")
|
|
|
|
Gitlab::Json.parse(json_data)
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_sql_queries(id, data)
|
2021-01-28 22:08:52 -05:00
|
|
|
queries_by_location(data).each do |location, queries|
|
|
|
|
next unless location
|
2020-12-09 10:10:12 -05:00
|
|
|
|
2021-01-28 22:08:52 -05:00
|
|
|
duration = queries.sum { |query| query['duration'].to_f }
|
|
|
|
log_info = {
|
|
|
|
method_path: "#{location[:filename]}:#{location[:method]}",
|
|
|
|
filename: location[:filename],
|
2020-12-09 10:10:12 -05:00
|
|
|
type: :sql,
|
|
|
|
request_id: id,
|
2021-01-28 22:08:52 -05:00
|
|
|
count: queries.count,
|
|
|
|
duration_ms: duration
|
|
|
|
}
|
2020-12-09 10:10:12 -05:00
|
|
|
|
|
|
|
logger.info(log_info)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-28 22:08:52 -05:00
|
|
|
def queries_by_location(data)
|
|
|
|
return [] unless queries = data.dig('data', 'active-record', 'details')
|
|
|
|
|
|
|
|
queries.group_by do |query|
|
|
|
|
parse_backtrace(query['backtrace'])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-09 10:10:12 -05:00
|
|
|
def parse_backtrace(backtrace)
|
|
|
|
return unless match = /(?<filename>.*):(?<filenum>\d+):in `(?<method>.*)'/.match(backtrace.first)
|
|
|
|
|
|
|
|
{
|
|
|
|
filename: match[:filename],
|
2021-01-28 22:08:52 -05:00
|
|
|
# filenum may change quite frequently with every change in the file,
|
|
|
|
# because the intention is to aggregate these queries, we group
|
|
|
|
# them rather by method name which should not change so frequently
|
|
|
|
# filenum: match[:filenum].to_i,
|
2020-12-09 10:10:12 -05:00
|
|
|
method: match[:method]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def logger
|
|
|
|
@logger ||= Gitlab::PerformanceBar::Logger.build
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|