Commit 651eb51e authored by Rémy Coutable's avatar Rémy Coutable

Display queries duration in performance bar queries modal

Signed-off-by: default avatarRémy Coutable <remy@rymai.me>
parent 5f218eb5
...@@ -4,44 +4,26 @@ Peek.into Peek::Views::Host ...@@ -4,44 +4,26 @@ Peek.into Peek::Views::Host
Peek.into Peek::Views::PerformanceBar Peek.into Peek::Views::PerformanceBar
if Gitlab::Database.mysql? if Gitlab::Database.mysql?
require 'peek-mysql' require 'peek-mysql'
Peek.into Peek::Views::Mysql2 PEEK_DB_CLIENT = ::Mysql2::Client
PEEK_DB_VIEW = Peek::Views::Mysql2
Peek.into PEEK_DB_VIEW
else else
require 'peek-pg' require 'peek-pg'
Peek.into Peek::Views::PG PEEK_DB_CLIENT = ::PG::Connection
PEEK_DB_VIEW = Peek::Views::PG
Peek.into PEEK_DB_VIEW
end end
Peek.into Peek::Views::Redis Peek.into Peek::Views::Redis
Peek.into Peek::Views::Sidekiq Peek.into Peek::Views::Sidekiq
Peek.into Peek::Views::Rblineprof Peek.into Peek::Views::Rblineprof
Peek.into Peek::Views::GC Peek.into Peek::Views::GC
if Gitlab::Database.mysql?
class Mysql2::Client
class << self
attr_accessor :query_details
end
self.query_details = Concurrent::Array.new
end
module Peek class PEEK_DB_CLIENT
module Views
class Mysql2 < View
prepend ::Gitlab::PerformanceBar::PeekMysqlWithQueries
end
end
end
else
class PG::Connection
class << self class << self
attr_accessor :query_details attr_accessor :query_details
end end
self.query_details = Concurrent::Array.new self.query_details = Concurrent::Array.new
end
module Peek
module Views
class PG < View
prepend ::Gitlab::PerformanceBar::PeekPgWithQueries
end
end
end
end end
PEEK_DB_VIEW.prepend ::Gitlab::PerformanceBar::PeekQueryTracker
# Inspired by https://github.com/peek/peek-mysql2/blob/master/lib/peek/views/mysql2.rb
module Gitlab
module PerformanceBar
module PeekMysqlWithQueries
def queries
::Mysql2::Client.query_details
end
def results
super.merge(queries: queries)
end
private
def setup_subscribers
super
# Reset each counter when a new request starts
before_request do
::Mysql2::Client.query_details = []
end
subscribe('sql.active_record') do |_, start, finish, _, data|
if RequestStore.active? && RequestStore.store[:peek_enabled]
track_query(data[:sql].strip, data[:binds], start, finish)
end
end
end
def track_query(raw_query, bindings, start, finish)
query = Gitlab::Sherlock::Query.new(raw_query, start, finish)
::Mysql2::Client.query_details << query.formatted_query
end
end
end
end
# Inspired by https://github.com/peek/peek-pg/blob/master/lib/peek/views/pg.rb # Inspired by https://github.com/peek/peek-pg/blob/master/lib/peek/views/pg.rb
module Gitlab module Gitlab
module PerformanceBar module PerformanceBar
module PeekPgWithQueries module PeekQueryTracker
def queries def sorted_queries
::PG::Connection.query_details PEEK_DB_CLIENT.query_details.
sort { |a, b| b[:duration] <=> a[:duration] }
end end
def results def results
super.merge(queries: queries) super.merge(queries: sorted_queries)
end end
private private
...@@ -17,7 +18,7 @@ module Gitlab ...@@ -17,7 +18,7 @@ module Gitlab
# Reset each counter when a new request starts # Reset each counter when a new request starts
before_request do before_request do
::PG::Connection.query_details = [] PEEK_DB_CLIENT.query_details = []
end end
subscribe('sql.active_record') do |_, start, finish, _, data| subscribe('sql.active_record') do |_, start, finish, _, data|
...@@ -29,7 +30,8 @@ module Gitlab ...@@ -29,7 +30,8 @@ module Gitlab
def track_query(raw_query, bindings, start, finish) def track_query(raw_query, bindings, start, finish)
query = Gitlab::Sherlock::Query.new(raw_query, start, finish) query = Gitlab::Sherlock::Query.new(raw_query, start, finish)
::PG::Connection.query_details << query.formatted_query query_info = { duration: query.duration.round(4), sql: query.formatted_query }
PEEK_DB_CLIENT.query_details << query_info
end end
end end
end end
......
...@@ -15,25 +15,31 @@ requestId = null; ...@@ -15,25 +15,31 @@ requestId = null;
return $('#peek').length; return $('#peek').length;
}; };
updatePerformanceBar = function(results) { updatePerformanceBar = function(results) {
var key, label, data, table, html, tr, td; var key, label, data, table, html, tr, duration_td, sql_td, strong;
for (key in results.data) { for (key in results.data) {
for (label in results.data[key]) { for (label in results.data[key]) {
data = results.data[key][label]; data = results.data[key][label];
console.log(data); if (label == 'queries') {
if (Array.isArray(data)) {
table = document.createElement('table'); table = document.createElement('table');
for (var i = 0; i < data.length; i += 1) { for (var i = 0; i < data.length; i += 1) {
tr = document.createElement('tr'); tr = document.createElement('tr');
td = document.createElement('td'); duration_td = document.createElement('td');
sql_td = document.createElement('td');
strong = document.createElement('strong');
strong.append(data[i]['duration'] + 'ms');
duration_td.appendChild(strong);
tr.appendChild(duration_td);
sql_td.appendChild(document.createTextNode(data[i]['sql']));
tr.appendChild(sql_td);
td.appendChild(document.createTextNode(data[i]));
tr.appendChild(td);
table.appendChild(tr); table.appendChild(tr);
} }
$table = $(table).addClass('table'); table.className = 'table';
$("[data-defer-to=" + key + "-" + label + "]").html($table); $("[data-defer-to=" + key + "-" + label + "]").html(table);
} }
else { else {
$("[data-defer-to=" + key + "-" + label + "]").text(results.data[key][label]); $("[data-defer-to=" + key + "-" + label + "]").text(results.data[key][label]);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment