2019-07-26 09:03:00 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Peek
|
|
|
|
module Views
|
|
|
|
class ActiveRecord < DetailedView
|
2019-08-06 11:04:43 -04:00
|
|
|
DEFAULT_THRESHOLDS = {
|
|
|
|
calls: 100,
|
2019-09-03 08:13:36 -04:00
|
|
|
duration: 3000,
|
|
|
|
individual_call: 1000
|
2019-08-06 11:04:43 -04:00
|
|
|
}.freeze
|
|
|
|
|
|
|
|
THRESHOLDS = {
|
|
|
|
production: {
|
|
|
|
calls: 100,
|
2019-09-03 08:13:36 -04:00
|
|
|
duration: 15000,
|
|
|
|
individual_call: 5000
|
2019-08-06 11:04:43 -04:00
|
|
|
}
|
|
|
|
}.freeze
|
|
|
|
|
|
|
|
def self.thresholds
|
|
|
|
@thresholds ||= THRESHOLDS.fetch(Rails.env.to_sym, DEFAULT_THRESHOLDS)
|
|
|
|
end
|
|
|
|
|
2021-03-24 14:09:31 -04:00
|
|
|
def results
|
|
|
|
super.merge(summary: summary)
|
|
|
|
end
|
|
|
|
|
2019-07-26 09:03:00 -04:00
|
|
|
private
|
|
|
|
|
2021-03-24 14:09:31 -04:00
|
|
|
def summary
|
|
|
|
detail_store.each_with_object({}) do |item, count|
|
|
|
|
count_summary(item, count)
|
|
|
|
end
|
2020-10-05 14:08:51 -04:00
|
|
|
end
|
|
|
|
|
2021-03-24 14:09:31 -04:00
|
|
|
def count_summary(item, count)
|
|
|
|
if item[:cached].present?
|
|
|
|
count[item[:cached]] ||= 0
|
|
|
|
count[item[:cached]] += 1
|
|
|
|
end
|
|
|
|
|
|
|
|
if item[:transaction].present?
|
|
|
|
count[item[:transaction]] ||= 0
|
|
|
|
count[item[:transaction]] += 1
|
|
|
|
end
|
2021-06-04 11:10:25 -04:00
|
|
|
|
2021-09-21 14:11:18 -04:00
|
|
|
count[item[:db_role]] ||= 0
|
|
|
|
count[item[:db_role]] += 1
|
2020-10-05 14:08:51 -04:00
|
|
|
end
|
|
|
|
|
2019-07-26 09:03:00 -04:00
|
|
|
def setup_subscribers
|
|
|
|
super
|
|
|
|
|
|
|
|
subscribe('sql.active_record') do |_, start, finish, _, data|
|
2021-03-05 07:08:55 -05:00
|
|
|
detail_store << generate_detail(start, finish, data) if Gitlab::PerformanceBar.enabled_for_request?
|
2019-07-26 09:03:00 -04:00
|
|
|
end
|
|
|
|
end
|
2021-03-05 07:08:55 -05:00
|
|
|
|
|
|
|
def generate_detail(start, finish, data)
|
|
|
|
{
|
2021-03-24 14:09:31 -04:00
|
|
|
start: start,
|
2021-03-05 07:08:55 -05:00
|
|
|
duration: finish - start,
|
|
|
|
sql: data[:sql].strip,
|
|
|
|
backtrace: Gitlab::BacktraceCleaner.clean_backtrace(caller),
|
2021-03-24 14:09:31 -04:00
|
|
|
cached: data[:cached] ? 'Cached' : '',
|
2021-06-04 11:10:25 -04:00
|
|
|
transaction: data[:connection].transaction_open? ? 'In a transaction' : '',
|
2021-07-28 02:09:29 -04:00
|
|
|
db_role: db_role(data),
|
|
|
|
db_config_name: "Config name: #{::Gitlab::Database.db_config_name(data[:connection])}"
|
2021-03-05 07:08:55 -05:00
|
|
|
}
|
|
|
|
end
|
2021-06-04 11:10:25 -04:00
|
|
|
|
|
|
|
def db_role(data)
|
|
|
|
role = ::Gitlab::Database::LoadBalancing.db_role_for_connection(data[:connection]) ||
|
|
|
|
::Gitlab::Database::LoadBalancing::ROLE_UNKNOWN
|
|
|
|
|
2021-07-28 02:09:29 -04:00
|
|
|
"Role: #{role.to_s.capitalize}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def format_call_details(call)
|
2021-08-06 11:10:05 -04:00
|
|
|
if ENV['GITLAB_MULTIPLE_DATABASE_METRICS']
|
2021-07-28 02:09:29 -04:00
|
|
|
super
|
|
|
|
else
|
|
|
|
super.except(:db_config_name)
|
|
|
|
end
|
2021-06-04 11:10:25 -04:00
|
|
|
end
|
2019-07-26 09:03:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|