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

71 lines
1.7 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-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' : '',
transaction: data[:connection].transaction_open? ? 'In a transaction' : ''
2021-04-17 20:07:23 +05:30
}
end
2019-10-12 21:52:04 +05:30
end
end
end
2021-04-17 20:07:23 +05:30
2021-06-08 01:23:25 +05:30
Peek::Views::ActiveRecord.prepend_mod_with('Peek::Views::ActiveRecord')