summaryrefslogtreecommitdiff
path: root/lib/gitlab/sherlock/transaction.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/gitlab/sherlock/transaction.rb')
-rw-r--r--lib/gitlab/sherlock/transaction.rb29
1 files changed, 23 insertions, 6 deletions
diff --git a/lib/gitlab/sherlock/transaction.rb b/lib/gitlab/sherlock/transaction.rb
index 5cb3e86aa4e..4641f15ee33 100644
--- a/lib/gitlab/sherlock/transaction.rb
+++ b/lib/gitlab/sherlock/transaction.rb
@@ -4,11 +4,12 @@ module Gitlab
attr_reader :id, :type, :path, :queries, :file_samples, :started_at,
:finished_at
+ # 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
- @duration = 0
@queries = []
@file_samples = []
@started_at = nil
@@ -16,6 +17,7 @@ module Gitlab
@thread = Thread.current
end
+ # Runs the transaction and returns the block's return value.
def run
@started_at = Time.now
@@ -30,34 +32,43 @@ module Gitlab
retval
end
+ # Returns the duration in seconds.
def duration
- @started_at && @finished_at ? @finished_at - @started_at : 0
+ @duration ||= started_at && finished_at ? finished_at - started_at : 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 track_query(query, bindings, start, finish)
- @queries << Query.new_with_bindings(query, bindings, start, finish)
- end
-
def profile_lines
retval = nil
@@ -70,6 +81,12 @@ module Gitlab
retval
end
+ private
+
+ def track_query(query, bindings, start, finish)
+ @queries << Query.new_with_bindings(query, bindings, start, finish)
+ end
+
def subscribe_to_active_record
ActiveSupport::Notifications.subscribe('sql.active_record') do |_, start, finish, _, data|
# In case somebody uses a multi-threaded server locally (e.g. Puma) we