Commit dcddbf83 authored by Igor Drozdov's avatar Igor Drozdov

Merge branch '349293-deprecate-sherlock' into 'master'

Deprecate Sherlock tool

See merge request gitlab-org/gitlab!77345
parents 9aa4267b 2697d2d0
......@@ -325,7 +325,6 @@ Performance/Sum:
- 'lib/container_registry/tag.rb'
- 'lib/gitlab/ci/reports/test_suite_comparer.rb'
- 'lib/gitlab/diff/file.rb'
- 'lib/gitlab/sherlock/transaction.rb'
- 'lib/gitlab/usage_data.rb'
- 'lib/peek/views/detailed_view.rb'
- 'spec/models/namespace/root_storage_statistics_spec.rb'
......
......@@ -26,7 +26,6 @@ Database/MultipleDatabases:
- lib/gitlab/import_export/group/relation_tree_restorer.rb
- lib/gitlab/legacy_github_import/importer.rb
- lib/gitlab/seeder.rb
- lib/gitlab/sherlock/query.rb
- lib/system_check/orphans/repository_check.rb
- spec/db/schema_spec.rb
- spec/initializers/database_config_spec.rb
......
......@@ -76,7 +76,6 @@ Rails/TimeZone:
- lib/gitlab/prometheus/queries/additional_metrics_environment_query.rb
- lib/gitlab/prometheus/queries/matched_metric_query.rb
- lib/gitlab/prometheus_client.rb
- lib/gitlab/sherlock/transaction.rb
- lib/gitlab/task_helpers.rb
- lib/gitlab/x509/tag.rb
- lib/grafana/time_window.rb
......@@ -141,7 +140,6 @@ Rails/TimeZone:
- spec/lib/gitlab/prometheus/queries/additional_metrics_deployment_query_spec.rb
- spec/lib/gitlab/prometheus/queries/deployment_query_spec.rb
- spec/lib/gitlab/prometheus/queries/validate_query_spec.rb
- spec/lib/gitlab/sherlock/transaction_spec.rb
- spec/lib/gitlab/sidekiq_logging/json_formatter_spec.rb
- spec/lib/gitlab/sidekiq_middleware/duplicate_jobs/strategies/until_executing_spec.rb
- spec/lib/gitlab/updated_notes_paginator_spec.rb
......
......@@ -28,7 +28,6 @@
@import './pages/service_desk';
@import './pages/settings';
@import './pages/settings_ci_cd';
@import './pages/sherlock';
@import './pages/storage_quota';
@import './pages/tree';
@import './pages/users';
table .sherlock-code {
max-width: 700px;
}
.sherlock-code {
pre {
word-wrap: normal;
code {
white-space: pre;
}
}
}
.sherlock-line-samples-table {
thead th,
tbody td {
font-size: 13px !important;
text-align: right;
padding: 0 10px !important;
}
.slow {
color: $red-500;
font-weight: $gl-font-weight-bold;
}
}
.sherlock-file-sample pre {
padding-top: 28px !important;
}
# frozen_string_literal: true
module Sherlock
class ApplicationController < ::ApplicationController
before_action :find_transaction
def find_transaction
if params[:transaction_id]
@transaction = Gitlab::Sherlock.collection
.find_transaction(params[:transaction_id])
end
end
end
end
# frozen_string_literal: true
module Sherlock
class FileSamplesController < Sherlock::ApplicationController
def show
@file_sample = @transaction.find_file_sample(params[:id])
end
end
end
# frozen_string_literal: true
module Sherlock
class QueriesController < Sherlock::ApplicationController
def show
@query = @transaction.find_query(params[:id])
end
end
end
# frozen_string_literal: true
module Sherlock
class TransactionsController < Sherlock::ApplicationController
def index
@transactions = Gitlab::Sherlock.collection.newest_first
end
def show
@transaction = Gitlab::Sherlock.collection.find_transaction(params[:id])
render_404 unless @transaction
end
def destroy_all
Gitlab::Sherlock.collection.clear
redirect_back_or_default(options: { status: :found })
end
end
end
......@@ -189,15 +189,6 @@ module Nav
end
end
# rubocop: enable Cop/UserAdmin
if Gitlab::Sherlock.enabled?
builder.add_secondary_menu_item(
id: 'sherlock',
title: _('Sherlock Transactions'),
icon: 'admin',
href: sherlock_transactions_path
)
end
end
def projects_menu_item_attrs
......
......@@ -46,10 +46,6 @@ module NavHelper
class_names
end
def has_extra_nav_icons?
Gitlab::Sherlock.enabled? || current_user.admin?
end
def page_has_markdown?
current_path?('merge_requests#show') ||
current_path?('projects/merge_requests/conflicts#show') ||
......
- page_title t('sherlock.title'), t('sherlock.transaction'),
t('sherlock.file_sample')
- header_title t('sherlock.title'), sherlock_transactions_path
.row-content-block
.float-right
= link_to(sherlock_transaction_path(@transaction), class: 'btn gl-button') do
= sprite_icon('arrow-left')
= t('sherlock.transaction')
.oneline
= t('sherlock.file_sample')
= @file_sample.id
.gl-mt-3
%p
%span.light
#{t('sherlock.time')}:
%strong
= @file_sample.duration.round(2)
= t('sherlock.milliseconds')
%p
%span.light
#{t('sherlock.events')}:
%strong
= @file_sample.events
%article.file-holder
.js-file-title.file-title
= sprite_icon("doc-text")
%strong
= @file_sample.file
.code.file-content.js-syntax-highlight
.line-numbers
%table.sherlock-line-samples-table.gl-mb-0
%thead
%tr
%th= t('sherlock.line_capitalized')
%th= t('sherlock.events')
%th= t('sherlock.time')
%th= t('sherlock.percent')
%tbody
- @file_sample.line_samples.each_with_index do |sample, index|
%tr{ class: sample.majority_of?(@file_sample.duration) ? 'slow' : '' }
%td= index + 1
%td= sample.events
%td
= sample.duration.round(2)
= t('sherlock.milliseconds')
%td
= sample.percentage_of(@file_sample.duration).round
= t('sherlock.percent')
.sherlock-file-sample
= highlight(@file_sample.file, @file_sample.source)
.gl-mt-3
.card
.card-header
%strong
= t('sherlock.application_backtrace')
%ul.content-list
- @query.application_backtrace.each do |location|
%li
%strong
- if defined?(BetterErrors)
= link_to(location.path, BetterErrors.editor.url(location.path, location.line))
- else
= location.path
%small.light
= t('sherlock.line')
= location.line
.card
.card-header
%strong
= t('sherlock.full_backtrace')
%ul.content-list
- @query.backtrace.each do |location|
%li
- if location.application?
%strong= location.path
- else
= location.path
%small.light
= t('sherlock.line')
= location.line
.gl-mt-3
.card
.card-header
%strong
= t('sherlock.general')
%ul.content-list
%li
%span.light
#{t('sherlock.time')}:
%strong
= @query.duration.round(4)
= t('sherlock.milliseconds')
%li
- frame = @query.last_application_frame
%span.light
#{t('sherlock.origin')}:
%strong
- if defined?(BetterErrors)
= link_to(frame.path, BetterErrors.editor.url(frame.path, frame.line))
- else
= frame.path
%small.light
= t('sherlock.line')
= frame.line
.card
.card-header
.float-right
%button.js-clipboard-trigger.gl-button.btn.btn-default.btn-sm{ title: t('sherlock.copy_to_clipboard'), type: :button }
= sprite_icon('copy-to-clipboard')
%pre.hidden
= @query.formatted_query
%strong
= t('sherlock.query')
%ul.content-list
%li
.code.js-syntax-highlight.sherlock-code
:preserve
#{highlight("#{@query.id}.sql", @query.formatted_query, language: 'sql')}
.card
.card-header
.float-right
%button.js-clipboard-trigger.gl-button.btn.btn-default.btn-sm{ title: t('sherlock.copy_to_clipboard'), type: :button }
= sprite_icon('copy-to-clipboard')
%pre.hidden
= @query.explain
%strong
= t('sherlock.query_plan')
%ul.content-list
%li
.code.js-syntax-highlight.sherlock-code
%pre
%code= @query.explain
- page_title t('sherlock.title'), t('sherlock.transaction'), t('sherlock.query')
- header_title t('sherlock.title'), sherlock_transactions_path
%ul.nav-links.nav.nav-tabs
%li.active
%a{ href: "#tab-general", data: { toggle: "tab" } }
= t('sherlock.general')
%li
%a{ href: "#tab-backtrace", data: { toggle: "tab" } }
= t('sherlock.backtrace')
.row-content-block
.float-right
= link_to(sherlock_transaction_path(@transaction), class: 'btn gl-button btn-default') do
= sprite_icon('arrow-left')
= t('sherlock.transaction')
.oneline
= t('sherlock.query')
= @query.id
.tab-content
.tab-pane.active#tab-general
= render(partial: 'general')
.tab-pane#tab-backtrace
= render(partial: 'backtrace')
- if @transaction.file_samples.empty?
.nothing-here-block
= t('sherlock.no_file_samples')
- else
.table-holder
%table.table
%thead
%tr
%th= t('sherlock.time_inclusive')
%th= t('sherlock.count')
%th= t('sherlock.path')
%th
%tbody
- @transaction.sorted_file_samples.each do |sample|
%tr
%td
= sample.duration.round(2)
= t('sherlock.milliseconds')
%td= @transaction.view_counts.fetch(sample.file, 1)
%td= sample.relative_path
%td
= link_to(t('sherlock.view'),
sherlock_transaction_file_sample_path(@transaction, sample),
class: 'gl-button btn btn-sm')
.gl-mt-3
.card
.card-header
%strong
= t('sherlock.general')
%ul.content-list
%li
%span.light
#{t('sherlock.id')}:
%strong
= @transaction.id
%li
%span.light
#{t('sherlock.type')}:
%strong
= @transaction.type
%li
%span.light
#{t('sherlock.path')}:
%strong
= @transaction.path
%li
%span.light
#{t('sherlock.time')}:
%strong
= @transaction.duration.round(2)
= t('sherlock.seconds')
%li
%span.light
#{t('sherlock.query_time')}
%strong
= @transaction.query_duration.round(2)
= t('sherlock.seconds')
%li
%span.light
#{t('sherlock.finished_at')}:
%strong
= time_ago_with_tooltip @transaction.finished_at
- if @transaction.queries.empty?
.nothing-here-block
= t('sherlock.no_queries')
- else
.table-holder
%table.table#sherlock-queries
%thead
%tr
%th= t('sherlock.time')
%th= t('sherlock.query')
%th
%tbody
- @transaction.sorted_queries.each do |query|
%tr
%td
= query.duration.round(2)
= t('sherlock.milliseconds')
%td
.code.js-syntax-highlight.sherlock-code
= highlight("#{query.id}.sql", query.formatted_query, language: 'sql')
%td
= link_to(t('sherlock.view'),
sherlock_transaction_query_path(@transaction, query),
class: 'gl-button btn btn-sm')
- page_title t('sherlock.title')
- header_title t('sherlock.title'), sherlock_transactions_path
.row-content-block
.float-right
= link_to(destroy_all_sherlock_transactions_path,
class: 'gl-button btn btn-danger',
method: :delete) do
= sprite_icon('remove')
= t('sherlock.delete_all_transactions')
.oneline= t('sherlock.introduction')
- if @transactions.empty?
.nothing-here-block= t('sherlock.no_transactions')
- else
.table-holder
%table.table
%thead
%tr
%th= t('sherlock.type')
%th= t('sherlock.path')
%th= t('sherlock.time')
%th= t('sherlock.queries')
%th= t('sherlock.finished_at')
%th
%tbody
- @transactions.each do |trans|
%tr
%td= trans.type
%td
%span{ title: trans.path }
= truncate(trans.path, length: 70)
%td
= trans.duration.round(2)
= t('sherlock.seconds')
%td= trans.queries.length
%td
= time_ago_with_tooltip trans.finished_at
%td
= link_to(sherlock_transaction_path(trans), class: 'gl-button btn btn-sm') do
= t('sherlock.view')
- page_title t('sherlock.title'), t('sherlock.transaction')
- header_title t('sherlock.title'), sherlock_transactions_path
%ul.nav-links.nav.nav-tabs
%li.active
%a{ href: "#tab-general", data: { toggle: "tab" } }
= t('sherlock.general')
%li
%a{ href: "#tab-queries", data: { toggle: "tab" } }
= t('sherlock.queries')
= gl_badge_tag @transaction.queries.length.to_s
%li
%a{ href: "#tab-file-samples", data: { toggle: "tab" } }
= t('sherlock.file_samples')
= gl_badge_tag @transaction.file_samples.length.to_s
.row-content-block
.float-right
= link_to(sherlock_transactions_path, class: 'gl-button btn') do
= sprite_icon('arrow-left', css_class: 'gl-mr-3')
= t('sherlock.all_transactions')
.oneline
= t('sherlock.transaction')
= @transaction.id
.tab-content
.tab-pane.active#tab-general
= render(partial: 'general')
.tab-pane#tab-queries
= render(partial: 'queries')
.tab-pane#tab-file-samples
= render(partial: 'file_samples')
# frozen_string_literal: true
if Gitlab::Sherlock.enabled?
Rails.application.configure do |config|
config.middleware.use(Gitlab::Sherlock::Middleware)
end
end
en:
sherlock:
title: Sherlock
delete_all_transactions: Delete All Transactions
introduction: >
Below is a list of all transactions recorded by Sherlock. Requests to
Sherlock's own routes are ignored.
no_transactions: No transactions to show
no_queries: No queries to show
no_file_samples: No file samples to show
all_transactions: All Transactions
transaction: Transaction
query: Query
file_sample: File Sample
type: Type
path: Path
time: Time
queries: Queries
finished_at: Finished at
ago: ago
view: View
seconds: seconds
milliseconds: ms
general: General
id: ID
time_inclusive: Time (inclusive)
backtrace: Backtrace
application_backtrace: Application Backtrace
full_backtrace: Full Backtrace
origin: Origin
line: line
line_capitalized: Line
copy_to_clipboard: Copy
query_plan: Query Plan
events: Events
percent: '%'
count: Count
query_time: Query Time
......@@ -20,7 +20,6 @@ Rails.application.routes.draw do
get 'favicon.png', to: favicon_redirect
get 'favicon.ico', to: favicon_redirect
draw :sherlock
draw :development
use_doorkeeper do
......
# frozen_string_literal: true
if Gitlab::Sherlock.enabled?
namespace :sherlock do
resources :transactions, only: [:index, :show] do
resources :queries, only: [:show]
resources :file_samples, only: [:show]
collection do
delete :destroy_all
end
end
end
end
......@@ -4,11 +4,6 @@ module EE
module NavHelper
extend ::Gitlab::Utils::Override
override :has_extra_nav_icons?
def has_extra_nav_icons?
super || can?(current_user, :read_operations_dashboard)
end
override :page_has_markdown?
def page_has_markdown?
super || current_path?('epics#show')
......
# frozen_string_literal: true
require 'securerandom'
module Gitlab
module Sherlock
@collection = Collection.new
class << self
attr_reader :collection
end
def self.enabled?
Rails.env.development? && !!ENV['ENABLE_SHERLOCK']
end
def self.enable_line_profiler?
RUBY_ENGINE == 'ruby'
end
end
end
# frozen_string_literal: true
module Gitlab
module Sherlock
# A collection of transactions recorded by Sherlock.
#
# Method calls for this class are synchronized using a mutex to allow
# sharing of a single Collection instance between threads (e.g. when using
# Puma as a webserver).
class Collection
include Enumerable
def initialize
@transactions = []
@mutex = Mutex.new
end
def add(transaction)
synchronize { @transactions << transaction }
end
alias_method :<<, :add
def each(&block)
synchronize { @transactions.each(&block) }
end
def clear
synchronize { @transactions.clear }
end
def empty?
synchronize { @transactions.empty? }
end
def find_transaction(id)
find { |trans| trans.id == id }
end
def newest_first
sort { |a, b| b.finished_at <=> a.finished_at }
end
private
def synchronize(&block)
@mutex.synchronize(&block)
end
end
end
end
# frozen_string_literal: true
module Gitlab
module Sherlock
class FileSample
attr_reader :id, :file, :line_samples, :events, :duration
# file - The full path to the file this sample belongs to.
# line_samples - An array of LineSample objects.
# duration - The total execution time in milliseconds.
# events - The total amount of events.
def initialize(file, line_samples, duration, events)
@id = SecureRandom.uuid
@file = file
@line_samples = line_samples
@duration = duration
@events = events
end
def relative_path
@relative_path ||= @file.gsub(%r{^#{Rails.root}/?}, '')
end
def to_param
@id
end
def source
@source ||= File.read(@file)
end
end
end
end
# frozen_string_literal: true
module Gitlab
module Sherlock
# Class for profiling code on a per line basis.
#
# The LineProfiler class can be used to profile code on per line basis
# without littering your code with Ruby implementation specific profiling
# methods.
#
# This profiler only includes samples taking longer than a given threshold
# and those that occur in the actual application (e.g. files from Gems are
# ignored).
class LineProfiler
# The minimum amount of time that has to be spent in a file for it to be
# included in a list of samples.
MINIMUM_DURATION = 10.0
# Profiles the given block.
#
# Example:
#
# profiler = LineProfiler.new
#
# retval, samples = profiler.profile do
# "cats are amazing"
# end
#
# retval # => "cats are amazing"
# samples # => [#<Gitlab::Sherlock::FileSample ...>, ...]
#
# Returns an Array containing the block's return value and an Array of
# FileSample objects.
def profile(&block)
if mri?
profile_mri(&block)
else
raise NotImplementedError,
'Line profiling is not supported on this platform'
end
end
# Profiles the given block using rblineprof (MRI only).
def profile_mri
require 'rblineprof'
retval = nil
samples = lineprof(/^#{Rails.root}/) { retval = yield }
file_samples = aggregate_rblineprof(samples)
[retval, file_samples]
end
# Returns an Array of file samples based on the output of rblineprof.
#
# lineprof_stats - A Hash containing rblineprof statistics on a per file
# basis.
#
# Returns an Array of FileSample objects.
def aggregate_rblineprof(lineprof_stats)
samples = []
lineprof_stats.each do |(file, stats)|
source_lines = File.read(file).each_line.to_a
line_samples = []
total_duration = microsec_to_millisec(stats[0][0])
total_events = stats[0][2]
next if total_duration <= MINIMUM_DURATION
stats[1..].each_with_index do |data, index|
next unless source_lines[index]
duration = microsec_to_millisec(data[0])
events = data[2]
line_samples << LineSample.new(duration, events)
end
samples << FileSample
.new(file, line_samples, total_duration, total_events)
end
samples
end
private
def microsec_to_millisec(microsec)
microsec / 1000.0
end
def mri?
RUBY_ENGINE == 'ruby'
end
end
end
end
# frozen_string_literal: true
module Gitlab
module Sherlock
class LineSample
attr_reader :duration, :events
# duration - The execution time in milliseconds.
# events - The amount of events.
def initialize(duration, events)
@duration = duration
@events = events
end
# Returns the sample duration percentage relative to the given duration.
#
# Example:
#
# sample.duration # => 150
# sample.percentage_of(1500) # => 10.0
#
# total_duration - The total duration to compare with.
#
# Returns a float
def percentage_of(total_duration)
(duration.to_f / total_duration) * 100.0
end
# Returns true if the current sample takes up the majority of the given
# duration.
#
# total_duration - The total duration to compare with.
def majority_of?(total_duration)
percentage_of(total_duration) >= 30
end
end
end
end
# frozen_string_literal: true
module Gitlab
module Sherlock
class Location
attr_reader :path, :line
SHERLOCK_DIR = File.dirname(__FILE__)
# Creates a new Location from a `Thread::Backtrace::Location`.
def self.from_ruby_location(location)
new(location.path, location.lineno)
end
# path - The full path of the frame as a String.
# line - The line number of the frame as a Fixnum.
def initialize(path, line)
@path = path
@line = line
end
# Returns true if the current frame originated from the application.
def application?
@path.start_with?(Rails.root.to_s) && !path.start_with?(SHERLOCK_DIR)
end
end
end
end
# frozen_string_literal: true
module Gitlab
module Sherlock
# Rack middleware used for tracking request metrics.
class Middleware
CONTENT_TYPES = %r{text/html|application/json}i.freeze
IGNORE_PATHS = %r{^/sherlock}.freeze
def initialize(app)
@app = app
end
# env - A Hash containing Rack environment details.
def call(env)
if instrument?(env)
call_with_instrumentation(env)
else
@app.call(env)
end
end
def call_with_instrumentation(env)
trans = transaction_from_env(env)
retval = trans.run { @app.call(env) }
Sherlock.collection.add(trans)
retval
end
def instrument?(env)
!!(env['HTTP_ACCEPT'] =~ CONTENT_TYPES &&
env['REQUEST_URI'] !~ IGNORE_PATHS)
end
def transaction_from_env(env)
Transaction.new(env['REQUEST_METHOD'], env['REQUEST_URI'])
end
end
end
end
# frozen_string_literal: true
module Gitlab
module Sherlock
class Query
attr_reader :id, :query, :started_at, :finished_at, :backtrace
# SQL identifiers that should be prefixed with newlines.
PREFIX_NEWLINE = %r{
\s+(FROM
|(LEFT|RIGHT)?INNER\s+JOIN
|(LEFT|RIGHT)?OUTER\s+JOIN
|WHERE
|AND
|GROUP\s+BY
|ORDER\s+BY
|LIMIT
|OFFSET)\s+}ix.freeze # Vim indent breaks when this is on a newline :<
# Creates a new Query using a String and a separate Array of bindings.
#
# query - A String containing a SQL query, optionally with numeric
# placeholders (`$1`, `$2`, etc).
#
# bindings - An Array of ActiveRecord columns and their values.
# started_at - The start time of the query as a Time-like object.
# finished_at - The completion time of the query as a Time-like object.
#
# Returns a new Query object.
def self.new_with_bindings(query, bindings, started_at, finished_at)
bindings.each_with_index do |(_, value), index|
quoted_value = ActiveRecord::Base.connection.quote(value)
query = query.gsub("$#{index + 1}", quoted_value)
end
new(query, started_at, finished_at)
end
# query - The SQL query as a String (without placeholders).
# started_at - The start time of the query as a Time-like object.
# finished_at - The completion time of the query as a Time-like object.
def initialize(query, started_at, finished_at)
@id = SecureRandom.uuid
@query = query
@started_at = started_at
@finished_at = finished_at
@backtrace = caller_locations.map do |loc|
Location.from_ruby_location(loc)
end
unless @query.end_with?(';')
@query = "#{@query};"
end
end
# Returns the query duration in milliseconds.
def duration
@duration ||= (@finished_at - @started_at) * 1000.0
end
def to_param
@id
end
# Returns a human readable version of the query.
def formatted_query
@formatted_query ||= format_sql(@query)
end
# Returns the last application frame of the backtrace.
def last_application_frame
@last_application_frame ||= @backtrace.find(&:application?)
end
# Returns an Array of application frames (excluding Gems and the likes).
def application_backtrace
@application_backtrace ||= @backtrace.select(&:application?)
end
# Returns the query plan as a String.
def explain
unless @explain
ActiveRecord::Base.connection.transaction do
@explain = raw_explain(@query).values.flatten.join("\n")
# Roll back any queries that mutate data so we don't mess up
# anything when running explain on an INSERT, UPDATE, DELETE, etc.
raise ActiveRecord::Rollback
end
end
@explain
end
private
def raw_explain(query)
explain = "EXPLAIN ANALYZE #{query};"
ActiveRecord::Base.connection.execute(explain)
end
def format_sql(query)
query.each_line
.map { |line| line.strip }
.join("\n")
.gsub(PREFIX_NEWLINE) { "\n#{Regexp.last_match(1)} " }
end
end
end
end
# frozen_string_literal: true
module Gitlab
module Sherlock
class Transaction
attr_reader :id, :type, :path, :queries, :file_samples, :started_at,
:finished_at, :view_counts
# type - The type of transaction (e.g. "GET", "POST", etc)
# path - The path of the transaction (e.g. the HTTP request path)
def initialize(type, path)
@id = SecureRandom.uuid
@type = type
@path = path
@queries = []
@file_samples = []
@started_at = nil
@finished_at = nil
@thread = Thread.current
@view_counts = Hash.new(0)
end
# Runs the transaction and returns the block's return value.
def run
@started_at = Time.now
retval = with_subscriptions do
profile_lines { yield }
end
@finished_at = Time.now
retval
end
# Returns the duration in seconds.
def duration
@duration ||= started_at && finished_at ? finished_at - started_at : 0
end
# Returns the total query duration in seconds.
def query_duration
@query_duration ||= @queries.map { |q| q.duration }.inject(:+) / 1000.0
end
def to_param
@id
end
# Returns the queries sorted in descending order by their durations.
def sorted_queries
@queries.sort { |a, b| b.duration <=> a.duration }
end
# Returns the file samples sorted in descending order by their durations.
def sorted_file_samples
@file_samples.sort { |a, b| b.duration <=> a.duration }
end
# Finds a query by the given ID.
#
# id - The query ID as a String.
#
# Returns a Query object if one could be found, nil otherwise.
def find_query(id)
@queries.find { |query| query.id == id }
end
# Finds a file sample by the given ID.
#
# id - The query ID as a String.
#
# Returns a FileSample object if one could be found, nil otherwise.
def find_file_sample(id)
@file_samples.find { |sample| sample.id == id }
end
def profile_lines
retval = nil
if Sherlock.enable_line_profiler?
retval, @file_samples = LineProfiler.new.profile { yield }
else
retval = yield
end
retval
end
def subscribe_to_active_record
ActiveSupport::Notifications.subscribe('sql.active_record') do |_, start, finish, _, data|
next unless same_thread?
unless data.fetch(:cached, data[:name] == 'CACHE')
track_query(data[:sql].strip, data[:binds], start, finish)
end
end
end
def subscribe_to_action_view
regex = /render_(template|partial)\.action_view/
ActiveSupport::Notifications.subscribe(regex) do |_, start, finish, _, data|
next unless same_thread?
track_view(data[:identifier])
end
end
private
def track_query(query, bindings, start, finish)
@queries << Query.new_with_bindings(query, bindings, start, finish)
end
def track_view(path)
@view_counts[path] += 1
end
def with_subscriptions
ar_subscriber = subscribe_to_active_record
av_subscriber = subscribe_to_action_view
retval = yield
ActiveSupport::Notifications.unsubscribe(ar_subscriber)
ActiveSupport::Notifications.unsubscribe(av_subscriber)
retval
end
# In case somebody uses a multi-threaded server locally (e.g. Puma) we
# _only_ want to track notifications that originate from the transaction
# thread.
def same_thread?
Thread.current == @thread
end
end
end
end
......@@ -32859,9 +32859,6 @@ msgstr ""
msgid "SharedRunnersMinutesSettings|Reset used pipeline minutes"
msgstr ""
msgid "Sherlock Transactions"
msgstr ""
msgid "Shimo|Go to Shimo Workspace"
msgstr ""
......
......@@ -20,7 +20,6 @@ RSpec.describe Nav::TopNavHelper do
let(:current_group) { nil }
let(:with_current_settings_admin_mode) { false }
let(:with_header_link_admin_mode) { false }
let(:with_sherlock_enabled) { false }
let(:with_projects) { false }
let(:with_groups) { false }
let(:with_milestones) { false }
......@@ -34,7 +33,6 @@ RSpec.describe Nav::TopNavHelper do
before do
allow(Gitlab::CurrentSettings).to receive(:admin_mode) { with_current_settings_admin_mode }
allow(helper).to receive(:header_link?).with(:admin_mode) { with_header_link_admin_mode }
allow(Gitlab::Sherlock).to receive(:enabled?) { with_sherlock_enabled }
# Defaulting all `dashboard_nav_link?` calls to false ensures the EE-specific behavior
# is not enabled in this CE spec
......@@ -434,27 +432,6 @@ RSpec.describe Nav::TopNavHelper do
expect(subject[:shortcuts]).to eq([expected_shortcuts])
end
end
context 'when sherlock is enabled' do
let(:with_sherlock_enabled) { true }
before do
# Note: We have to mock the sherlock route because the route is conditional on
# sherlock being enabled, but it parsed at Rails load time and can't be overridden
# in a spec.
allow(helper).to receive(:sherlock_transactions_path) { '/fake_sherlock_path' }
end
it 'has sherlock as last :secondary item' do
expected_sherlock_item = ::Gitlab::Nav::TopNavMenuItem.build(
id: 'sherlock',
title: 'Sherlock Transactions',
icon: 'admin',
href: '/fake_sherlock_path'
)
expect(subject[:secondary].last).to eq(expected_sherlock_item)
end
end
end
context 'when current_user is admin' do
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Sherlock::Collection do
let(:collection) { described_class.new }
let(:transaction) do
Gitlab::Sherlock::Transaction.new('POST', '/cat_pictures')
end
describe '#add' do
it 'adds a new transaction' do
collection.add(transaction)
expect(collection).not_to be_empty
end
it 'is aliased as <<' do
collection << transaction
expect(collection).not_to be_empty
end
end
describe '#each' do
it 'iterates over every transaction' do
collection.add(transaction)
expect { |b| collection.each(&b) }.to yield_with_args(transaction)
end
end
describe '#clear' do
it 'removes all transactions' do
collection.add(transaction)
collection.clear
expect(collection).to be_empty
end
end
describe '#empty?' do
it 'returns true for an empty collection' do
expect(collection).to be_empty
end
it 'returns false for a collection with a transaction' do
collection.add(transaction)
expect(collection).not_to be_empty
end
end
describe '#find_transaction' do
it 'returns the transaction for the given ID' do
collection.add(transaction)
expect(collection.find_transaction(transaction.id)).to eq(transaction)
end
it 'returns nil when no transaction could be found' do
collection.add(transaction)
expect(collection.find_transaction('cats')).to be_nil
end
end
describe '#newest_first' do
it 'returns transactions sorted from new to old' do
trans1 = Gitlab::Sherlock::Transaction.new('POST', '/cat_pictures')
trans2 = Gitlab::Sherlock::Transaction.new('POST', '/more_cat_pictures')
allow(trans1).to receive(:finished_at).and_return(Time.utc(2015, 1, 1))
allow(trans2).to receive(:finished_at).and_return(Time.utc(2015, 1, 2))
collection.add(trans1)
collection.add(trans2)
expect(collection.newest_first).to eq([trans2, trans1])
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Sherlock::FileSample do
let(:sample) { described_class.new(__FILE__, [], 150.4, 2) }
describe '#id' do
it 'returns the ID' do
expect(sample.id).to be_an_instance_of(String)
end
end
describe '#file' do
it 'returns the file path' do
expect(sample.file).to eq(__FILE__)
end
end
describe '#line_samples' do
it 'returns the line samples' do
expect(sample.line_samples).to eq([])
end
end
describe '#events' do
it 'returns the total number of events' do
expect(sample.events).to eq(2)
end
end
describe '#duration' do
it 'returns the total execution time' do
expect(sample.duration).to eq(150.4)
end
end
describe '#relative_path' do
it 'returns the relative path' do
expect(sample.relative_path)
.to eq('spec/lib/gitlab/sherlock/file_sample_spec.rb')
end
end
describe '#to_param' do
it 'returns the sample ID' do
expect(sample.to_param).to eq(sample.id)
end
end
describe '#source' do
it 'returns the contents of the file' do
expect(sample.source).to eq(File.read(__FILE__))
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Sherlock::LineProfiler do
let(:profiler) { described_class.new }
describe '#profile' do
it 'runs the profiler when using MRI' do
allow(profiler).to receive(:mri?).and_return(true)
allow(profiler).to receive(:profile_mri)
profiler.profile { 'cats' }
end
it 'raises NotImplementedError when profiling an unsupported platform' do
allow(profiler).to receive(:mri?).and_return(false)
expect { profiler.profile { 'cats' } }.to raise_error(NotImplementedError)
end
end
describe '#profile_mri' do
it 'returns an Array containing the return value and profiling samples' do
allow(profiler).to receive(:lineprof)
.and_yield
.and_return({ __FILE__ => [[0, 0, 0, 0]] })
retval, samples = profiler.profile_mri { 42 }
expect(retval).to eq(42)
expect(samples).to eq([])
end
end
describe '#aggregate_rblineprof' do
let(:raw_samples) do
{ __FILE__ => [[30000, 30000, 5, 0], [15000, 15000, 4, 0]] }
end
it 'returns an Array of FileSample objects' do
samples = profiler.aggregate_rblineprof(raw_samples)
expect(samples).to be_an_instance_of(Array)
expect(samples[0]).to be_an_instance_of(Gitlab::Sherlock::FileSample)
end
describe 'the first FileSample object' do
let(:file_sample) do
profiler.aggregate_rblineprof(raw_samples)[0]
end
it 'uses the correct file path' do
expect(file_sample.file).to eq(__FILE__)
end
it 'contains a list of line samples' do
line_sample = file_sample.line_samples[0]
expect(line_sample).to be_an_instance_of(Gitlab::Sherlock::LineSample)
expect(line_sample.duration).to eq(15.0)
expect(line_sample.events).to eq(4)
end
it 'contains the total file execution time' do
expect(file_sample.duration).to eq(30.0)
end
it 'contains the total amount of file events' do
expect(file_sample.events).to eq(5)
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Sherlock::LineSample do
let(:sample) { described_class.new(150.0, 4) }
describe '#duration' do
it 'returns the duration' do
expect(sample.duration).to eq(150.0)
end
end
describe '#events' do
it 'returns the amount of events' do
expect(sample.events).to eq(4)
end
end
describe '#percentage_of' do
it 'returns the percentage of 1500.0' do
expect(sample.percentage_of(1500.0)).to be_within(0.1).of(10.0)
end
end
describe '#majority_of' do
it 'returns true if the sample takes up the majority of the given duration' do
expect(sample.majority_of?(500.0)).to eq(true)
end
it "returns false if the sample doesn't take up the majority of the given duration" do
expect(sample.majority_of?(1500.0)).to eq(false)
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Sherlock::Location do
let(:location) { described_class.new(__FILE__, 1) }
describe 'from_ruby_location' do
it 'creates a Location from a Thread::Backtrace::Location' do
input = caller_locations[0]
output = described_class.from_ruby_location(input)
expect(output).to be_an_instance_of(described_class)
expect(output.path).to eq(input.path)
expect(output.line).to eq(input.lineno)
end
end
describe '#path' do
it 'returns the file path' do
expect(location.path).to eq(__FILE__)
end
end
describe '#line' do
it 'returns the line number' do
expect(location.line).to eq(1)
end
end
describe '#application?' do
it 'returns true for an application frame' do
expect(location.application?).to eq(true)
end
it 'returns false for a non application frame' do
loc = described_class.new('/tmp/cats.rb', 1)
expect(loc.application?).to eq(false)
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Sherlock::Middleware do
let(:app) { double(:app) }
let(:middleware) { described_class.new(app) }
describe '#call' do
describe 'when instrumentation is enabled' do
it 'instruments a request' do
allow(middleware).to receive(:instrument?).and_return(true)
allow(middleware).to receive(:call_with_instrumentation)
middleware.call({})
end
end
describe 'when instrumentation is disabled' do
it "doesn't instrument a request" do
allow(middleware).to receive(:instrument).and_return(false)
allow(app).to receive(:call)
middleware.call({})
end
end
end
describe '#call_with_instrumentation' do
it 'instruments a request' do
trans = double(:transaction)
retval = 'cats are amazing'
env = {}
allow(app).to receive(:call).with(env).and_return(retval)
allow(middleware).to receive(:transaction_from_env).and_return(trans)
allow(trans).to receive(:run).and_yield.and_return(retval)
allow(Gitlab::Sherlock.collection).to receive(:add).with(trans)
middleware.call_with_instrumentation(env)
end
end
describe '#instrument?' do
it 'returns false for a text/css request' do
env = { 'HTTP_ACCEPT' => 'text/css', 'REQUEST_URI' => '/' }
expect(middleware.instrument?(env)).to eq(false)
end
it 'returns false for a request to a Sherlock route' do
env = {
'HTTP_ACCEPT' => 'text/html',
'REQUEST_URI' => '/sherlock/transactions'
}
expect(middleware.instrument?(env)).to eq(false)
end
it 'returns true for a request that should be instrumented' do
env = {
'HTTP_ACCEPT' => 'text/html',
'REQUEST_URI' => '/cats'
}
expect(middleware.instrument?(env)).to eq(true)
end
end
describe '#transaction_from_env' do
it 'returns a Transaction' do
env = {
'HTTP_ACCEPT' => 'text/html',
'REQUEST_URI' => '/cats'
}
expect(middleware.transaction_from_env(env))
.to be_an_instance_of(Gitlab::Sherlock::Transaction)
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Sherlock::Query do
let(:started_at) { Time.utc(2015, 1, 1) }
let(:finished_at) { started_at + 5 }
let(:query) do
described_class.new('SELECT COUNT(*) FROM users', started_at, finished_at)
end
describe 'new_with_bindings' do
it 'returns a Query' do
sql = 'SELECT COUNT(*) FROM users WHERE id = $1'
bindings = [[double(:column), 10]]
query = described_class
.new_with_bindings(sql, bindings, started_at, finished_at)
expect(query.query).to eq('SELECT COUNT(*) FROM users WHERE id = 10;')
end
end
describe '#id' do
it 'returns a String' do
expect(query.id).to be_an_instance_of(String)
end
end
describe '#query' do
it 'returns the query with a trailing semi-colon' do
expect(query.query).to eq('SELECT COUNT(*) FROM users;')
end
end
describe '#started_at' do
it 'returns the start time' do
expect(query.started_at).to eq(started_at)
end
end
describe '#finished_at' do
it 'returns the completion time' do
expect(query.finished_at).to eq(finished_at)
end
end
describe '#backtrace' do
it 'returns the backtrace' do
expect(query.backtrace).to be_an_instance_of(Array)
end
end
describe '#duration' do
it 'returns the duration in milliseconds' do
expect(query.duration).to be_within(0.1).of(5000.0)
end
end
describe '#to_param' do
it 'returns the query ID' do
expect(query.to_param).to eq(query.id)
end
end
describe '#formatted_query' do
it 'returns a formatted version of the query' do
expect(query.formatted_query).to eq(<<-EOF.strip)
SELECT COUNT(*)
FROM users;
EOF
end
end
describe '#last_application_frame' do
it 'returns the last application frame' do
frame = query.last_application_frame
expect(frame).to be_an_instance_of(Gitlab::Sherlock::Location)
expect(frame.path).to eq(__FILE__)
end
end
describe '#application_backtrace' do
it 'returns an Array of application frames' do
frames = query.application_backtrace
expect(frames).to be_an_instance_of(Array)
expect(frames).not_to be_empty
frames.each do |frame|
expect(frame.path).to start_with(Rails.root.to_s)
end
end
end
describe '#explain' do
it 'returns the query plan as a String' do
lines = [
['Aggregate (cost=123 rows=1)'],
[' -> Index Only Scan using index_cats_are_amazing']
]
result = double(:result, values: lines)
allow(query).to receive(:raw_explain).and_return(result)
expect(query.explain).to eq(<<-EOF.strip)
Aggregate (cost=123 rows=1)
-> Index Only Scan using index_cats_are_amazing
EOF
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Sherlock::Transaction do
let(:transaction) { described_class.new('POST', '/cat_pictures') }
describe '#id' do
it 'returns the transaction ID' do
expect(transaction.id).to be_an_instance_of(String)
end
end
describe '#type' do
it 'returns the type' do
expect(transaction.type).to eq('POST')
end
end
describe '#path' do
it 'returns the path' do
expect(transaction.path).to eq('/cat_pictures')
end
end
describe '#queries' do
it 'returns an Array of queries' do
expect(transaction.queries).to be_an_instance_of(Array)
end
end
describe '#file_samples' do
it 'returns an Array of file samples' do
expect(transaction.file_samples).to be_an_instance_of(Array)
end
end
describe '#started_at' do
it 'returns the start time' do
allow(transaction).to receive(:profile_lines).and_yield
transaction.run { 'cats are amazing' }
expect(transaction.started_at).to be_an_instance_of(Time)
end
end
describe '#finished_at' do
it 'returns the completion time' do
allow(transaction).to receive(:profile_lines).and_yield
transaction.run { 'cats are amazing' }
expect(transaction.finished_at).to be_an_instance_of(Time)
end
end
describe '#view_counts' do
it 'returns a Hash' do
expect(transaction.view_counts).to be_an_instance_of(Hash)
end
it 'sets the default value of a key to 0' do
expect(transaction.view_counts['cats.rb']).to be_zero
end
end
describe '#run' do
it 'runs the transaction' do
allow(transaction).to receive(:profile_lines).and_yield
retval = transaction.run { 'cats are amazing' }
expect(retval).to eq('cats are amazing')
end
end
describe '#duration' do
it 'returns the duration in seconds' do
start_time = Time.now
allow(transaction).to receive(:started_at).and_return(start_time)
allow(transaction).to receive(:finished_at).and_return(start_time + 5)
expect(transaction.duration).to be_within(0.1).of(5.0)
end
end
describe '#query_duration' do
it 'returns the total query duration in seconds' do
time = Time.now
query1 = Gitlab::Sherlock::Query.new('SELECT 1', time, time + 5)
query2 = Gitlab::Sherlock::Query.new('SELECT 2', time, time + 2)
transaction.queries << query1
transaction.queries << query2
expect(transaction.query_duration).to be_within(0.1).of(7.0)
end
end
describe '#to_param' do
it 'returns the transaction ID' do
expect(transaction.to_param).to eq(transaction.id)
end
end
describe '#sorted_queries' do
it 'returns the queries in descending order' do
start_time = Time.now
query1 = Gitlab::Sherlock::Query.new('SELECT 1', start_time, start_time)
query2 = Gitlab::Sherlock::Query
.new('SELECT 2', start_time, start_time + 5)
transaction.queries << query1
transaction.queries << query2
expect(transaction.sorted_queries).to eq([query2, query1])
end
end
describe '#sorted_file_samples' do
it 'returns the file samples in descending order' do
sample1 = Gitlab::Sherlock::FileSample.new(__FILE__, [], 10.0, 1)
sample2 = Gitlab::Sherlock::FileSample.new(__FILE__, [], 15.0, 1)
transaction.file_samples << sample1
transaction.file_samples << sample2
expect(transaction.sorted_file_samples).to eq([sample2, sample1])
end
end
describe '#find_query' do
it 'returns a Query when found' do
query = Gitlab::Sherlock::Query.new('SELECT 1', Time.now, Time.now)
transaction.queries << query
expect(transaction.find_query(query.id)).to eq(query)
end
it 'returns nil when no query could be found' do
expect(transaction.find_query('cats')).to be_nil
end
end
describe '#find_file_sample' do
it 'returns a FileSample when found' do
sample = Gitlab::Sherlock::FileSample.new(__FILE__, [], 10.0, 1)
transaction.file_samples << sample
expect(transaction.find_file_sample(sample.id)).to eq(sample)
end
it 'returns nil when no file sample could be found' do
expect(transaction.find_file_sample('cats')).to be_nil
end
end
describe '#profile_lines' do
describe 'when line profiling is enabled' do
it 'yields the block using the line profiler' do
allow(Gitlab::Sherlock).to receive(:enable_line_profiler?)
.and_return(true)
allow_next_instance_of(Gitlab::Sherlock::LineProfiler) do |instance|
allow(instance).to receive(:profile).and_return('cats are amazing', [])
end
retval = transaction.profile_lines { 'cats are amazing' }
expect(retval).to eq('cats are amazing')
end
end
describe 'when line profiling is disabled' do
it 'yields the block' do
allow(Gitlab::Sherlock).to receive(:enable_line_profiler?)
.and_return(false)
retval = transaction.profile_lines { 'cats are amazing' }
expect(retval).to eq('cats are amazing')
end
end
end
describe '#subscribe_to_active_record' do
let(:subscription) { transaction.subscribe_to_active_record }
let(:time) { Time.now }
let(:query_data) { { sql: 'SELECT 1', binds: [] } }
after do
ActiveSupport::Notifications.unsubscribe(subscription)
end
it 'tracks executed queries' do
expect(transaction).to receive(:track_query)
.with('SELECT 1', [], time, time)
subscription.publish('test', time, time, nil, query_data)
end
it 'only tracks queries triggered from the transaction thread' do
expect(transaction).not_to receive(:track_query)
Thread.new { subscription.publish('test', time, time, nil, query_data) }
.join
end
end
describe '#subscribe_to_action_view' do
let(:subscription) { transaction.subscribe_to_action_view }
let(:time) { Time.now }
let(:view_data) { { identifier: 'foo.rb' } }
after do
ActiveSupport::Notifications.unsubscribe(subscription)
end
it 'tracks rendered views' do
expect(transaction).to receive(:track_view).with('foo.rb')
subscription.publish('test', time, time, nil, view_data)
end
it 'only tracks views rendered from the transaction thread' do
expect(transaction).not_to receive(:track_view)
Thread.new { subscription.publish('test', time, time, nil, view_data) }
.join
end
end
end
......@@ -53,7 +53,6 @@ module SimpleCovEnv
track_files '{app,config/initializers,config/initializers_before_autoloader,db/post_migrate,haml_lint,lib,rubocop,tooling}/**/*.rb'
add_filter '/vendor/ruby/'
add_filter '/app/controllers/sherlock/' # Profiling tool used only in development
add_filter '/bin/'
add_filter 'db/fixtures/development/' # Matches EE files as well
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment