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