Class: Gitlab::Sherlock::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/sherlock/transaction.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, path) ⇒ Transaction

type - The type of transaction (e.g. “GET”, “POST”, etc) path - The path of the transaction (e.g. the HTTP request path)


9
10
11
12
13
14
15
16
17
18
19
# File 'lib/gitlab/sherlock/transaction.rb', line 9

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

Instance Attribute Details

#file_samplesObject (readonly)

Returns the value of attribute file_samples


4
5
6
# File 'lib/gitlab/sherlock/transaction.rb', line 4

def file_samples
  @file_samples
end

#finished_atObject (readonly)

Returns the value of attribute finished_at


4
5
6
# File 'lib/gitlab/sherlock/transaction.rb', line 4

def finished_at
  @finished_at
end

#idObject (readonly)

Returns the value of attribute id


4
5
6
# File 'lib/gitlab/sherlock/transaction.rb', line 4

def id
  @id
end

#pathObject (readonly)

Returns the value of attribute path


4
5
6
# File 'lib/gitlab/sherlock/transaction.rb', line 4

def path
  @path
end

#queriesObject (readonly)

Returns the value of attribute queries


4
5
6
# File 'lib/gitlab/sherlock/transaction.rb', line 4

def queries
  @queries
end

#started_atObject (readonly)

Returns the value of attribute started_at


4
5
6
# File 'lib/gitlab/sherlock/transaction.rb', line 4

def started_at
  @started_at
end

#typeObject (readonly)

Returns the value of attribute type


4
5
6
# File 'lib/gitlab/sherlock/transaction.rb', line 4

def type
  @type
end

#view_countsObject (readonly)

Returns the value of attribute view_counts


4
5
6
# File 'lib/gitlab/sherlock/transaction.rb', line 4

def view_counts
  @view_counts
end

Instance Method Details

#durationObject

Returns the duration in seconds.


35
36
37
# File 'lib/gitlab/sherlock/transaction.rb', line 35

def duration
  @duration ||= started_at && finished_at ? finished_at - started_at : 0
end

#find_file_sample(id) ⇒ Object

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.


72
73
74
# File 'lib/gitlab/sherlock/transaction.rb', line 72

def find_file_sample(id)
  @file_samples.find { |sample| sample.id == id }
end

#find_query(id) ⇒ Object

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.


63
64
65
# File 'lib/gitlab/sherlock/transaction.rb', line 63

def find_query(id)
  @queries.find { |query| query.id == id }
end

#profile_linesObject


76
77
78
79
80
81
82
83
84
85
86
# File 'lib/gitlab/sherlock/transaction.rb', line 76

def profile_lines
  retval = nil

  if Sherlock.enable_line_profiler?
    retval, @file_samples = LineProfiler.new.profile { yield }
  else
    retval = yield
  end

  retval
end

#query_durationObject

Returns the total query duration in seconds.


40
41
42
# File 'lib/gitlab/sherlock/transaction.rb', line 40

def query_duration
  @query_duration ||= @queries.map { |q| q.duration }.inject(:+) / 1000.0
end

#runObject

Runs the transaction and returns the block's return value.


22
23
24
25
26
27
28
29
30
31
32
# File 'lib/gitlab/sherlock/transaction.rb', line 22

def run
  @started_at = Time.now

  retval = with_subscriptions do
    profile_lines { yield }
  end

  @finished_at = Time.now

  retval
end

#sorted_file_samplesObject

Returns the file samples sorted in descending order by their durations.


54
55
56
# File 'lib/gitlab/sherlock/transaction.rb', line 54

def sorted_file_samples
  @file_samples.sort { |a, b| b.duration <=> a.duration }
end

#sorted_queriesObject

Returns the queries sorted in descending order by their durations.


49
50
51
# File 'lib/gitlab/sherlock/transaction.rb', line 49

def sorted_queries
  @queries.sort { |a, b| b.duration <=> a.duration }
end

#subscribe_to_action_viewObject


96
97
98
99
100
101
102
103
104
# File 'lib/gitlab/sherlock/transaction.rb', line 96

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

#subscribe_to_active_recordObject


88
89
90
91
92
93
94
# File 'lib/gitlab/sherlock/transaction.rb', line 88

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

#to_paramObject


44
45
46
# File 'lib/gitlab/sherlock/transaction.rb', line 44

def to_param
  @id
end