2017-05-29 18:18:46 -04:00
|
|
|
require 'prometheus/client'
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Metrics
|
|
|
|
module Prometheus
|
|
|
|
include Gitlab::CurrentSettings
|
|
|
|
|
2017-06-15 17:41:47 -04:00
|
|
|
def metrics_folder_present?
|
2017-06-20 06:22:56 -04:00
|
|
|
ENV.has_key?('prometheus_multiproc_dir') &&
|
|
|
|
::Dir.exist?(ENV['prometheus_multiproc_dir']) &&
|
2017-06-15 17:41:47 -04:00
|
|
|
::File.writable?(ENV['prometheus_multiproc_dir'])
|
|
|
|
end
|
|
|
|
|
2017-05-29 18:18:46 -04:00
|
|
|
def prometheus_metrics_enabled?
|
2017-06-20 06:22:56 -04: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
|
|
|
|
@registry ||= ::Prometheus::Client.registry
|
|
|
|
end
|
|
|
|
|
|
|
|
def counter(name, docstring, base_labels = {})
|
|
|
|
provide_metric(name) || registry.counter(name, docstring, base_labels)
|
|
|
|
end
|
|
|
|
|
|
|
|
def summary(name, docstring, base_labels = {})
|
|
|
|
provide_metric(name) || registry.summary(name, docstring, base_labels)
|
|
|
|
end
|
|
|
|
|
|
|
|
def gauge(name, docstring, base_labels = {})
|
|
|
|
provide_metric(name) || registry.gauge(name, docstring, base_labels)
|
|
|
|
end
|
|
|
|
|
|
|
|
def histogram(name, docstring, base_labels = {}, buckets = ::Prometheus::Client::Histogram::DEFAULT_BUCKETS)
|
|
|
|
provide_metric(name) || registry.histogram(name, docstring, base_labels, buckets)
|
|
|
|
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
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
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
|