gitlab-org--gitlab-foss/lib/gitlab/metrics/prometheus.rb

74 lines
2 KiB
Ruby
Raw Normal View History

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
def metrics_folder_present?
multiprocess_files_dir = ::Prometheus::Client.configuration.multiprocess_files_dir
multiprocess_files_dir &&
::Dir.exist?(multiprocess_files_dir) &&
::File.writable?(multiprocess_files_dir)
end
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
end
def registry
2017-09-07 14:22:01 -04:00
return @registry if @registry
REGISTRY_MUTEX.synchronize do
@registry ||= ::Prometheus::Client.registry
end
end
def counter(name, docstring, base_labels = {})
2017-09-07 14:22:01 -04:00
safe_provide_metric(:counter, name, docstring, base_labels)
end
def summary(name, docstring, base_labels = {})
2017-09-07 14:22:01 -04:00
safe_provide_metric(:summary, name, docstring, base_labels)
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)
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
provide_metric(name) || registry.method(method).call(name, *args)
2017-09-07 14:22:01 -04:00
end
end
def provide_metric(name)
if prometheus_metrics_enabled?
registry.get(name)
else
NullMetric.new
end
end
def prometheus_metrics_enabled_unmemoized
metrics_folder_present? && current_application_settings[:prometheus_metrics_enabled] || false
end
end
end
2017-06-01 08:38:40 -04:00
end