2016-06-17 15:45:37 +00:00
|
|
|
module Gitlab
|
|
|
|
module Metrics
|
|
|
|
# Class for tracking timing information about method calls
|
|
|
|
class MethodCall
|
|
|
|
attr_reader :real_time, :cpu_time, :call_count
|
|
|
|
|
|
|
|
# name - The full name of the method (including namespace) such as
|
|
|
|
# `User#sign_in`.
|
|
|
|
#
|
2017-08-22 14:13:51 +00:00
|
|
|
def self.call_real_duration_histogram
|
|
|
|
@call_real_duration_histogram ||= Gitlab::Metrics.histogram(:gitlab_method_call_real_duration_milliseconds,
|
|
|
|
'Method calls real duration',
|
2017-09-04 23:34:28 +00:00
|
|
|
{call_name: nil},
|
2017-08-22 14:13:51 +00:00
|
|
|
[1, 2, 5, 10, 20, 50, 100, 1000])
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.call_cpu_duration_histogram
|
|
|
|
@call_duration_histogram ||= Gitlab::Metrics.histogram(:gitlab_method_call_cpu_duration_milliseconds,
|
|
|
|
'Method calls cpu duration',
|
2017-09-04 23:34:28 +00:00
|
|
|
{call_name: nil},
|
2017-08-22 14:13:51 +00:00
|
|
|
[1, 2, 5, 10, 20, 50, 100, 1000])
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2017-09-04 23:34:28 +00:00
|
|
|
def initialize(name)
|
2016-06-17 15:45:37 +00:00
|
|
|
@name = name
|
2016-07-28 13:08:57 +00:00
|
|
|
@real_time = 0
|
|
|
|
@cpu_time = 0
|
2016-06-17 15:45:37 +00:00
|
|
|
@call_count = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
# Measures the real and CPU execution time of the supplied block.
|
|
|
|
def measure
|
2016-06-24 11:44:50 +00:00
|
|
|
start_real = System.monotonic_time
|
2016-06-17 15:45:37 +00:00
|
|
|
start_cpu = System.cpu_time
|
|
|
|
retval = yield
|
|
|
|
|
2016-06-24 11:44:50 +00:00
|
|
|
@real_time += System.monotonic_time - start_real
|
|
|
|
@cpu_time += System.cpu_time - start_cpu
|
2016-06-17 15:45:37 +00:00
|
|
|
@call_count += 1
|
|
|
|
|
2017-08-22 14:13:51 +00:00
|
|
|
if above_threshold?
|
2017-09-04 23:34:28 +00:00
|
|
|
self.class.call_real_duration_histogram.observe({ call_name: @name }, @real_time)
|
|
|
|
self.class.call_cpu_duration_histogram.observe({ call_name: @name }, @cpu_time)
|
2017-08-22 14:13:51 +00:00
|
|
|
end
|
|
|
|
|
2016-06-17 15:45:37 +00:00
|
|
|
retval
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a Metric instance of the current method call.
|
|
|
|
def to_metric
|
|
|
|
Metric.new(
|
2017-08-22 14:13:51 +00:00
|
|
|
Instrumentation.series,
|
2016-06-17 15:45:37 +00:00
|
|
|
{
|
2017-08-22 14:13:51 +00:00
|
|
|
duration: real_time,
|
2016-06-17 15:45:37 +00:00
|
|
|
cpu_duration: cpu_time,
|
2017-08-22 14:13:51 +00:00
|
|
|
call_count: call_count
|
2016-06-17 15:45:37 +00:00
|
|
|
},
|
|
|
|
method: @name
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns true if the total runtime of this method exceeds the method call
|
|
|
|
# threshold.
|
|
|
|
def above_threshold?
|
|
|
|
real_time >= Metrics.method_call_threshold
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|