2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-01-14 18:37:52 +05:30
|
|
|
module Gitlab
|
|
|
|
module Metrics
|
2018-03-17 18:26:18 +05:30
|
|
|
include Gitlab::Metrics::Prometheus
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
EXECUTION_MEASUREMENT_BUCKETS = [0.001, 0.01, 0.1, 1].freeze
|
2016-01-14 18:37:52 +05:30
|
|
|
|
2020-01-12 00:16:45 +05:30
|
|
|
@error = false
|
|
|
|
|
2016-01-14 18:37:52 +05:30
|
|
|
def self.enabled?
|
2020-05-24 23:13:21 +05:30
|
|
|
prometheus_metrics_enabled?
|
2016-01-14 18:37:52 +05:30
|
|
|
end
|
2020-01-12 00:16:45 +05:30
|
|
|
|
|
|
|
def self.error?
|
|
|
|
@error
|
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
def self.record_duration_for_status?(status)
|
|
|
|
status.to_i.between?(200, 499)
|
|
|
|
end
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
def self.server_error?(status)
|
|
|
|
status.to_i >= 500
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
# Tracks an event.
|
|
|
|
#
|
|
|
|
# See `Gitlab::Metrics::Transaction#add_event` for more details.
|
|
|
|
def self.add_event(*args)
|
|
|
|
current_transaction&.add_event(*args)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Allow access from other metrics related middlewares
|
|
|
|
def self.current_transaction
|
2021-12-11 22:18:48 +05:30
|
|
|
WebTransaction.current || BackgroundTransaction.current
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the prefix to use for the name of a series.
|
|
|
|
def self.series_prefix
|
|
|
|
@series_prefix ||= Gitlab::Runtime.sidekiq? ? 'sidekiq_' : 'rails_'
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.settings
|
|
|
|
@settings ||= begin
|
|
|
|
current_settings = Gitlab::CurrentSettings.current_application_settings
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
method_call_threshold: current_settings[:metrics_method_call_threshold]
|
|
|
|
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.method_call_threshold
|
|
|
|
# This is memoized since this method is called for every instrumented
|
|
|
|
# method. Loading data from an external cache on every method call slows
|
|
|
|
# things down too much.
|
|
|
|
# in milliseconds
|
|
|
|
@method_call_threshold ||= settings[:method_call_threshold]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Measures the execution time of a block.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# Gitlab::Metrics.measure(:find_by_username_duration) do
|
|
|
|
# UserFinder.new(some_username).find_by_username
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# name - The name of the field to store the execution time in.
|
|
|
|
#
|
|
|
|
# Returns the value yielded by the supplied block.
|
|
|
|
def self.measure(name)
|
|
|
|
trans = current_transaction
|
|
|
|
|
|
|
|
return yield unless trans
|
|
|
|
|
|
|
|
real_start = System.monotonic_time
|
|
|
|
cpu_start = System.cpu_time
|
|
|
|
|
|
|
|
retval = yield
|
|
|
|
|
|
|
|
cpu_stop = System.cpu_time
|
|
|
|
real_stop = System.monotonic_time
|
|
|
|
|
|
|
|
real_time = (real_stop - real_start)
|
|
|
|
cpu_time = cpu_stop - cpu_start
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
trans.observe("gitlab_#{name}_real_duration_seconds".to_sym, real_time) do
|
2020-05-24 23:13:21 +05:30
|
|
|
docstring "Measure #{name}"
|
|
|
|
buckets EXECUTION_MEASUREMENT_BUCKETS
|
|
|
|
end
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
trans.observe("gitlab_#{name}_cpu_duration_seconds".to_sym, cpu_time) do
|
2020-05-24 23:13:21 +05:30
|
|
|
docstring "Measure #{name}"
|
|
|
|
buckets EXECUTION_MEASUREMENT_BUCKETS
|
|
|
|
with_feature "prometheus_metrics_measure_#{name}_cpu_duration"
|
|
|
|
end
|
|
|
|
|
|
|
|
retval
|
|
|
|
end
|
2016-01-14 18:37:52 +05:30
|
|
|
end
|
|
|
|
end
|