2016-06-17 11:45:37 -04:00
|
|
|
module Gitlab
|
|
|
|
module Metrics
|
|
|
|
# Class for tracking timing information about method calls
|
|
|
|
class MethodCall
|
2018-01-29 06:33:08 -05:00
|
|
|
include Gitlab::Metrics::Methods
|
2017-09-06 09:40:41 -04:00
|
|
|
BASE_LABELS = { module: nil, method: nil }.freeze
|
2017-10-16 09:18:59 -04:00
|
|
|
attr_reader :real_time, :cpu_time, :call_count, :labels
|
2016-06-17 11:45:37 -04:00
|
|
|
|
2018-01-22 05:55:36 -05:00
|
|
|
define_histogram :gitlab_method_call_duration_seconds do
|
|
|
|
docstring 'Method calls real duration'
|
|
|
|
base_labels Transaction::BASE_LABELS.merge(BASE_LABELS)
|
|
|
|
buckets [0.01, 0.05, 0.1, 0.5, 1]
|
|
|
|
with_feature :prometheus_metrics_method_instrumentation
|
|
|
|
end
|
2017-12-11 18:23:56 -05:00
|
|
|
|
2017-09-05 21:02:08 -04:00
|
|
|
# name - The full name of the method (including namespace) such as
|
|
|
|
# `User#sign_in`.
|
|
|
|
#
|
2017-09-06 07:59:21 -04:00
|
|
|
def initialize(name, module_name, method_name, transaction)
|
|
|
|
@module_name = module_name
|
|
|
|
@method_name = method_name
|
2017-09-06 07:35:47 -04:00
|
|
|
@transaction = transaction
|
2016-06-17 11:45:37 -04:00
|
|
|
@name = name
|
2017-10-16 09:18:59 -04:00
|
|
|
@labels = { module: @module_name, method: @method_name }
|
2017-12-20 13:30:58 -05:00
|
|
|
@real_time = 0.0
|
|
|
|
@cpu_time = 0.0
|
2016-06-17 11:45:37 -04:00
|
|
|
@call_count = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
# Measures the real and CPU execution time of the supplied block.
|
|
|
|
def measure
|
2016-06-24 07:44:50 -04:00
|
|
|
start_real = System.monotonic_time
|
2016-06-17 11:45:37 -04:00
|
|
|
start_cpu = System.cpu_time
|
|
|
|
retval = yield
|
|
|
|
|
2017-09-06 14:36:25 -04:00
|
|
|
real_time = System.monotonic_time - start_real
|
|
|
|
cpu_time = System.cpu_time - start_cpu
|
|
|
|
|
|
|
|
@real_time += real_time
|
2017-09-07 14:24:31 -04:00
|
|
|
@cpu_time += cpu_time
|
2016-06-17 11:45:37 -04:00
|
|
|
@call_count += 1
|
|
|
|
|
2018-01-12 11:55:53 -05:00
|
|
|
if above_threshold?
|
2018-01-15 16:06:40 -05:00
|
|
|
self.class.gitlab_method_call_duration_seconds.observe(@transaction.labels.merge(labels), real_time)
|
2017-11-22 18:26:50 -05:00
|
|
|
end
|
2017-08-22 10:13:51 -04:00
|
|
|
|
2016-06-17 11:45:37 -04:00
|
|
|
retval
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a Metric instance of the current method call.
|
|
|
|
def to_metric
|
|
|
|
Metric.new(
|
2017-08-22 10:13:51 -04:00
|
|
|
Instrumentation.series,
|
2016-06-17 11:45:37 -04:00
|
|
|
{
|
2017-12-20 13:30:58 -05:00
|
|
|
duration: real_time.in_milliseconds.to_i,
|
|
|
|
cpu_duration: cpu_time.in_milliseconds.to_i,
|
2017-08-22 10:13:51 -04:00
|
|
|
call_count: call_count
|
2016-06-17 11:45:37 -04:00
|
|
|
},
|
|
|
|
method: @name
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns true if the total runtime of this method exceeds the method call
|
|
|
|
# threshold.
|
|
|
|
def above_threshold?
|
2017-12-20 13:30:58 -05:00
|
|
|
real_time.in_milliseconds >= Metrics.method_call_threshold
|
2016-06-17 11:45:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|