Store Sherlock::Query in Peek adapter

Signed-off-by: Rémy Coutable <remy@rymai.me>
This commit is contained in:
Rémy Coutable 2017-06-01 16:10:17 +02:00
parent d39ecf1ca7
commit 46273e1446
3 changed files with 104 additions and 0 deletions

View file

@ -7,3 +7,35 @@ Peek.into Peek::Views::Redis
Peek.into Peek::Views::Sidekiq
Peek.into Peek::Views::Rblineprof
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
module Views
class Mysql2 < View
prepend ::Gitlab::PerformanceBar::PeekMysqlWithQueries
end
end
end
else
class PG::Connection
class << self
attr_accessor :query_details
end
self.query_details = Concurrent::Array.new
end
module Peek
module Views
class PG < View
prepend ::Gitlab::PerformanceBar::PeekPgWithQueries
end
end
end
end

View file

@ -0,0 +1,36 @@
# 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

View file

@ -0,0 +1,36 @@
# Inspired by https://github.com/peek/peek-pg/blob/master/lib/peek/views/pg.rb
module Gitlab
module PerformanceBar
module PeekPgWithQueries
def queries
::PG::Connection.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
::PG::Connection.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)
::PG::Connection.query_details << query.formatted_query
end
end
end
end