2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-01-18 08:21:42 -05:00
|
|
|
module Gitlab
|
|
|
|
module Profiler
|
2020-02-25 13:09:02 -05:00
|
|
|
extend WithRequestStore
|
|
|
|
|
2019-08-31 15:25:25 -04:00
|
|
|
FILTERED_STRING = '[FILTERED]'
|
2018-01-18 08:21:42 -05:00
|
|
|
|
|
|
|
IGNORE_BACKTRACES = %w[
|
|
|
|
lib/gitlab/i18n.rb
|
|
|
|
lib/gitlab/request_context.rb
|
|
|
|
config/initializers
|
|
|
|
lib/gitlab/database/load_balancing/
|
|
|
|
lib/gitlab/etag_caching/
|
|
|
|
lib/gitlab/metrics/
|
|
|
|
lib/gitlab/middleware/
|
2018-06-15 08:29:55 -04:00
|
|
|
ee/lib/gitlab/middleware/
|
2018-01-18 08:21:42 -05:00
|
|
|
lib/gitlab/performance_bar/
|
|
|
|
lib/gitlab/request_profiler/
|
2019-04-13 01:54:32 -04:00
|
|
|
lib/gitlab/query_limiting/
|
|
|
|
lib/gitlab/tracing/
|
2018-01-18 08:21:42 -05:00
|
|
|
lib/gitlab/profiler.rb
|
2019-04-13 01:54:32 -04:00
|
|
|
lib/gitlab/correlation_id.rb
|
|
|
|
lib/gitlab/webpack/dev_server_middleware.rb
|
2019-08-01 02:07:42 -04:00
|
|
|
lib/gitlab/sidekiq_status/
|
|
|
|
lib/gitlab/sidekiq_logging/
|
|
|
|
lib/gitlab/sidekiq_middleware/
|
2018-01-18 08:21:42 -05:00
|
|
|
].freeze
|
|
|
|
|
|
|
|
# Takes a URL to profile (can be a fully-qualified URL, or an absolute path)
|
|
|
|
# and returns the ruby-prof profile result. Formatting that result is the
|
|
|
|
# caller's responsibility. Requests are GET requests unless post_data is
|
|
|
|
# passed.
|
|
|
|
#
|
|
|
|
# Optional arguments:
|
|
|
|
# - logger: will be used for SQL logging, including a summary at the end of
|
|
|
|
# the log file of the total time spent per model class.
|
|
|
|
#
|
|
|
|
# - post_data: a string of raw POST data to use. Changes the HTTP verb to
|
|
|
|
# POST.
|
|
|
|
#
|
2019-10-09 08:06:13 -04:00
|
|
|
# - user: a user to authenticate as.
|
2018-01-18 08:21:42 -05:00
|
|
|
#
|
|
|
|
# - private_token: instead of providing a user instance, the token can be
|
|
|
|
# given as a string. Takes precedence over the user option.
|
|
|
|
def self.profile(url, logger: nil, post_data: nil, user: nil, private_token: nil)
|
|
|
|
app = ActionDispatch::Integration::Session.new(Rails.application)
|
|
|
|
verb = :get
|
|
|
|
headers = {}
|
|
|
|
|
|
|
|
if post_data
|
|
|
|
verb = :post
|
|
|
|
headers['Content-Type'] = 'application/json'
|
|
|
|
end
|
|
|
|
|
2018-11-23 07:14:45 -05:00
|
|
|
if private_token
|
|
|
|
headers['Private-Token'] = private_token
|
|
|
|
user = nil # private_token overrides user
|
2018-01-18 08:21:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
logger = create_custom_logger(logger, private_token: private_token)
|
|
|
|
|
2020-02-25 13:09:02 -05:00
|
|
|
result = with_request_store do
|
|
|
|
# Make an initial call for an asset path in development mode to avoid
|
|
|
|
# sprockets dominating the profiler output.
|
|
|
|
ActionController::Base.helpers.asset_path('katex.css') if Rails.env.development?
|
2018-01-18 08:21:42 -05:00
|
|
|
|
2020-02-25 13:09:02 -05:00
|
|
|
# Rails loads internationalization files lazily the first time a
|
|
|
|
# translation is needed. Running this prevents this overhead from showing
|
|
|
|
# up in profiles.
|
|
|
|
::I18n.t('.')[:test_string]
|
2018-01-18 08:21:42 -05:00
|
|
|
|
2020-02-25 13:09:02 -05:00
|
|
|
# Remove API route mounting from the profile.
|
|
|
|
app.get('/api/v4/users')
|
2018-01-18 08:21:42 -05:00
|
|
|
|
2020-02-25 13:09:02 -05:00
|
|
|
with_custom_logger(logger) do
|
|
|
|
with_user(user) do
|
|
|
|
RubyProf.profile { app.public_send(verb, url, params: post_data, headers: headers) } # rubocop:disable GitlabSecurity/PublicSend
|
|
|
|
end
|
2018-11-23 07:14:45 -05:00
|
|
|
end
|
2018-01-18 08:21:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
log_load_times_by_model(logger)
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.create_custom_logger(logger, private_token: nil)
|
|
|
|
return unless logger
|
|
|
|
|
|
|
|
logger.dup.tap do |new_logger|
|
|
|
|
new_logger.instance_variable_set(:@private_token, private_token)
|
|
|
|
|
|
|
|
class << new_logger
|
|
|
|
attr_reader :load_times_by_model, :private_token
|
|
|
|
|
|
|
|
def debug(message, *)
|
2019-08-22 08:17:11 -04:00
|
|
|
message = message.gsub(private_token, FILTERED_STRING) if private_token
|
2018-01-18 08:21:42 -05:00
|
|
|
|
|
|
|
_, type, time = *message.match(/(\w+) Load \(([0-9.]+)ms\)/)
|
|
|
|
|
|
|
|
if type && time
|
|
|
|
@load_times_by_model ||= {}
|
2018-03-21 12:22:56 -04:00
|
|
|
@load_times_by_model[type] ||= []
|
|
|
|
@load_times_by_model[type] << time.to_f
|
2018-01-18 08:21:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
super
|
|
|
|
|
2019-12-27 10:08:16 -05:00
|
|
|
Gitlab::BacktraceCleaner.clean_backtrace(caller).each do |caller_line|
|
2018-01-18 08:21:42 -05:00
|
|
|
stripped_caller_line = caller_line.sub("#{Rails.root}/", '')
|
|
|
|
|
|
|
|
super(" ↳ #{stripped_caller_line}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.with_custom_logger(logger)
|
|
|
|
original_colorize_logging = ActiveSupport::LogSubscriber.colorize_logging
|
2021-07-23 08:09:05 -04:00
|
|
|
original_activerecord_logger = ApplicationRecord.logger
|
2018-01-18 08:21:42 -05:00
|
|
|
original_actioncontroller_logger = ActionController::Base.logger
|
|
|
|
|
|
|
|
if logger
|
|
|
|
ActiveSupport::LogSubscriber.colorize_logging = false
|
2021-07-23 08:09:05 -04:00
|
|
|
ApplicationRecord.logger = logger
|
2018-01-18 08:21:42 -05:00
|
|
|
ActionController::Base.logger = logger
|
|
|
|
end
|
|
|
|
|
2018-11-23 07:14:45 -05:00
|
|
|
yield.tap do
|
|
|
|
ActiveSupport::LogSubscriber.colorize_logging = original_colorize_logging
|
2021-07-23 08:09:05 -04:00
|
|
|
ApplicationRecord.logger = original_activerecord_logger
|
2018-11-23 07:14:45 -05:00
|
|
|
ActionController::Base.logger = original_actioncontroller_logger
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.with_user(user)
|
|
|
|
if user
|
|
|
|
API::Helpers::CommonHelpers.send(:define_method, :find_current_user!) { user } # rubocop:disable GitlabSecurity/PublicSend
|
|
|
|
ApplicationController.send(:define_method, :current_user) { user } # rubocop:disable GitlabSecurity/PublicSend
|
|
|
|
ApplicationController.send(:define_method, :authenticate_user!) { } # rubocop:disable GitlabSecurity/PublicSend
|
|
|
|
end
|
2018-01-18 08:21:42 -05:00
|
|
|
|
2018-11-23 07:14:45 -05:00
|
|
|
yield.tap do
|
|
|
|
remove_method(API::Helpers::CommonHelpers, :find_current_user!)
|
|
|
|
remove_method(ApplicationController, :current_user)
|
|
|
|
remove_method(ApplicationController, :authenticate_user!)
|
|
|
|
end
|
|
|
|
end
|
2018-01-18 08:21:42 -05:00
|
|
|
|
2018-11-23 07:14:45 -05:00
|
|
|
def self.remove_method(klass, meth)
|
|
|
|
klass.send(:remove_method, meth) if klass.instance_methods(false).include?(meth) # rubocop:disable GitlabSecurity/PublicSend
|
2018-01-18 08:21:42 -05:00
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-01-18 08:21:42 -05:00
|
|
|
def self.log_load_times_by_model(logger)
|
|
|
|
return unless logger.respond_to?(:load_times_by_model)
|
|
|
|
|
2018-03-21 12:22:56 -04:00
|
|
|
summarised_load_times = logger.load_times_by_model.to_a.map do |(model, times)|
|
|
|
|
[model, times.count, times.sum]
|
|
|
|
end
|
|
|
|
|
2019-07-24 17:08:25 -04:00
|
|
|
summarised_load_times.sort_by(&:last).reverse_each do |(model, query_count, time)|
|
2018-03-21 12:22:56 -04:00
|
|
|
logger.info("#{model} total (#{query_count}): #{time.round(2)}ms")
|
2018-01-18 08:21:42 -05:00
|
|
|
end
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-07-19 08:38:57 -04:00
|
|
|
|
|
|
|
def self.print_by_total_time(result, options = {})
|
2020-03-25 14:08:10 -04:00
|
|
|
default_options = { sort_method: :total_time, filter_by: :total_time }
|
2018-07-19 08:38:57 -04:00
|
|
|
|
2021-06-08 11:10:00 -04:00
|
|
|
RubyProf::FlatPrinter.new(result).print($stdout, default_options.merge(options))
|
2018-07-19 08:38:57 -04:00
|
|
|
end
|
2018-01-18 08:21:42 -05:00
|
|
|
end
|
|
|
|
end
|