2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
module Gitlab
|
|
|
|
module Metrics
|
|
|
|
module Prometheus
|
2018-03-17 18:26:18 +05:30
|
|
|
extend ActiveSupport::Concern
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
REGISTRY_MUTEX = Mutex.new
|
|
|
|
PROVIDER_MUTEX = Mutex.new
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
class_methods do
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def metrics_folder_present?
|
|
|
|
multiprocess_files_dir = ::Prometheus::Client.configuration.multiprocess_files_dir
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
multiprocess_files_dir &&
|
|
|
|
::Dir.exist?(multiprocess_files_dir) &&
|
|
|
|
::File.writable?(multiprocess_files_dir)
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def prometheus_metrics_enabled?
|
|
|
|
strong_memoize(:prometheus_metrics_enabled) do
|
|
|
|
prometheus_metrics_enabled_unmemoized
|
|
|
|
end
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
def reset_registry!
|
|
|
|
clear_memoization(:registry)
|
|
|
|
|
|
|
|
REGISTRY_MUTEX.synchronize do
|
2022-08-13 15:12:31 +05:30
|
|
|
::Prometheus::Client.cleanup!
|
2018-11-08 19:23:39 +05:30
|
|
|
::Prometheus::Client.reset!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def registry
|
|
|
|
strong_memoize(:registry) do
|
|
|
|
REGISTRY_MUTEX.synchronize do
|
|
|
|
strong_memoize(:registry) do
|
|
|
|
::Prometheus::Client.registry
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def counter(name, docstring, base_labels = {})
|
|
|
|
safe_provide_metric(:counter, name, docstring, base_labels)
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def summary(name, docstring, base_labels = {})
|
|
|
|
safe_provide_metric(:summary, name, docstring, base_labels)
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def gauge(name, docstring, base_labels = {}, multiprocess_mode = :all)
|
|
|
|
safe_provide_metric(:gauge, name, docstring, base_labels, multiprocess_mode)
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def histogram(name, docstring, base_labels = {}, buckets = ::Prometheus::Client::Histogram::DEFAULT_BUCKETS)
|
|
|
|
safe_provide_metric(:histogram, name, docstring, base_labels, buckets)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
2020-01-12 00:16:45 +05:30
|
|
|
def error_detected!
|
|
|
|
set_error!(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear_errors!
|
|
|
|
set_error!(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_error!(status)
|
|
|
|
clear_memoization(:prometheus_metrics_enabled)
|
|
|
|
|
|
|
|
PROVIDER_MUTEX.synchronize do
|
|
|
|
@error = status
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def safe_provide_metric(method, name, *args)
|
|
|
|
metric = provide_metric(name)
|
|
|
|
return metric if metric
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
PROVIDER_MUTEX.synchronize do
|
|
|
|
provide_metric(name) || registry.method(method).call(name, *args)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def provide_metric(name)
|
|
|
|
if prometheus_metrics_enabled?
|
|
|
|
registry.get(name)
|
|
|
|
else
|
|
|
|
NullMetric.instance
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def prometheus_metrics_enabled_unmemoized
|
2020-01-12 00:16:45 +05:30
|
|
|
!error? && metrics_folder_present? && Gitlab::CurrentSettings.prometheus_metrics_enabled || false
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|