From 8346da6527ab4b9233605565a022ef9f05d3f4c9 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Sun, 9 Jun 2019 18:18:14 -0700 Subject: [PATCH] Add backtraces to Peek performance bar for SQL calls Just as we have backtraces for Gitaly, we should also have backtraces for SQL calls. This makes it much easier to find the source of the SQL call and optimize N+1 queries and other performance issues with an endpoint. --- changelogs/unreleased/sh-add-backtrace-to-sql-queries.yml | 5 +++++ lib/gitlab/performance_bar/peek_query_tracker.rb | 7 ++++--- 2 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 changelogs/unreleased/sh-add-backtrace-to-sql-queries.yml diff --git a/changelogs/unreleased/sh-add-backtrace-to-sql-queries.yml b/changelogs/unreleased/sh-add-backtrace-to-sql-queries.yml new file mode 100644 index 00000000000..d4ca027d1b9 --- /dev/null +++ b/changelogs/unreleased/sh-add-backtrace-to-sql-queries.yml @@ -0,0 +1,5 @@ +--- +title: Add backtraces to Peek performance bar for SQL calls +merge_request: +author: +type: added diff --git a/lib/gitlab/performance_bar/peek_query_tracker.rb b/lib/gitlab/performance_bar/peek_query_tracker.rb index 16c16aa0265..3a27e26eaba 100644 --- a/lib/gitlab/performance_bar/peek_query_tracker.rb +++ b/lib/gitlab/performance_bar/peek_query_tracker.rb @@ -27,15 +27,16 @@ module Gitlab subscribe('sql.active_record') do |_, start, finish, _, data| if Gitlab::SafeRequestStore.store[:peek_enabled] unless data[:cached] - track_query(data[:sql].strip, data[:binds], start, finish) + backtrace = Gitlab::Profiler.clean_backtrace(caller) + track_query(data[:sql].strip, data[:binds], backtrace, start, finish) end end end end - def track_query(raw_query, bindings, start, finish) + def track_query(raw_query, bindings, backtrace, start, finish) duration = (finish - start) * 1000.0 - query_info = { duration: duration.round(3), sql: raw_query } + query_info = { duration: duration.round(3), sql: raw_query, backtrace: backtrace } PEEK_DB_CLIENT.query_details << query_info end