summaryrefslogtreecommitdiff
path: root/lib/peek/views
diff options
context:
space:
mode:
Diffstat (limited to 'lib/peek/views')
-rw-r--r--lib/peek/views/active_record.rb25
-rw-r--r--lib/peek/views/detailed_view.rb53
-rw-r--r--lib/peek/views/gitaly.rb30
-rw-r--r--lib/peek/views/redis_detailed.rb (renamed from lib/peek/views/redis.rb)35
-rw-r--r--lib/peek/views/rugged.rb46
5 files changed, 140 insertions, 49 deletions
diff --git a/lib/peek/views/active_record.rb b/lib/peek/views/active_record.rb
new file mode 100644
index 00000000000..2d78818630d
--- /dev/null
+++ b/lib/peek/views/active_record.rb
@@ -0,0 +1,25 @@
+# frozen_string_literal: true
+
+module Peek
+ module Views
+ class ActiveRecord < DetailedView
+ private
+
+ def setup_subscribers
+ super
+
+ subscribe('sql.active_record') do |_, start, finish, _, data|
+ if Gitlab::SafeRequestStore.store[:peek_enabled]
+ unless data[:cached]
+ detail_store << {
+ duration: finish - start,
+ sql: data[:sql].strip,
+ backtrace: Gitlab::Profiler.clean_backtrace(caller)
+ }
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/lib/peek/views/detailed_view.rb b/lib/peek/views/detailed_view.rb
new file mode 100644
index 00000000000..f4ca1cb5075
--- /dev/null
+++ b/lib/peek/views/detailed_view.rb
@@ -0,0 +1,53 @@
+# frozen_string_literal: true
+
+module Peek
+ module Views
+ class DetailedView < View
+ def results
+ {
+ duration: formatted_duration,
+ calls: calls,
+ details: details
+ }
+ end
+
+ def detail_store
+ ::Gitlab::SafeRequestStore["#{key}_call_details"] ||= []
+ end
+
+ private
+
+ def duration
+ detail_store.map { |entry| entry[:duration] }.sum # rubocop:disable CodeReuse/ActiveRecord
+ end
+
+ def calls
+ detail_store.count
+ end
+
+ def call_details
+ detail_store
+ end
+
+ def format_call_details(call)
+ call.merge(duration: (call[:duration] * 1000).round(3))
+ end
+
+ def details
+ call_details
+ .sort { |a, b| b[:duration] <=> a[:duration] }
+ .map(&method(:format_call_details))
+ end
+
+ def formatted_duration
+ ms = duration * 1000
+
+ if ms >= 1000
+ "%.2fms" % ms
+ else
+ "%.0fms" % ms
+ end
+ end
+ end
+ end
+end
diff --git a/lib/peek/views/gitaly.rb b/lib/peek/views/gitaly.rb
index 30f95a10024..6ad6ddfd89d 100644
--- a/lib/peek/views/gitaly.rb
+++ b/lib/peek/views/gitaly.rb
@@ -2,7 +2,9 @@
module Peek
module Views
- class Gitaly < View
+ class Gitaly < DetailedView
+ private
+
def duration
::Gitlab::GitalyClient.query_time
end
@@ -11,36 +13,14 @@ module Peek
::Gitlab::GitalyClient.get_request_count
end
- def results
- {
- duration: formatted_duration,
- calls: calls,
- details: details
- }
- end
-
- private
-
- def details
+ def call_details
::Gitlab::GitalyClient.list_call_details
- .sort { |a, b| b[:duration] <=> a[:duration] }
- .map(&method(:format_call_details))
end
def format_call_details(call)
pretty_request = call[:request]&.reject { |k, v| v.blank? }.to_h.pretty_inspect
- call.merge(duration: (call[:duration] * 1000).round(3),
- request: pretty_request || {})
- end
-
- def formatted_duration
- ms = duration * 1000
- if ms >= 1000
- "%.2fms" % ms
- else
- "%.0fms" % ms
- end
+ super.merge(request: pretty_request || {})
end
def setup_subscribers
diff --git a/lib/peek/views/redis.rb b/lib/peek/views/redis_detailed.rb
index 73de8672fa4..f36f581d5e9 100644
--- a/lib/peek/views/redis.rb
+++ b/lib/peek/views/redis_detailed.rb
@@ -1,7 +1,6 @@
# frozen_string_literal: true
require 'redis'
-require 'peek-redis'
module Gitlab
module Peek
@@ -17,16 +16,21 @@ module Gitlab
private
def add_call_details(duration, args)
+ return unless peek_enabled?
# redis-rb passes an array (e.g. [:get, key])
return unless args.length == 1
detail_store << {
cmd: args.first,
duration: duration,
- backtrace: Gitlab::Profiler.clean_backtrace(caller)
+ backtrace: ::Gitlab::Profiler.clean_backtrace(caller)
}
end
+ def peek_enabled?
+ Gitlab::SafeRequestStore.store[:peek_enabled]
+ end
+
def detail_store
::Gitlab::SafeRequestStore['redis_call_details'] ||= []
end
@@ -36,26 +40,17 @@ end
module Peek
module Views
- module RedisDetailed
+ class RedisDetailed < DetailedView
REDACTED_MARKER = "<redacted>"
- def results
- super.merge(details: details)
- end
-
- def details
- detail_store
- .sort { |a, b| b[:duration] <=> a[:duration] }
- .map(&method(:format_call_details))
+ def key
+ 'redis'
end
- def detail_store
- ::Gitlab::SafeRequestStore['redis_call_details'] ||= []
- end
+ private
def format_call_details(call)
- call.merge(cmd: format_command(call[:cmd]),
- duration: (call[:duration] * 1000).round(3))
+ super.merge(cmd: format_command(call[:cmd]))
end
def format_command(cmd)
@@ -76,11 +71,3 @@ end
class Redis::Client
prepend Gitlab::Peek::RedisInstrumented
end
-
-module Peek
- module Views
- class Redis < View
- prepend Peek::Views::RedisDetailed
- end
- end
-end
diff --git a/lib/peek/views/rugged.rb b/lib/peek/views/rugged.rb
new file mode 100644
index 00000000000..18b3f422852
--- /dev/null
+++ b/lib/peek/views/rugged.rb
@@ -0,0 +1,46 @@
+# frozen_string_literal: true
+
+module Peek
+ module Views
+ class Rugged < DetailedView
+ def results
+ return {} unless calls > 0
+
+ super
+ end
+
+ private
+
+ def duration
+ ::Gitlab::RuggedInstrumentation.query_time
+ end
+
+ def calls
+ ::Gitlab::RuggedInstrumentation.query_count
+ end
+
+ def call_details
+ ::Gitlab::RuggedInstrumentation.list_call_details
+ end
+
+ def format_call_details(call)
+ super.merge(args: format_args(call[:args]))
+ end
+
+ def format_args(args)
+ args.map do |arg|
+ # ActiveSupport::JSON recursively calls as_json on all
+ # instance variables, and if that instance variable points to
+ # something that refers back to the same instance, we can wind
+ # up in an infinite loop. Currently this only seems to happen with
+ # Gitlab::Git::Repository and ::Repository.
+ if arg.instance_variables.present?
+ arg.to_s
+ else
+ arg
+ end
+ end
+ end
+ end
+ end
+end