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
|
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' : '',
|
|
|
|
transaction: data[:connection].transaction_open? ? 'In a transaction' : ''
|
2021-03-05 07:08:55 -05:00
|
|
|
}
|
|
|
|
end
|
2019-07-26 09:03:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-03-05 07:08:55 -05:00
|
|
|
|
|
|
|
Peek::Views::ActiveRecord.prepend_if_ee('EE::Peek::Views::ActiveRecord')
|