2017-05-29 18:18:46 -04:00
|
|
|
require 'prometheus/client'
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Metrics
|
|
|
|
module Prometheus
|
|
|
|
include Gitlab::CurrentSettings
|
|
|
|
|
2017-09-07 14:22:01 -04:00
|
|
|
REGISTRY_MUTEX = Mutex.new
|
|
|
|
PROVIDER_MUTEX = Mutex.new
|
|
|
|
|
2017-06-15 17:41:47 -04:00
|
|
|
def metrics_folder_present?
|
2017-07-19 04:54:39 -04:00
|
|
|
multiprocess_files_dir = ::Prometheus::Client.configuration.multiprocess_files_dir
|
|
|
|
|
|
|
|
multiprocess_files_dir &&
|
|
|
|
::Dir.exist?(multiprocess_files_dir) &&
|
|
|
|
::File.writable?(multiprocess_files_dir)
|
2017-06-15 17:41:47 -04:00
|
|
|
end
|
|
|
|
|
2017-05-29 18:18:46 -04:00
|
|
|
def prometheus_metrics_enabled?
|
2017-11-22 18:37:55 -05:00
|
|
|
return @prometheus_metrics_enabled if defined?(@prometheus_metrics_enabled)
|
|
|
|
|
|
|
|
@prometheus_metrics_enabled = prometheus_metrics_enabled_unmemoized
|
2017-05-29 18:18:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def registry
|
2017-09-07 14:22:01 -04:00
|
|
|
return @registry if @registry
|
|
|
|
|
|
|
|
REGISTRY_MUTEX.synchronize do
|
|
|
|
@registry ||= ::Prometheus::Client.registry
|
|
|
|
end
|
2017-05-29 18:18:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def counter(name, docstring, base_labels = {})
|
2017-09-07 14:22:01 -04:00
|
|
|
safe_provide_metric(:counter, name, docstring, base_labels)
|
2017-05-29 18:18:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def summary(name, docstring, base_labels = {})
|
2017-09-07 14:22:01 -04:00
|
|
|
safe_provide_metric(:summary, name, docstring, base_labels)
|
2017-05-29 18:18:46 -04:00
|
|
|
end
|
|
|
|
|
2017-07-04 11:28:34 -04:00
|
|
|
def gauge(name, docstring, base_labels = {}, multiprocess_mode = :all)
|
2017-09-07 14:22:01 -04:00
|
|
|
safe_provide_metric(:gauge, name, docstring, base_labels, multiprocess_mode)
|
2017-05-29 18:18:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def histogram(name, docstring, base_labels = {}, buckets = ::Prometheus::Client::Histogram::DEFAULT_BUCKETS)
|
2017-09-07 14:22:01 -04:00
|
|
|
safe_provide_metric(:histogram, name, docstring, base_labels, buckets)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def safe_provide_metric(method, name, *args)
|
|
|
|
metric = provide_metric(name)
|
|
|
|
return metric if metric
|
|
|
|
|
|
|
|
PROVIDER_MUTEX.synchronize do
|
2017-09-07 17:13:40 -04:00
|
|
|
provide_metric(name) || registry.method(method).call(name, *args)
|
2017-09-07 14:22:01 -04:00
|
|
|
end
|
2017-05-29 18:18:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def provide_metric(name)
|
|
|
|
if prometheus_metrics_enabled?
|
|
|
|
registry.get(name)
|
|
|
|
else
|
|
|
|
NullMetric.new
|
|
|
|
end
|
|
|
|
end
|
2017-06-15 17:41:47 -04:00
|
|
|
|
|
|
|
def prometheus_metrics_enabled_unmemoized
|
|
|
|
metrics_folder_present? && current_application_settings[:prometheus_metrics_enabled] || false
|
|
|
|
end
|
2017-05-29 18:18:46 -04:00
|
|
|
end
|
|
|
|
end
|
2017-06-01 08:38:40 -04:00
|
|
|
end
|