summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Speicher <robert@gitlab.com>2018-08-07 20:04:28 +0000
committerRobert Speicher <robert@gitlab.com>2018-08-07 20:04:28 +0000
commit9c0f52714ce9b406a2775a0487843d80e596d9a6 (patch)
treeddefe1af0ef2627047a36938bb596d52b7dcff27
parent9814c646ff872fb29a2390196c116a9a41c0f258 (diff)
parentcf04146ae834a4692f03b5ab989d78eb789858dc (diff)
downloadgitlab-ce-9c0f52714ce9b406a2775a0487843d80e596d9a6.tar.gz
Merge branch 'enable-verbose-query-logs' into 'master'
Enable verbose query logging See merge request gitlab-org/gitlab-ce!20976
-rw-r--r--config/initializers/active_record_verbose_query_logs.rb54
1 files changed, 54 insertions, 0 deletions
diff --git a/config/initializers/active_record_verbose_query_logs.rb b/config/initializers/active_record_verbose_query_logs.rb
new file mode 100644
index 00000000000..44f86fec7e0
--- /dev/null
+++ b/config/initializers/active_record_verbose_query_logs.rb
@@ -0,0 +1,54 @@
+# frozen_string_literal: true
+
+# This is backport of https://github.com/rails/rails/pull/26815/files
+# Enabled by default for every non-production environment
+
+module ActiveRecord
+ class LogSubscriber
+ module VerboseQueryLogs
+ def debug(progname = nil, &block)
+ return unless super
+
+ log_query_source
+ end
+
+ def log_query_source
+ source_line, line_number = extract_callstack(caller_locations)
+
+ if source_line
+ if defined?(::Rails.root)
+ app_root = "#{::Rails.root}/".freeze
+ source_line = source_line.sub(app_root, "")
+ end
+
+ logger.debug(" ↳ #{source_line}:#{line_number}")
+ end
+ end
+
+ def extract_callstack(callstack)
+ line = callstack.find do |frame|
+ frame.absolute_path && !ignored_callstack(frame.absolute_path)
+ end
+
+ offending_line = line || callstack.first
+ [
+ offending_line.path,
+ offending_line.lineno,
+ offending_line.label
+ ]
+ end
+
+ LOG_SUBSCRIBER_FILE = ActiveRecord::LogSubscriber.method(:logger).source_location.first
+ RAILS_GEM_ROOT = File.expand_path("../../../..", LOG_SUBSCRIBER_FILE) + "/"
+ APP_CONFIG_ROOT = File.expand_path("..", __dir__) + "/"
+
+ def ignored_callstack(path)
+ path.start_with?(APP_CONFIG_ROOT, RAILS_GEM_ROOT, RbConfig::CONFIG["rubylibdir"])
+ end
+ end
+
+ unless Gitlab.rails5?
+ prepend(VerboseQueryLogs) unless Rails.env.production?
+ end
+ end
+end