Class: Gitlab::Metrics::MethodCall

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/metrics/method_call.rb

Overview

Class for tracking timing information about method calls

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, module_name, method_name, transaction) ⇒ MethodCall

name - The full name of the method (including namespace) such as

`User#sign_in`.


12
13
14
15
16
17
18
19
20
21
# File 'lib/gitlab/metrics/method_call.rb', line 12

def initialize(name, module_name, method_name, transaction)
  @module_name = module_name
  @method_name = method_name
  @transaction = transaction
  @name = name
  @labels = { module: @module_name, method: @method_name }
  @real_time = 0.0
  @cpu_time = 0.0
  @call_count = 0
end

Instance Attribute Details

#call_countObject (readonly)

Returns the value of attribute call_count.



7
8
9
# File 'lib/gitlab/metrics/method_call.rb', line 7

def call_count
  @call_count
end

#cpu_timeObject (readonly)

Returns the value of attribute cpu_time.



7
8
9
# File 'lib/gitlab/metrics/method_call.rb', line 7

def cpu_time
  @cpu_time
end

#real_timeObject (readonly)

Returns the value of attribute real_time.



7
8
9
# File 'lib/gitlab/metrics/method_call.rb', line 7

def real_time
  @real_time
end

Instance Method Details

#above_threshold?Boolean

Returns true if the total runtime of this method exceeds the method call threshold.

Returns:

  • (Boolean)


50
51
52
# File 'lib/gitlab/metrics/method_call.rb', line 50

def above_threshold?
  real_time.in_milliseconds >= ::Gitlab::Metrics.method_call_threshold
end

#measureObject

Measures the real and CPU execution time of the supplied block.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gitlab/metrics/method_call.rb', line 24

def measure
  start_real = System.monotonic_time
  start_cpu = System.cpu_time
  retval = yield

  real_time = System.monotonic_time - start_real
  cpu_time = System.cpu_time - start_cpu

  @real_time += real_time
  @cpu_time += cpu_time
  @call_count += 1

  if above_threshold? && transaction
    label_keys = labels.keys
    transaction.observe(:gitlab_method_call_duration_seconds, real_time, labels) do
      docstring 'Method calls real duration'
      label_keys label_keys
      buckets [0.01, 0.05, 0.1, 0.5, 1]
    end
  end

  retval
end