2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
module Gitlab
|
|
|
|
module Metrics
|
|
|
|
# Class for tracking timing information about method calls
|
|
|
|
class MethodCall
|
2020-10-24 23:57:45 +05:30
|
|
|
attr_reader :real_time, :cpu_time, :call_count
|
2016-06-22 15:30:34 +05:30
|
|
|
|
|
|
|
# name - The full name of the method (including namespace) such as
|
|
|
|
# `User#sign_in`.
|
|
|
|
#
|
2018-03-17 18:26:18 +05:30
|
|
|
def initialize(name, module_name, method_name, transaction)
|
|
|
|
@module_name = module_name
|
|
|
|
@method_name = method_name
|
|
|
|
@transaction = transaction
|
2016-06-22 15:30:34 +05:30
|
|
|
@name = name
|
2018-03-17 18:26:18 +05:30
|
|
|
@labels = { module: @module_name, method: @method_name }
|
|
|
|
@real_time = 0.0
|
|
|
|
@cpu_time = 0.0
|
2016-06-22 15:30:34 +05:30
|
|
|
@call_count = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
# Measures the real and CPU execution time of the supplied block.
|
|
|
|
def measure
|
2016-08-24 12:49:21 +05:30
|
|
|
start_real = System.monotonic_time
|
2016-06-22 15:30:34 +05:30
|
|
|
start_cpu = System.cpu_time
|
|
|
|
retval = yield
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
real_time = System.monotonic_time - start_real
|
|
|
|
cpu_time = System.cpu_time - start_cpu
|
|
|
|
|
|
|
|
@real_time += real_time
|
|
|
|
@cpu_time += cpu_time
|
2016-06-22 15:30:34 +05:30
|
|
|
@call_count += 1
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
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
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
retval
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns true if the total runtime of this method exceeds the method call
|
|
|
|
# threshold.
|
|
|
|
def above_threshold?
|
2019-07-07 11:18:12 +05:30
|
|
|
real_time.in_milliseconds >= ::Gitlab::Metrics.method_call_threshold
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
2020-10-24 23:57:45 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :labels, :transaction
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|