2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
require 'ruby-prof'
|
2019-10-12 21:52:04 +05:30
|
|
|
require 'memory_profiler'
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module RequestProfiler
|
|
|
|
class Middleware
|
|
|
|
def initialize(app)
|
|
|
|
@app = app
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
|
|
|
if profile?(env)
|
|
|
|
call_with_profiling(env)
|
|
|
|
else
|
|
|
|
@app.call(env)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def profile?(env)
|
|
|
|
header_token = env['HTTP_X_PROFILE_TOKEN']
|
|
|
|
return unless header_token.present?
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
profile_token = Gitlab::RequestProfiler.profile_token
|
2016-09-13 17:45:13 +05:30
|
|
|
return unless profile_token.present?
|
|
|
|
|
|
|
|
header_token == profile_token
|
|
|
|
end
|
|
|
|
|
|
|
|
def call_with_profiling(env)
|
2019-10-12 21:52:04 +05:30
|
|
|
case env['HTTP_X_PROFILE_MODE']
|
|
|
|
when 'execution', nil
|
|
|
|
call_with_call_stack_profiling(env)
|
|
|
|
when 'memory'
|
|
|
|
call_with_memory_profiling(env)
|
|
|
|
else
|
|
|
|
raise ActionController::BadRequest, invalid_profile_mode(env)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def invalid_profile_mode(env)
|
|
|
|
<<~HEREDOC
|
|
|
|
Invalid X-Profile-Mode: #{env['HTTP_X_PROFILE_MODE']}.
|
|
|
|
Supported profile mode request header:
|
|
|
|
- X-Profile-Mode: execution
|
|
|
|
- X-Profile-Mode: memory
|
|
|
|
HEREDOC
|
|
|
|
end
|
|
|
|
|
|
|
|
def call_with_call_stack_profiling(env)
|
2016-09-13 17:45:13 +05:30
|
|
|
ret = nil
|
2019-10-12 21:52:04 +05:30
|
|
|
report = RubyProf::Profile.profile do
|
2020-04-08 14:13:33 +05:30
|
|
|
ret = catch(:warden) do # rubocop:disable Cop/BanCatchThrow
|
2016-09-13 17:45:13 +05:30
|
|
|
@app.call(env)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
generate_report(env, 'execution', 'html') do |file|
|
|
|
|
printer = RubyProf::CallStackPrinter.new(report)
|
|
|
|
printer.print(file)
|
|
|
|
end
|
|
|
|
|
|
|
|
handle_request_ret(ret)
|
|
|
|
end
|
|
|
|
|
|
|
|
def call_with_memory_profiling(env)
|
|
|
|
ret = nil
|
|
|
|
report = MemoryProfiler.report do
|
2020-04-08 14:13:33 +05:30
|
|
|
ret = catch(:warden) do # rubocop:disable Cop/BanCatchThrow
|
2019-10-12 21:52:04 +05:30
|
|
|
@app.call(env)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
generate_report(env, 'memory', 'txt') do |file|
|
|
|
|
report.pretty_print(to_file: file)
|
|
|
|
end
|
|
|
|
|
|
|
|
handle_request_ret(ret)
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_report(env, report_type, extension)
|
|
|
|
file_name = "#{env['PATH_INFO'].tr('/', '|')}_#{Time.current.to_i}"\
|
|
|
|
"_#{report_type}.#{extension}"
|
2016-09-13 17:45:13 +05:30
|
|
|
file_path = "#{PROFILES_DIR}/#{file_name}"
|
|
|
|
|
|
|
|
FileUtils.mkdir_p(PROFILES_DIR)
|
2019-10-12 21:52:04 +05:30
|
|
|
|
|
|
|
begin
|
|
|
|
File.open(file_path, 'wb') do |file|
|
|
|
|
yield(file)
|
|
|
|
end
|
2021-06-08 01:23:25 +05:30
|
|
|
rescue StandardError
|
2019-10-12 21:52:04 +05:30
|
|
|
FileUtils.rm(file_path)
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
2019-10-12 21:52:04 +05:30
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
def handle_request_ret(ret)
|
2016-09-13 17:45:13 +05:30
|
|
|
if ret.is_a?(Array)
|
|
|
|
ret
|
|
|
|
else
|
2020-04-08 14:13:33 +05:30
|
|
|
throw(:warden, ret) # rubocop:disable Cop/BanCatchThrow
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|