diff options
author | Greg Myers <gmyers@gitlab.com> | 2019-08-27 15:51:04 +0000 |
---|---|---|
committer | Greg Myers <gmyers@gitlab.com> | 2019-08-27 15:51:04 +0000 |
commit | 51a0166af2c532aa7a0d04bb503b43499be97033 (patch) | |
tree | 9ebd74e8518d573fe4860763ddb41146dab1f453 /lib/peek | |
parent | f706677357fcdf9cd870d098379788543ea71058 (diff) | |
parent | 27c222d16ccb666878c7ac558f484efa56f77f37 (diff) | |
download | gitlab-ce-51a0166af2c532aa7a0d04bb503b43499be97033.tar.gz |
Merge branch 'master' into 'api-curl-quote-url-docs'api-curl-quote-url-docs
# Conflicts:
# doc/api/events.md
Diffstat (limited to 'lib/peek')
-rw-r--r-- | lib/peek/rblineprof/custom_controller_helpers.rb | 124 | ||||
-rw-r--r-- | lib/peek/views/active_record.rb | 25 | ||||
-rw-r--r-- | lib/peek/views/detailed_view.rb | 53 | ||||
-rw-r--r-- | lib/peek/views/gitaly.rb | 30 | ||||
-rw-r--r-- | lib/peek/views/redis_detailed.rb | 73 | ||||
-rw-r--r-- | lib/peek/views/rugged.rb | 46 |
6 files changed, 202 insertions, 149 deletions
diff --git a/lib/peek/rblineprof/custom_controller_helpers.rb b/lib/peek/rblineprof/custom_controller_helpers.rb deleted file mode 100644 index 581cc6a37b4..00000000000 --- a/lib/peek/rblineprof/custom_controller_helpers.rb +++ /dev/null @@ -1,124 +0,0 @@ -# frozen_string_literal: true - -module Peek - module Rblineprof - module CustomControllerHelpers - extend ActiveSupport::Concern - - # This will become useless once https://github.com/peek/peek-rblineprof/pull/5 - # is merged - def pygmentize(file_name, code, lexer = nil) - if lexer.present? - Gitlab::Highlight.highlight(file_name, code) - else - "<pre>#{Rack::Utils.escape_html(code)}</pre>" - end - end - - # rubocop:disable all - def inject_rblineprof - ret = nil - profile = lineprof(rblineprof_profiler_regex) do - ret = yield - end - - if response.content_type =~ %r|text/html| - sort = params[:lineprofiler_sort] - mode = params[:lineprofiler_mode] || 'cpu' - min = (params[:lineprofiler_min] || 5).to_i * 1000 - summary = params[:lineprofiler_summary] - - # Sort each file by the longest calculated time - per_file = profile.map do |file, lines| - total, child, excl, total_cpu, child_cpu, excl_cpu = lines[0] - - wall = summary == 'exclusive' ? excl : total - cpu = summary == 'exclusive' ? excl_cpu : total_cpu - idle = summary == 'exclusive' ? (excl - excl_cpu) : (total - total_cpu) - - [ - file, lines, - wall, cpu, idle, - sort == 'idle' ? idle : sort == 'cpu' ? cpu : wall - ] - end.sort_by{ |a,b,c,d,e,f| -f } - - output = ["<div class='modal-dialog modal-xl'><div class='modal-content'>"] - output << "<div class='modal-header'>" - output << "<h4>Line profiling: #{human_description(params[:lineprofiler])}</h4>" - output << "<button class='close' type='button' data-dismiss='modal' aria-label='close'><span aria-hidden='true'>×</span></button>" - output << "</div>" - output << "<div class='modal-body'>" - - per_file.each do |file_name, lines, file_wall, file_cpu, file_idle, file_sort| - output << "<div class='peek-rblineprof-file'><div class='heading'>" - - show_src = file_sort > min - tmpl = show_src ? "<a href='#' class='js-lineprof-file'>%s</a>" : "%s" - - if mode == 'cpu' - output << sprintf("<span class='duration'>% 8.1fms + % 8.1fms</span> #{tmpl}", file_cpu / 1000.0, file_idle / 1000.0, file_name.sub(Rails.root.to_s + '/', '')) - else - output << sprintf("<span class='duration'>% 8.1fms</span> #{tmpl}", file_wall/1000.0, file_name.sub(Rails.root.to_s + '/', '')) - end - - output << "</div>" # .heading - - next unless show_src - - output << "<div class='data'>" - code = [] - times = [] - File.readlines(file_name).each_with_index do |line, i| - code << line - wall, cpu, calls = lines[i + 1] - - if calls && calls > 0 - if mode == 'cpu' - idle = wall - cpu - times << sprintf("% 8.1fms + % 8.1fms (% 5d)", cpu / 1000.0, idle / 1000.0, calls) - else - times << sprintf("% 8.1fms (% 5d)", wall / 1000.0, calls) - end - else - times << ' ' - end - end - output << "<pre class='duration'>#{times.join("\n")}</pre>" - # The following line was changed from - # https://github.com/peek/peek-rblineprof/blob/8d3b7a283a27de2f40abda45974516693d882258/lib/peek/rblineprof/controller_helpers.rb#L125 - # This will become useless once https://github.com/peek/peek-rblineprof/pull/16 - # is merged and is implemented. - output << "<pre class='code highlight white'>#{pygmentize(file_name, code.join, 'ruby')}</pre>" - output << "</div></div>" # .data then .peek-rblineprof-file - end - - output << "</div></div></div>" - - response.body += "<div class='modal' id='modal-peek-line-profile' tabindex=-1>#{output.join}</div>".html_safe - end - - ret - end - - private - - def human_description(lineprofiler_param) - case lineprofiler_param - when 'app' - 'app/ & lib/' - when 'views' - 'app/view/' - when 'gems' - 'vendor/gems' - when 'all' - 'everything in Rails.root' - when 'stdlib' - 'everything in the Ruby standard library' - else - 'app/, config/, lib/, vendor/ & plugin/' - end - end - end - end -end 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_detailed.rb b/lib/peek/views/redis_detailed.rb new file mode 100644 index 00000000000..f36f581d5e9 --- /dev/null +++ b/lib/peek/views/redis_detailed.rb @@ -0,0 +1,73 @@ +# frozen_string_literal: true + +require 'redis' + +module Gitlab + module Peek + module RedisInstrumented + def call(*args, &block) + start = Time.now + super(*args, &block) + ensure + duration = (Time.now - start) + add_call_details(duration, args) + end + + 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) + } + end + + def peek_enabled? + Gitlab::SafeRequestStore.store[:peek_enabled] + end + + def detail_store + ::Gitlab::SafeRequestStore['redis_call_details'] ||= [] + end + end + end +end + +module Peek + module Views + class RedisDetailed < DetailedView + REDACTED_MARKER = "<redacted>" + + def key + 'redis' + end + + private + + def format_call_details(call) + super.merge(cmd: format_command(call[:cmd])) + end + + def format_command(cmd) + if cmd.length >= 2 && cmd.first =~ /^auth$/i + cmd[-1] = REDACTED_MARKER + # Scrub out the value of the SET calls to avoid binary + # data or large data from spilling into the view + elsif cmd.length >= 3 && cmd.first =~ /set/i + cmd[2..-1] = REDACTED_MARKER + end + + cmd.join(' ') + end + end + end +end + +class Redis::Client + prepend Gitlab::Peek::RedisInstrumented +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 |