debian-mirror-gitlab/lib/peek/views/active_record.rb

93 lines
2.3 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
module Peek
module Views
class ActiveRecord < DetailedView
2019-12-04 20:38:33 +05:30
DEFAULT_THRESHOLDS = {
calls: 100,
duration: 3000,
individual_call: 1000
}.freeze
THRESHOLDS = {
production: {
calls: 100,
duration: 15000,
individual_call: 5000
}
}.freeze
def self.thresholds
@thresholds ||= THRESHOLDS.fetch(Rails.env.to_sym, DEFAULT_THRESHOLDS)
end
2021-04-29 21:17:54 +05:30
def results
super.merge(summary: summary)
end
2019-10-12 21:52:04 +05:30
private
2021-04-29 21:17:54 +05:30
def summary
detail_store.each_with_object({}) do |item, count|
count_summary(item, count)
end
2021-01-03 14:25:43 +05:30
end
2021-04-29 21:17:54 +05:30
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-09-04 01:27:46 +05:30
if ::Gitlab::Database::LoadBalancing.enable?
count[item[:db_role]] ||= 0
count[item[:db_role]] += 1
end
2021-01-03 14:25:43 +05:30
end
2019-10-12 21:52:04 +05:30
def setup_subscribers
super
subscribe('sql.active_record') do |_, start, finish, _, data|
2021-04-17 20:07:23 +05:30
detail_store << generate_detail(start, finish, data) if Gitlab::PerformanceBar.enabled_for_request?
2019-10-12 21:52:04 +05:30
end
end
2021-04-17 20:07:23 +05:30
def generate_detail(start, finish, data)
{
2021-04-29 21:17:54 +05:30
start: start,
2021-04-17 20:07:23 +05:30
duration: finish - start,
sql: data[:sql].strip,
backtrace: Gitlab::BacktraceCleaner.clean_backtrace(caller),
2021-04-29 21:17:54 +05:30
cached: data[:cached] ? 'Cached' : '',
2021-09-04 01:27:46 +05:30
transaction: data[:connection].transaction_open? ? 'In a transaction' : '',
2021-10-27 15:23:28 +05:30
db_role: db_role(data),
db_config_name: "Config name: #{::Gitlab::Database.db_config_name(data[:connection])}"
2021-04-17 20:07:23 +05:30
}
end
2021-09-04 01:27:46 +05:30
def db_role(data)
return unless ::Gitlab::Database::LoadBalancing.enable?
role = ::Gitlab::Database::LoadBalancing.db_role_for_connection(data[:connection]) ||
::Gitlab::Database::LoadBalancing::ROLE_UNKNOWN
2021-10-27 15:23:28 +05:30
"Role: #{role.to_s.capitalize}"
end
def format_call_details(call)
if ENV['GITLAB_MULTIPLE_DATABASE_METRICS']
super
else
super.except(:db_config_name)
end
2021-09-04 01:27:46 +05:30
end
2019-10-12 21:52:04 +05:30
end
end
end