2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
# Inspired by https://github.com/peek/peek-pg/blob/master/lib/peek/views/pg.rb
|
|
|
|
# PEEK_DB_CLIENT is a constant set in config/initializers/peek.rb
|
|
|
|
module Gitlab
|
|
|
|
module PerformanceBar
|
|
|
|
module PeekQueryTracker
|
|
|
|
def sorted_queries
|
|
|
|
PEEK_DB_CLIENT.query_details
|
|
|
|
.sort { |a, b| b[:duration] <=> a[:duration] }
|
|
|
|
end
|
|
|
|
|
|
|
|
def results
|
|
|
|
super.merge(queries: sorted_queries)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def setup_subscribers
|
|
|
|
super
|
|
|
|
|
|
|
|
# Reset each counter when a new request starts
|
|
|
|
before_request do
|
|
|
|
PEEK_DB_CLIENT.query_details = []
|
|
|
|
end
|
|
|
|
|
|
|
|
subscribe('sql.active_record') do |_, start, finish, _, data|
|
2018-12-05 23:21:45 +05:30
|
|
|
if Gitlab::SafeRequestStore.store[:peek_enabled]
|
2019-07-31 22:56:46 +05:30
|
|
|
unless data[:cached]
|
2019-09-04 21:01:54 +05:30
|
|
|
backtrace = Gitlab::Profiler.clean_backtrace(caller)
|
|
|
|
track_query(data[:sql].strip, data[:binds], backtrace, start, finish)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
def track_query(raw_query, bindings, backtrace, start, finish)
|
2018-03-17 18:26:18 +05:30
|
|
|
duration = (finish - start) * 1000.0
|
2019-09-04 21:01:54 +05:30
|
|
|
query_info = { duration: duration.round(3), sql: raw_query, backtrace: backtrace }
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
PEEK_DB_CLIENT.query_details << query_info
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|