2018-11-16 19:37:17 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-29 18:18:46 -04:00
|
|
|
require 'prometheus/client'
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Metrics
|
|
|
|
module Prometheus
|
2018-01-19 10:30:51 -05:00
|
|
|
extend ActiveSupport::Concern
|
2017-05-29 18:18:46 -04:00
|
|
|
|
2017-09-07 14:22:01 -04:00
|
|
|
REGISTRY_MUTEX = Mutex.new
|
|
|
|
PROVIDER_MUTEX = Mutex.new
|
|
|
|
|
2018-01-19 10:30:51 -05:00
|
|
|
class_methods do
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
2017-07-19 04:54:39 -04:00
|
|
|
|
2018-01-19 10:30:51 -05:00
|
|
|
def metrics_folder_present?
|
|
|
|
multiprocess_files_dir = ::Prometheus::Client.configuration.multiprocess_files_dir
|
2017-06-15 17:41:47 -04:00
|
|
|
|
2018-01-19 10:30:51 -05:00
|
|
|
multiprocess_files_dir &&
|
|
|
|
::Dir.exist?(multiprocess_files_dir) &&
|
|
|
|
::File.writable?(multiprocess_files_dir)
|
|
|
|
end
|
|
|
|
|
|
|
|
def prometheus_metrics_enabled?
|
|
|
|
strong_memoize(:prometheus_metrics_enabled) do
|
|
|
|
prometheus_metrics_enabled_unmemoized
|
|
|
|
end
|
2017-11-17 07:27:16 -05:00
|
|
|
end
|
2017-05-29 18:18:46 -04:00
|
|
|
|
2018-05-09 01:29:30 -04:00
|
|
|
def reset_registry!
|
|
|
|
clear_memoization(:registry)
|
|
|
|
|
|
|
|
REGISTRY_MUTEX.synchronize do
|
|
|
|
::Prometheus::Client.reset!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-19 10:30:51 -05:00
|
|
|
def registry
|
|
|
|
strong_memoize(:registry) do
|
|
|
|
REGISTRY_MUTEX.synchronize do
|
|
|
|
strong_memoize(:registry) do
|
|
|
|
::Prometheus::Client.registry
|
|
|
|
end
|
2017-11-17 07:27:16 -05:00
|
|
|
end
|
|
|
|
end
|
2017-09-07 14:22:01 -04:00
|
|
|
end
|
2017-05-29 18:18:46 -04:00
|
|
|
|
2018-01-19 10:30:51 -05:00
|
|
|
def counter(name, docstring, base_labels = {})
|
|
|
|
safe_provide_metric(:counter, name, docstring, base_labels)
|
|
|
|
end
|
2017-05-29 18:18:46 -04:00
|
|
|
|
2018-01-19 10:30:51 -05:00
|
|
|
def summary(name, docstring, base_labels = {})
|
|
|
|
safe_provide_metric(:summary, name, docstring, base_labels)
|
|
|
|
end
|
2017-05-29 18:18:46 -04:00
|
|
|
|
2018-01-19 10:30:51 -05:00
|
|
|
def gauge(name, docstring, base_labels = {}, multiprocess_mode = :all)
|
|
|
|
safe_provide_metric(:gauge, name, docstring, base_labels, multiprocess_mode)
|
|
|
|
end
|
2017-05-29 18:18:46 -04:00
|
|
|
|
2018-01-19 10:30:51 -05:00
|
|
|
def histogram(name, docstring, base_labels = {}, buckets = ::Prometheus::Client::Histogram::DEFAULT_BUCKETS)
|
|
|
|
safe_provide_metric(:histogram, name, docstring, base_labels, buckets)
|
|
|
|
end
|
2017-09-07 14:22:01 -04:00
|
|
|
|
2018-01-19 10:30:51 -05:00
|
|
|
private
|
2017-09-07 14:22:01 -04:00
|
|
|
|
2018-01-19 10:30:51 -05:00
|
|
|
def safe_provide_metric(method, name, *args)
|
|
|
|
metric = provide_metric(name)
|
|
|
|
return metric if metric
|
2017-09-07 14:22:01 -04:00
|
|
|
|
2018-01-19 10:30:51 -05:00
|
|
|
PROVIDER_MUTEX.synchronize do
|
|
|
|
provide_metric(name) || registry.method(method).call(name, *args)
|
|
|
|
end
|
2017-09-07 14:22:01 -04:00
|
|
|
end
|
2017-05-29 18:18:46 -04:00
|
|
|
|
2018-01-19 10:30:51 -05:00
|
|
|
def provide_metric(name)
|
|
|
|
if prometheus_metrics_enabled?
|
|
|
|
registry.get(name)
|
|
|
|
else
|
|
|
|
NullMetric.instance
|
|
|
|
end
|
2017-05-29 18:18:46 -04:00
|
|
|
end
|
2017-06-15 17:41:47 -04:00
|
|
|
|
2018-01-19 10:30:51 -05:00
|
|
|
def prometheus_metrics_enabled_unmemoized
|
2018-02-02 13:39:55 -05:00
|
|
|
metrics_folder_present? && Gitlab::CurrentSettings.prometheus_metrics_enabled || false
|
2018-01-19 10:30:51 -05:00
|
|
|
end
|
2017-06-15 17:41:47 -04:00
|
|
|
end
|
2017-05-29 18:18:46 -04:00
|
|
|
end
|
|
|
|
end
|
2017-06-01 08:38:40 -04:00
|
|
|
end
|