2015-11-04 13:13:19 -05:00
|
|
|
module Gitlab
|
|
|
|
module Sherlock
|
|
|
|
class Transaction
|
|
|
|
attr_reader :id, :type, :path, :queries, :file_samples, :started_at,
|
2015-11-09 06:36:01 -05:00
|
|
|
:finished_at, :view_counts
|
2015-11-04 13:13:19 -05:00
|
|
|
|
2015-11-05 12:01:05 -05:00
|
|
|
# type - The type of transaction (e.g. "GET", "POST", etc)
|
|
|
|
# path - The path of the transaction (e.g. the HTTP request path)
|
2015-11-04 13:13:19 -05:00
|
|
|
def initialize(type, path)
|
|
|
|
@id = SecureRandom.uuid
|
|
|
|
@type = type
|
|
|
|
@path = path
|
|
|
|
@queries = []
|
|
|
|
@file_samples = []
|
|
|
|
@started_at = nil
|
|
|
|
@finished_at = nil
|
|
|
|
@thread = Thread.current
|
2015-11-09 06:36:01 -05:00
|
|
|
@view_counts = Hash.new(0)
|
2015-11-04 13:13:19 -05:00
|
|
|
end
|
|
|
|
|
2015-11-05 12:01:05 -05:00
|
|
|
# Runs the transaction and returns the block's return value.
|
2015-11-04 13:13:19 -05:00
|
|
|
def run
|
|
|
|
@started_at = Time.now
|
|
|
|
|
2015-11-09 06:36:01 -05:00
|
|
|
retval = with_subscriptions do
|
|
|
|
profile_lines { yield }
|
|
|
|
end
|
2015-11-04 13:13:19 -05:00
|
|
|
|
|
|
|
@finished_at = Time.now
|
|
|
|
|
|
|
|
retval
|
|
|
|
end
|
|
|
|
|
2015-11-05 12:01:05 -05:00
|
|
|
# Returns the duration in seconds.
|
2015-11-04 13:13:19 -05:00
|
|
|
def duration
|
2015-11-05 12:01:05 -05:00
|
|
|
@duration ||= started_at && finished_at ? finished_at - started_at : 0
|
2015-11-04 13:13:19 -05:00
|
|
|
end
|
|
|
|
|
2015-11-19 06:33:58 -05:00
|
|
|
# Returns the total query duration in seconds.
|
|
|
|
def query_duration
|
|
|
|
@query_duration ||= @queries.map { |q| q.duration }.inject(:+) / 1000.0
|
|
|
|
end
|
|
|
|
|
2015-11-04 13:13:19 -05:00
|
|
|
def to_param
|
|
|
|
@id
|
|
|
|
end
|
|
|
|
|
2015-11-05 12:01:05 -05:00
|
|
|
# Returns the queries sorted in descending order by their durations.
|
2015-11-04 13:13:19 -05:00
|
|
|
def sorted_queries
|
|
|
|
@queries.sort { |a, b| b.duration <=> a.duration }
|
|
|
|
end
|
|
|
|
|
2015-11-05 12:01:05 -05:00
|
|
|
# Returns the file samples sorted in descending order by their durations.
|
2015-11-04 13:13:19 -05:00
|
|
|
def sorted_file_samples
|
|
|
|
@file_samples.sort { |a, b| b.duration <=> a.duration }
|
|
|
|
end
|
|
|
|
|
2015-11-05 12:01:05 -05:00
|
|
|
# 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.
|
2015-11-04 13:13:19 -05:00
|
|
|
def find_query(id)
|
|
|
|
@queries.find { |query| query.id == id }
|
|
|
|
end
|
|
|
|
|
2015-11-05 12:01:05 -05:00
|
|
|
# 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.
|
2015-11-04 13:13:19 -05:00
|
|
|
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
|
|
|
|
|
2015-11-09 06:36:01 -05:00
|
|
|
def subscribe_to_active_record
|
|
|
|
ActiveSupport::Notifications.subscribe('sql.active_record') do |_, start, finish, _, data|
|
|
|
|
next unless same_thread?
|
|
|
|
|
|
|
|
track_query(data[:sql].strip, data[:binds], start, finish)
|
|
|
|
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
|
|
|
|
|
2015-11-05 12:01:05 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def track_query(query, bindings, start, finish)
|
|
|
|
@queries << Query.new_with_bindings(query, bindings, start, finish)
|
|
|
|
end
|
|
|
|
|
2015-11-09 06:36:01 -05:00
|
|
|
def track_view(path)
|
|
|
|
@view_counts[path] += 1
|
|
|
|
end
|
2015-11-04 13:13:19 -05:00
|
|
|
|
2015-11-09 06:36:01 -05:00
|
|
|
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
|
2015-11-04 13:13:19 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|