diff options
author | Rémy Coutable <remy@rymai.me> | 2017-06-01 16:10:17 +0200 |
---|---|---|
committer | Rémy Coutable <remy@rymai.me> | 2017-06-09 17:21:39 +0200 |
commit | 46273e14465f98ac1db306122cdf06d3d87b319e (patch) | |
tree | 8e440c501a904ffddd2daefba43ce3c8de36d020 /lib | |
parent | d39ecf1ca7e9455abcdeb17c251a2d248a47d471 (diff) | |
download | gitlab-ce-46273e14465f98ac1db306122cdf06d3d87b319e.tar.gz |
Store Sherlock::Query in Peek adapter
Signed-off-by: Rémy Coutable <remy@rymai.me>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/performance_bar/peek_mysql_with_queries.rb | 36 | ||||
-rw-r--r-- | lib/gitlab/performance_bar/peek_pg_with_queries.rb | 36 |
2 files changed, 72 insertions, 0 deletions
diff --git a/lib/gitlab/performance_bar/peek_mysql_with_queries.rb b/lib/gitlab/performance_bar/peek_mysql_with_queries.rb new file mode 100644 index 00000000000..ea577de5a69 --- /dev/null +++ b/lib/gitlab/performance_bar/peek_mysql_with_queries.rb @@ -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 diff --git a/lib/gitlab/performance_bar/peek_pg_with_queries.rb b/lib/gitlab/performance_bar/peek_pg_with_queries.rb new file mode 100644 index 00000000000..6e9cb593d36 --- /dev/null +++ b/lib/gitlab/performance_bar/peek_pg_with_queries.rb @@ -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 |